定制 freemius/wordpress-sdk 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

freemius/wordpress-sdk

Composer 安装命令:

composer require freemius/wordpress-sdk

包简介

Freemius WordPress SDK

README 文档

README

Welcome to the official repository for the Freemius SDK! Adding the SDK to your WordPress plugin, theme, or add-ons, enables all the benefits that come with using the Freemius platform such as:

Freemius truly empowers developers to create prosperous subscription-based businesses.

If you're new to Freemius then we recommend taking a look at our Getting Started guide first.

If you're a WordPress plugin or theme developer and are interested in monetizing with Freemius then you can sign-up for a FREE account:

https://dashboard.freemius.com/register/

Once you have your account setup and are familiar with how it all works you're ready to begin integrating Freemius into your WordPress product

You can see some of the existing WordPress.org plugins & themes that are already utilizing the power of Freemius here:

Code Documentation

You can find the SDK's documentation here: https://freemius.com/help/documentation/wordpress-sdk/

Integrating & Initializing the SDK

As part of the integration process, you'll need to add the latest version of the Freemius SDK into your WordPress project.

Then, when you've completed the SDK integration form a snippet of code is generated which you'll need to copy and paste into the top of your main plugin's PHP file, right after the plugin's header comment.

Note: For themes, this will be in the root functions.php file instead.

A typical SDK snippet will look similar to the following (your particular snippet may differ slightly depending on your integration):

if ( ! function_exists( 'my_prefix_fs' ) ) {
    // Create a helper function for easy SDK access.
    function my_prefix_fs() {
        global $my_prefix_fs;

        if ( ! isset( $my_prefix_fs ) ) {
            // Include Freemius SDK.
            require_once dirname(__FILE__) . '/freemius/start.php';

            $my_prefix_fs = fs_dynamic_init( array(
                'id'                  => '1234',
                'slug'                => 'my-new-plugin',
                'premium_slug'        => 'my-new-plugin-premium',
                'type'                => 'plugin',
                'public_key'          => 'pk_bAEfta69seKymZzmf2xtqq8QXHz9y',
                'is_premium'          => true,
                // If your plugin is a serviceware, set this option to false.
                'has_premium_version' => true,
                'has_paid_plans'      => true,
                'is_org_compliant'    => true,
                'menu'                => array(
                    'slug'           => 'my-new-plugin',
                    'parent'         => array(
                        'slug' => 'options-general.php',
                    ),
                ),
                // Set the SDK to work in a sandbox mode (for development & testing).
                // IMPORTANT: MAKE SURE TO REMOVE SECRET KEY BEFORE DEPLOYMENT.
                'secret_key'          => 'sk_ubb4yN3mzqGR2x8#P7r5&@*xC$utE',
            ) );
        }

        return $my_prefix_fs;
    }

    // Init Freemius.
    my_prefix_fs();
    // Signal that SDK was initiated.
    do_action( 'my_prefix_fs_loaded' );
}

Usage example

You can call any SDK methods by prefixing them with the shortcode function for your particular plugin/theme (specified when completing the SDK integration form in the Developer Dashboard):

<?php my_prefix_fs()->get_upgrade_url(); ?>

Or when calling Freemius multiple times in a scope, it's recommended to use it with the global variable:

<?php
    global $my_prefix_fs;
    $my_prefix_fs->get_account_url();
?>

There are many other SDK methods available that you can use to enhance the functionality of your WordPress product. Some of the more common use-cases are covered in the Freemius SDK Gists documentation.

Adding license-based logic examples

Add marketing content that encourages your users to upgrade to a paid version:

<?php
    if ( my_prefix_fs()->is_not_paying() ) {
        echo '<section><h1>' . esc_html__('Awesome Premium Features', 'my-plugin-slug') . '</h1>';
        echo '<a href="' . my_prefix_fs()->get_upgrade_url() . '">' .
            esc_html__('Upgrade Now!', 'my-plugin-slug') .
            '</a>';
        echo '</section>';
    }
?>

Add logic which will only be available in your premium plugin version:

<?php
    // This "if" block will be auto removed from the Free version.
    if ( my_prefix_fs()->is__premium_only() ) {
    
        // ... premium only logic ...
        
    }
?>

To add a function which will only be available in your premium plugin version, add __premium_only as the suffix of the function name. Ensure that all lines that call that method directly or by hooks, are also wrapped in premium only logic:

