appsero/client

Appsero 客户端


README

版本 2.0.2

安装

您可以通过 composer 和手动两种方式安装 AppSero 客户端。

1. Composer 安装

在您的项目(主题/插件)中添加依赖项

composer require appsero/client

现在,如果您还没有做的话,请将 autoload.php 添加到您的文件中。

require __DIR__ . '/vendor/autoload.php';

2. 手动安装

在您的项目中克隆存储库。

cd /path/to/your/project/folder
git clone https://github.com/AppSero/client.git appsero

现在将依赖项包含到您的插件/主题中。

if( !class_exists('Appsero\Client') ) {
    require __DIR__ . '/appsero/src/Client.php';
}

洞察

AppSero 可用于主题和插件。

Appsero\Client 类有三个参数

$client = new Appsero\Client( $hash, $name, $file );
  • hash (字符串, 必需) - 插件或主题的唯一标识符。
  • name (字符串, 必需) - 插件或主题的名称。
  • file (字符串, 必需) - 插件的主要文件路径。对于主题,是到 functions.php 的路径

使用示例

在开始使用该类之前,请参考安装步骤。

您可以在 Appsero 控制台 获取您的插件 hash。第 3 个参数 必须 是插件的主要文件。

/**
 * Initialize the tracker
 *
 * @return void
 */
function appsero_init_tracker_appsero_test() {

    if ( ! class_exists( 'Appsero\Client' ) ) {
        require_once __DIR__ . '/appsero/src/Client.php';
    }

    $client = new Appsero\Client( 'a4a8da5b-b419-4656-98e9-4a42e9044891', 'Akismet', __FILE__ );

    // Active insights
    $client->insights()->init();

    // Active license page and checker
    $args = array(
        'type'       => 'options',
        'menu_title' => 'Akismet',
        'page_title' => 'Akismet License Settings',
        'menu_slug'  => 'akismet_settings',
    );
    $client->license()->add_settings_page( $args );
}

appsero_init_tracker_appsero_test();

请直接调用此函数,切勿使用任何动作钩子来调用此函数。

以下是用于主插件文件的插件示例代码。以下是用于主题的 functions.php 文件的示例代码。

使用更新器(管理 Pro 插件更新)

默认情况下,Appsero 客户端不包含此客户端的更新器功能。如果您想管理您的高级插件的更新,请将 更新器 分别包含在您的产品中。

更多使用方法

$client = new Appsero\Client( 'a4a8da5b-b419-4656-98e9-4a42e9044892', 'Twenty Twelve', __FILE__ );

1. 隐藏通知

有时您可能不想显示通知,或者想要自定义通知消息。您也可以这样做。

$client->insights()
       ->hide_notice()
       ->init();

2. 自定义通知消息

$client->insights()
       ->notice( 'My Custom Notice Message' )
       ->init();

3. 添加额外数据

您可以从您的主题或插件中添加额外元数据。在这种情况下,必须从 Appsero 控制台中白名单 keysadd_extra 方法还支持将回调作为参数,如果您需要进行数据库调用,则回调是最佳选择。

$metadata = array(
    'key'     => 'value',
    'another' => 'another_value'
);
$client->insights()
       ->add_extra( $metadata )
       ->init();

或者,如果您想执行查询,请传递回调,我们将在必要时调用该函数。

$metadata = function () {
    $total_posts = wp_count_posts();

    return array(
        'total_posts' => $total_posts,
        'another'     => 'another_value'
    );
};
$client->insights()
       ->add_extra( $metadata )
       ->init();

4. 设置文本域

您可以设置自己的文本域以翻译文本。

$client->set_textdomain( 'your-project-textdomain' );

5. 获取插件数据

如果您想获取与您的插件或主题一起使用的最常用的插件,请将活动插件的请求数据发送到 Appsero。

$client->insights()
       ->add_plugin_data()
       ->init();

6. 设置通知消息

更改同意消息文本

$client->insights()
       ->notice("Your custom notice text")
       ->init();

检查许可证有效性

检查您的插件/主题是否使用有效的许可证,首先创建一个 License 对象的全局变量,然后在代码中的任何地方使用它。如果您在相同函数之外使用它,请确保在使用条件之前将变量全局化。

