承接 stellarwp/uplink 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

stellarwp/uplink

Composer 安装命令:

composer require stellarwp/uplink

包简介

A library that integrates a WordPress product with the StellarWP Licensing system.

README 文档

README

CI Static Analysis

Installation

It's recommended that you install Uplink as a project dependency via Composer:

composer require stellarwp/uplink

We actually recommend that this library gets included in your project using Strauss.

Luckily, adding Strauss to your composer.json is only slightly more complicated than adding a typical dependency, so checkout our strauss docs.

Initialize the library

Initializing the StellarWP Uplink library should be done within the plugins_loaded action, preferably at priority 0.

use StellarWP\Uplink\Uplink;

add_action( 'plugins_loaded', function() {
	/**
	 * Configure the container.
	 *
	 * The container must be compatible with stellarwp/container-contract.
	 * See here: https://github.com/stellarwp/container-contract#usage.
	 *
	 * If you do not have a container, we recommend https://github.com/lucatume/di52
	 * and the corresponding wrapper:
	 * https://github.com/stellarwp/container-contract/blob/main/examples/di52/Container.php
	 */
	$container = new Container();
	Config::set_container( $container );
	Config::set_hook_prefix( 'my-custom-prefix' );

	/*
	 * If you wish to allow a customer to authorize their product, set your Token Auth Prefix.
	 *
	 * This will allow storage of a unique token associated with the customer's license/domain.
	 *
	 * Important: The Token auth prefix should be the same across all of your products.
	 */
	Config::set_token_auth_prefix( 'my_origin' );

	// Optionally, change the default auth token caching.
	Config::set_auth_cache_expiration( WEEK_IN_SECONDS );

	// Or, disable it completely.
	Config::set_auth_cache_expiration( -1 );

	Uplink::init();
}, 0 );

Translation

Package is using __( 'Invalid request: nonce field is expired. Please try again.', '%TEXTDOMAIN%' ) function for translation. In order to change domain placeholder '%TEXTDOMAIN%' to your plugin translation domain run

./bin/stellar-uplink domain=<your-plugin-domain>

or

./bin/stellar-uplink

and prompt the plugin domain You can also add lines below to your composer file in order to run command automatically

"scripts": {
	"stellar-uplink": [
	  "vendor/bin/stellar-uplink domain=<your-plugin-domain>"
	],
	"post-install-cmd": [
	  "@stellar-uplink"
	],
	"post-update-cmd": [
	  "@stellar-uplink"
	]
  }

Embedding a license in your plugin

StellarWP Uplink plugins are downloaded with an embedded license key so that users do not need to manually enter the key when activating their plugin. To make this possible, the class must be in a specific location so that the licensing server can find it.

# The class file should be in this path:
src/Uplink/Helper.php

Note

While the default is src/Uplink/Helper.php, the licensing server supports embedding the license in any file path defined on the server.

The file should match the following - keeping the KEY constant set to a blank string, or, if you want a default license key, set it to that.:

<?php declare( strict_types=1 );

namespace Whatever\Namespace\Uplink;

final class Helper {
	public const KEY = '';
}

Registering a plugin

Registers a plugin for licensing and updates.

use StellarWP\Uplink\Register;

$plugin_slug    = 'my-plugin';
$plugin_name    = 'My Plugin';
$plugin_version = MyPlugin::VERSION;
$plugin_path    = 'my-plugin/my-plugin.php';
$plugin_class   = MyPlugin::class;
$license_class  = MyPlugin\Uplink\Helper::class;

Register::plugin(
	$plugin_slug,
	$plugin_name,
	$plugin_version,
	$plugin_path,
	$plugin_class,
	$license_class, // This is optional.
	false // Whether this is an oAuth plugin. Default false.
);

Registering a service

Registers a service for licensing. Since services require a plugin, we pull version and class information from the plugin.

use StellarWP\Uplink\Register;

$service_slug    = 'my-service';
$service_name    = 'My Service';
$service_version = MyPlugin::VERSION;
$plugin_path     = 'my-plugin/my-plugin.php';
$plugin_class    = MyPlugin::class;

Register::service(
	$service_slug,
	$service_name,
	$service_version,
	$plugin_path,
	$plugin_class,
	null,
	false
);

Render license key form on your settings page

In order to render license key form just add the following to your settings page, tab, etc.

⚠️ This will render license key fields for all of your registered plugins/services in the same Uplink/Container instance.

use StellarWP\Uplink as UplinkNamespace;

$form = UplinkNamespace\get_form();
$plugins = UplinkNamespace\get_plugins();

foreach ( $plugins as $plugin ) {
	$field = UplinkNamespace\get_field( $plugin->get_slug() );
	// Tha name property of the input field.
	$field->set_field_name( 'field-' . $slug );
	$form->add_field( $field );
}

$form->render();
// or echo $form->get_render_html();

To render a single product's license key, use the following:

use StellarWP\Uplink as UplinkNamespace;

$field = UplinkNamespace\get_field( 'my-test-plugin' );

$field->render();
// or echo $field->get_render_html();

Example: Register settings page and render license fields

Register a settings page for a plugin if you need it.