<?php
    class My_Plugin {
        function init() {
            ...

            // This "if" block will be auto removed from the free version.
            if ( my_prefix_fs()->is__premium_only() ) {
                // Init premium version.
                $this->admin_init__premium_only();

                add_action( 'admin_init', array( &$this, 'admin_init_hook__premium_only' );
            }

            ...
        }

        // This method will be only included in the premium version.
        function admin_init__premium_only() {
            ...
        }

        // This method will be only included in the premium version.
        function admin_init_hook__premium_only() {
            ...
        }
    }
?>

Add logic which will only be executed for customers in your 'professional' plan:

<?php
    if ( my_prefix_fs()->is_plan('professional', true) ) {
        // .. logic related to Professional plan only ...
    }
?>

Add logic which will only be executed for customers in your 'professional' plan or higher plans:

<?php
    if ( my_prefix_fs()->is_plan('professional') ) {
        // ... logic related to Professional plan and higher plans ...
    }
?>

Add logic which will only be available in your premium plugin version AND will only be executed for customers in your 'professional' plan (and higher plans):

<?php
    // This "if" block will be auto removed from the Free version.
    if ( my_prefix_fs()->is_plan__premium_only('professional') ) {
        // ... logic related to Professional plan and higher plans ...
    }
?>

Add logic only for users in trial:

<?php
    if ( my_prefix_fs()->is_trial() ) {
        // ... logic for users in trial ...
    }
?>

Add logic for specified paid plan:

<?php
    // This "if" block will be auto removed from the Free version.
    if ( my_prefix_fs()->is__premium_only() ) {
        if ( my_prefix_fs()->is_plan( 'professional', true ) ) {

            // ... logic related to Professional plan only ...

        } else if ( my_prefix_fs()->is_plan( 'business' ) ) {

            // ... logic related to Business plan and higher plans ...

        }
    }
?>

Excluding files and folders from the free plugin version

There are two ways to exclude files from your free version.

  1. Add __premium_only just before the file extension. For example, functions__premium_only.php will be included only in the premium plugin version. This works for all types of files, not only PHP.
  2. Add @fs_premium_only a special meta tag to the plugin's main PHP file header. Example:
<?php
	/**
	 * Plugin Name: My Very Awesome Plugin
	 * Plugin URI:  http://my-awesome-plugin.com
	 * Description: Create and manage Awesomeness right in WordPress.
	 * Version:     1.0.0
	 * Author:      Awesomattic
	 * Author URI:  http://my-awesome-plugin.com/me/
	 * License:     GPLv2
	 * Text Domain: myplugin
	 * Domain Path: /langs
	 *
	 * @fs_premium_only /lib/functions.php, /premium-files/
	 */

	if ( ! defined( 'ABSPATH' ) ) {
		exit;
	}
    
    // ... my code ...
?>

In the example plugin header above, the file /lib/functions.php and the directory /premium-files/ will be removed from the free plugin version.

Hooks: Actions and Filters

Similar to WordPress’ filters and actions hooks, the Freemius WordPress SDK provides a collection of filters and actions that enable you to customize and extend its functionality in your WordPress plugins or themes.

WordPress.org Compliance

Based on WordPress.org Guidelines you are not allowed to submit a plugin that has premium code in it:

All code hosted by WordPress.org servers must be free and fully-functional. If you want to sell advanced features for a plugin (such as a "pro" version), then you must sell and serve that code from your own site, we will not host it on our servers.

Therefore, if you want to deploy your free plugin's version to WordPress.org, make sure you wrap all your premium code with if ( my_prefix_fs()->{{ method }}__premium_only() ) or use some of the other methods provided by the SDK to exclude premium features & files from the free version.

Deployment

Zip your Freemius product’s root folder and upload it in the Deployment section in the Freemius Developer's Dashboard. The plugin/theme will automatically be scanned and processed by a custom-developed PHP Processor which will auto-generate two versions of your plugin:

  1. Premium version: Identical to your uploaded version, including all code (except your secret_key). Will be enabled for download ONLY for your paying or in trial customers.
  2. Free version: The code stripped from all your paid features (based on the logic added wrapped in { method }__premium_only()).

The free version is the one that you should give your users to download. Therefore, download the free generated version and upload to your site. Or, if your plugin was WordPress.org compliant and you made sure to exclude all your premium code with the different provided techniques, you can deploy the downloaded free version to the .org repo.

License

Copyright (c) Freemius®, Inc.

Licensed under the GNU general public license (version 3).

Contributing

Please see our contributing guide.

freemius/wordpress-sdk 适用场景与选型建议

freemius/wordpress-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 146.24k 次下载、GitHub Stars 达 304, 最近一次更新时间为 2015 年 10 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「plugin」 「theme」 「wordpress」 「sdk」 「wordpress-plugin」 「freemius」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 freemius/wordpress-sdk 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 146.24k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 307
  • 点击次数: 14
  • 依赖项目数: 4
  • 推荐数: 1

GitHub 信息

  • Stars: 304
  • Watchers: 23
  • Forks: 91
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-only
  • 更新时间: 2015-10-14