$client = new Appsero\Client( 'a4a8da5b-b419-4656-98e9-4a42e9044892', 'Twenty Twelve', __FILE__ );

$args = array(
    'type'        => 'submenu',
    'menu_title'  => 'Twenty Twelve License',
    'page_title'  => 'Twenty Twelve License Settings',
    'menu_slug'   => 'twenty_twelve_settings',
    'parent_slug' => 'themes.php',
);

global $twenty_twelve_license;
$twenty_twelve_license = $client->license();
$twenty_twelve_license->add_settings_page( $args );

if ( $twenty_twelve_license->is_valid()  ) {
    // Your special code here
}

Or check by pricing plan title

if ( $twenty_twelve_license->is_valid_by( 'title', 'Business' ) ) {
    // Your special code here
}

// Set custom options key for storing the license info
$twenty_twelve_license->set_option_key( 'my_plugin_license' );

使用您自己的许可证表单

您可以通过使用 HTTP 请求创建表单轻松管理许可证。从许可证对象调用 license_form_submit 方法。

global $twenty_twelve_license; // License object
$twenty_twelve_license->license_form_submit([
    '_nonce'      => wp_create_nonce( 'Twenty Twelve' ), // create a nonce with name
    '_action'     => 'active', // active, deactive
    'license_key' => 'random-license-key', // no need to provide if you want to deactive
]);
if ( ! $twenty_twelve_license->error ) {
    // license activated
    $twenty_twelve_license->success; // Success message is here
} else {
    $twenty_twelve_license->error; // has error message here
}

设置自定义停用原因

首先在Appsero仪表板中设置您的停用原因,然后在您的插件/主题中使用过滤器钩子进行映射。

  • id 是停用别名
  • text 是停用标题
  • placeholder 将显示在文本区域字段上
  • icon 您可以设置23x23大小的SVG图标
add_filter( 'appsero_custom_deactivation_reasons', function () {
    return [
        [
            'id'          => 'looks-buggy',
            'text'        => 'Looks buggy',
            'placeholder' => 'Can you please tell which feature looks buggy?',
            'icon'        => '',
        ],
        [
            'id'          => 'bad-ui',
            'text'        => 'Bad UI',
            'placeholder' => 'Could you tell us a bit more?',
            'icon'        => '',
        ],
    ];
} );


扩展操作

1. 允许跟踪权限后

// Fires after tracking permission allowed (optin)
function sample_tracker_optin(array $data){
    // use data, as it's now permitted to send anywhere
    // Like FLuentCRM
}
add_action('PLUGIN_OR_THEME_SLUG_tracker_optin', 'sample_tracker_optin', 10);

2. 拒绝跟踪权限后

// Fires after tracking permission denied (optout)
function sample_tracker_optout(){
    // Don't ask for further permission, respect user's decision 
}
add_action('PLUGIN_OR_THEME_SLUG_tracker_optout', 'sample_tracker_optout', 10);

3. 许可证激活后

// Fires after license is activated successfully
function sample_license_activated(array $response){
    // use response
    // response has license information
    // Like FLuentCRM
}
add_action('PLUGIN_OR_THEME_SLUG_license_activated', 'sample_license_activated', 10);

4. 许可证停用后

// Fires after license deactivated successfully
function sample_license_deactivated(array $response){
    // use response
    // response has license information
}
add_action('PLUGIN_OR_THEME_SLUG_license_deactivated', 'sample_license_deactivated', 10);

5. 许可证刷新后

// Fires after license refreshed successfully
function sample_license_refreshed(){
    // license just refreshed
}
add_action('PLUGIN_OR_THEME_SLUG_license_refreshed', 'sample_license_refreshed', 10);

6. 提交卸载原因后

// Fires after uninstall reason submitted
function sample_uninstall_reason_submitted(array $data){
    // use the data
    // Like FLuentCRM
}
add_action('PLUGIN_OR_THEME_SLUG_uninstall_reason_submitted', 'sample_uninstall_reason_submitted', 10);

致谢

Appsero创建和维护。