add_action( 'admin_menu', function () {
    add_menu_page(
        'Sample',
        'Sample',
        'manage_options',
        'sample-plugin-lib',
        'render_settings_page',
        '',
        null
    );

}, 11 );

Add lines below to your settings page. This will render license key form with titles and a submit button.

use StellarWP\Uplink as UplinkNamespace;

function render_settings_page() {
    // ...
	$form = UplinkNamespace\get_form();
	$plugins = UplinkNamespace\get_plugins();

	foreach ( $plugins as $plugin ) {
		$field = UplinkNamespace\get_field( $plugin->get_slug() );
		// Tha name property of the input field.
		$field->set_field_name( 'field-' . $slug );
		$form->add_field( $field );
	}

	$form->show_button( true, __( 'Submit', 'text-domain' ) );

	$form->render();

    //....
}

License Authorization

⚠️ Your auth_url is set on the Origins table on the Stellar Licensing server! You must first request to have this added before proceeding.

There may be certain functionality you wish to make available when you know a license is authorized.

This library provides the tools to fetch and store unique tokens, working together with the Uplink Origin plugin.

After following the instructions at the top to define a Config::set_token_auth_prefix(), this will enable the following functionality:

  1. The ability to render a "Connect" button anywhere in your plugin while the user is in wp-admin, using the provided function below.
  2. The button will display "Disconnect" once they are authorized, which deletes the locally stored Token.
  3. The ability for the customer's site to accept specific Query Variables in wp-admin, that will store the generated Token, and an optional new License Key for a Product Slug.
  4. Check if a license is authorized, either in the License Validation payload, or manually.

⚠️ Generating a Token requires manual configuration on your Origin site using the Uplink Origin Plugin.

Render Authorize Button

💡 Note: the button is only rendered if the following conditions are met:

  1. You have an auth_url set on the StellarWP Licensing Server.
  2. The current user is a Super Admin (can be changed with a WP filter).
  3. This is not a multisite installation, or...
  4. If multisite and using subfolders, only on the root network dashboard.
  5. If multisite and NOT using subfolders and on a subsite AND a token doesn't already exist at the network level, in which case it needs to be managed at the network.
// Call the namespaced function with your plugin slug.
\StellarWP\Uplink\render_authorize_button( 'kadence-blocks-pro' );

You can also pass in a custom license domain, which can be fetched on the Uplink Origin side from the uplink_domain query variable:

// Call the namespaced function with your plugin slug and license domain.
\StellarWP\Uplink\render_authorize_button( 'kadence-blocks-pro', 'customer-site.com' );

💡 The button is very customizable with filters, see Authorize_Button_Controller.php.

Manually Check if a License is Remotely Authorized

This connects to the licensing server to check in real time if the license is authorized. Use sparingly.

$token       = \StellarWP\Uplink\get_authorization_token( 'my-plugin-slug' );
$license_key = \StellarWP\Uplink\get_license_key( 'my-plugin-slug' );
$domain      = \StellarWP\Uplink\get_license_domain();

if ( ! $token || ! $license_key || ! $domain ) {
	return; // or, log/show errors.
}

$is_authorized = \StellarWP\Uplink\is_authorized( $license_key, 'my-plugin-slug', $token, $domain );

echo $is_authorized ? esc_html__( 'authorized' ) : esc_html__( 'not authorized' );

Manually Fetch Auth URL

If for some reason you need to fetch your auth_url manually, you can do so by:

echo esc_url( \StellarWP\Uplink\get_auth_url( 'my-plugin-slug' ) );

💡 Auth URL connections are cached for one day using transients.

Callback Redirect

The Callback Redirect generated by the Origin looks something like this, where uplinksample.lndo.site is your customer's website:

https://uplinksample.lndo.site/wp-admin/import.php?uplink_token=d9a407d0-0eb1-41cf-8cd0-e5da668143b4&_uplink_nonce=Oyj13TCvhaa12IJm

The Origin is responsible for asking StellarWP Licensing to generate a token and redirect back to where the customer originally clicked on the button.

The following Query Variables are available for reference:

💡 Note: This data automatically gets stored when detected, using the admin_init hook!

  1. uplink_token - The unique UUIDv4 token generated by StellarWP Licensing.
  2. _uplink_nonce - The original nonce sent with the callback URL, as part of the "Connect" button.
  3. uplink_license (optional) - Whether we should also update or set a License Key.
  4. uplink_slug (optional) - The Product or Service Slug that we're updating the license for.

⚠️ uplink_slug MUST be supplied if uplink_license is!

Changelog

This project uses @stellarwp/changelogger to manage its changelog. All notable changes are tracked via changelog entry files in the changelog/ directory.

To add a new changelog entry:

bunx @stellarwp/changelogger add

To compile changelog entries into changelog.txt:

bunx @stellarwp/changelogger write --overwrite-version <version>

Additional documentation

  • Features — Overview of library features, license key validation flow, embedded keys, UI rendering, and API configuration constants.
  • Testing — How to set up and run automated tests with Codeception and slic.

stellarwp/uplink 适用场景与选型建议

stellarwp/uplink 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 439.7k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2022 年 12 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 stellarwp/uplink 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 stellarwp/uplink 我们能提供哪些服务?
定制开发 / 二次开发

基于 stellarwp/uplink 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 439.7k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 24
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 19
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2022-12-21