sudar /
bulk-delete
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * License Handler for Bulk Delete Addons. |
||
| 4 | * |
||
| 5 | * @since 5.0 |
||
| 6 | * |
||
| 7 | * @author Sudar |
||
| 8 | * |
||
| 9 | * @package BulkDelete/License |
||
| 10 | */ |
||
| 11 | defined( 'ABSPATH' ) || exit; // Exit if accessed directly |
||
| 12 | |||
| 13 | class BD_License_Handler { |
||
| 14 | /** |
||
| 15 | * Name of the addon. |
||
| 16 | * |
||
| 17 | * @since 5.0 |
||
| 18 | */ |
||
| 19 | private $addon_name; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Code of the addon. |
||
| 23 | * |
||
| 24 | * @since 5.0 |
||
| 25 | */ |
||
| 26 | private $addon_code; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Version of the plugin. |
||
| 30 | * |
||
| 31 | * @since 5.0 |
||
| 32 | */ |
||
| 33 | private $version; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * plugin file name. |
||
| 37 | * |
||
| 38 | * @since 5.0 |
||
| 39 | */ |
||
| 40 | private $plugin_file; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Plugin base name. |
||
| 44 | * |
||
| 45 | * @since 5.5 |
||
| 46 | */ |
||
| 47 | private $plugin_basename; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Author of the plugin. |
||
| 51 | * |
||
| 52 | * @since 5.0 |
||
| 53 | */ |
||
| 54 | private $author; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Instance of the updater class. |
||
| 58 | * |
||
| 59 | * @since 5.5 |
||
| 60 | */ |
||
| 61 | private $updater; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Notice Message. |
||
| 65 | * |
||
| 66 | * @since 5.5 |
||
| 67 | */ |
||
| 68 | private $notice_msg = ''; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Constructor. |
||
| 72 | * |
||
| 73 | * @since 5.0 |
||
| 74 | * |
||
| 75 | * @param string $addon_name Name of the addon |
||
| 76 | * @param string $addon_code Code of the addon |
||
| 77 | * @param string $version Version of the addon |
||
| 78 | * @param string $plugin_file Addon file name |
||
| 79 | * @param string $author (optional) Author of the addon |
||
| 80 | */ |
||
| 81 | public function __construct( $addon_name, $addon_code, $version, $plugin_file, $author = 'Sudar Muthu' ) { |
||
| 82 | $this->addon_name = $addon_name; |
||
| 83 | $this->addon_code = $addon_code; |
||
| 84 | $this->version = $version; |
||
| 85 | $this->plugin_file = $plugin_file; |
||
| 86 | $this->plugin_basename = plugin_basename( $plugin_file ); |
||
| 87 | $this->author = $author; |
||
| 88 | |||
| 89 | $this->hooks(); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * setup hooks. |
||
| 94 | * |
||
| 95 | * @access private |
||
| 96 | * |
||
| 97 | * @since 5.0 |
||
| 98 | */ |
||
| 99 | private function hooks() { |
||
| 100 | add_action( 'admin_init', array( $this, 'check_license' ), 0 ); |
||
| 101 | add_action( 'admin_notices', array( $this, 'show_admin_notices' ) ); |
||
| 102 | add_action( 'after_plugin_row_' . $this->plugin_basename, array( $this, 'plugin_row' ), 11, 3 ); |
||
| 103 | |||
| 104 | add_action( 'bd_license_form' , array( $this, 'display_license_form' ) ); |
||
| 105 | add_action( 'bd_license_field', array( $this, 'add_license_field' ) ); |
||
| 106 | add_filter( 'bd_license_input', array( $this, 'parse_license_input' ), 1 ); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Check whether the license is valid for the addon. |
||
| 111 | * |
||
| 112 | * If the license is not valid then add a notice about it. |
||
| 113 | * If it is valid then hook the plugin updater. |
||
| 114 | * |
||
| 115 | * @since 5.5 |
||
| 116 | */ |
||
| 117 | public function check_license() { |
||
| 118 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
||
| 119 | return; |
||
| 120 | } |
||
| 121 | |||
| 122 | $campaign_args = array( |
||
| 123 | 'utm_source' => 'wpadmin', |
||
| 124 | 'utm_campaign' => 'BulkDelete', |
||
| 125 | 'utm_medium' => 'header-notice', |
||
| 126 | 'utm_content' => strtolower( $this->addon_code ), |
||
| 127 | ); |
||
| 128 | $addon_url = bd_get_addon_url( $this->addon_name, $campaign_args ); |
||
| 129 | |||
| 130 | $license_code = BD_License::get_license_code( $this->addon_code ); |
||
| 131 | |||
| 132 | if ( false == $license_code ) { |
||
| 133 | $this->notice_msg = sprintf( __( '"%1$s" addon is installed but not activated. To activate the addon, please <a href="%2$s">enter your license key</a>. If you don\'t have a license key, then you can <a href="%3$s" target="_blank">purchase one</a>.', 'bulk-delete' ), $this->addon_name, esc_url( get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . Bulk_Delete::ADDON_PAGE_SLUG ), esc_url( $addon_url ) ); |
||
| 134 | } else { |
||
| 135 | if ( ! BD_License::has_valid_license( $this->addon_name, $this->addon_code ) ) { |
||
| 136 | $this->notice_msg = sprintf( __( 'The license for "%1$s" addon is either invalid or has expired. Please <a href="%2$s" target="_blank">renew the license</a> or <a href="%3$s">enter a new license key</a> to receive updates and support.', 'bulk-delete' ), $this->addon_name, esc_url( $addon_url ), esc_url( get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . Bulk_Delete::ADDON_PAGE_SLUG ) ); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | $this->hook_updater( $license_code ); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Start the updater. |
||
| 145 | * |
||
| 146 | * @since 5.0 |
||
| 147 | * @access private |
||
| 148 | * |
||
| 149 | * @param string $license_code License Code |
||
| 150 | */ |
||
| 151 | private function hook_updater( $license_code ) { |
||
| 152 | if ( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) { |
||
| 153 | require_once Bulk_Delete::$PLUGIN_DIR . '/include/libraries/EDD_SL_Plugin_Updater.php'; |
||
| 154 | } |
||
| 155 | |||
| 156 | $this->updater = new EDD_SL_Plugin_Updater( BD_EDD_API_Wrapper::STORE_URL, $this->plugin_file, array( |
||
| 157 | 'version' => $this->version, |
||
| 158 | 'license' => $license_code, |
||
| 159 | 'item_name' => $this->addon_name, |
||
| 160 | 'addon_code' => $this->addon_code, |
||
| 161 | 'author' => $this->author, |
||
| 162 | 'url' => home_url(), |
||
| 163 | ) |
||
| 164 | ); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Display notification at the top of all admin pages. |
||
| 169 | * |
||
| 170 | * @since 5.5 |
||
| 171 | */ |
||
| 172 | public function show_admin_notices() { |
||
| 173 | if ( '' != $this->notice_msg ) { |
||
| 174 | printf( '<div class="error"><p><strong>%s</strong></p></div>', $this->notice_msg ); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Display license information about addon in plugin list table. |
||
| 180 | * |
||
| 181 | * @since 5.5 |
||
| 182 | * |
||
| 183 | * @param string $plugin_file Path to the plugin file, relative to the plugins directory. |
||
| 184 | * @param array $plugin_data An array of plugin data. |
||
| 185 | * @param string $status Status of the plugin. |
||
| 186 | */ |
||
| 187 | public function plugin_row( $plugin_file, $plugin_data, $status ) { |
||
| 188 | if ( $plugin_file != $this->plugin_basename ) { |
||
| 189 | return; |
||
| 190 | } |
||
| 191 | |||
| 192 | $campaign_args = array( |
||
| 193 | 'utm_source' => 'wpadmin', |
||
| 194 | 'utm_campaign' => 'BulkDelete', |
||
| 195 | 'utm_medium' => 'plugin-page', |
||
| 196 | 'utm_content' => strtolower( $this->addon_code ), |
||
| 197 | ); |
||
| 198 | $addon_url = bd_get_addon_url( $this->addon_name, $campaign_args ); |
||
| 199 | |||
| 200 | $license_code = BD_License::get_license_code( $this->addon_code ); |
||
| 201 | if ( false == $license_code ) { |
||
| 202 | $plugin_row_msg = sprintf( __( 'Addon is not activated. To activate the addon, please <a href="%1$s">enter your license key</a>. If you don\'t have a license key, then you can <a href="%2$s" target="_blank">purchase one</a>.', 'bulk-delete' ), esc_url( get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . Bulk_Delete::ADDON_PAGE_SLUG ), esc_url( $addon_url ) ); |
||
| 203 | ?> |
||
| 204 | <tr class="plugin-update-tr"> |
||
| 205 | <td colspan="3" class="plugin-update"> |
||
| 206 | <div class="update-message"><span class="bd-licence-activate-notice"><?php echo $plugin_row_msg; ?></span></div> |
||
| 207 | </td> |
||
| 208 | </tr> |
||
| 209 | <?php |
||
| 210 | } else { |
||
| 211 | if ( ! BD_License::has_valid_license( $this->addon_name, $this->addon_code ) ) { |
||
| 212 | $plugin_row_msg = sprintf( __( 'The license for this addon is either invalid or has expired. Please <a href="%1$s" target="_blank">renew the license</a> or <a href="%2$s">enter a new license key</a> to receive updates and support.', 'bulk-delete' ), esc_url( $addon_url ), esc_url( get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . Bulk_Delete::ADDON_PAGE_SLUG ) ); |
||
| 213 | ?> |
||
| 214 | <tr class="plugin-update-tr"> |
||
| 215 | <td colspan="3" class="plugin-update"> |
||
| 216 | <div class="update-message"><span class="bd-licence-activate-notice"><?php echo $plugin_row_msg; ?></span></div> |
||
| 217 | </td> |
||
| 218 | </tr> |
||
| 219 | <?php |
||
| 220 | } |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Decide whether to display the license form or not. |
||
| 226 | * |
||
| 227 | * @since 5.0 |
||
| 228 | */ |
||
| 229 | public function display_license_form() { |
||
| 230 | if ( ! BD_License::has_valid_license( $this->addon_name, $this->addon_code ) ) { |
||
| 231 | $bd = BULK_DELETE(); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 232 | $bd->display_activate_license_form = true; |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Add the license field to license form. |
||
| 238 | * |
||
| 239 | * @since 5.0 |
||
| 240 | */ |
||
| 241 | public function add_license_field() { |
||
| 242 | if ( ! BD_License::has_valid_license( $this->addon_name, $this->addon_code ) ) { |
||
| 243 | add_settings_field( |
||
| 244 | $this->addon_code, // ID |
||
| 245 | '"' . $this->addon_name . '" ' . __( 'Addon License Key', 'bulk-delete' ), // Title |
||
| 246 | array( $this, 'print_license_key_field' ), // Callback |
||
| 247 | Bulk_Delete::ADDON_PAGE_SLUG, // Page |
||
| 248 | Bulk_Delete::SETTING_SECTION_ID // Section |
||
| 249 | ); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Print the license field. |
||
| 255 | * |
||
| 256 | * @since 5.0 |
||
| 257 | */ |
||
| 258 | public function print_license_key_field() { |
||
| 259 | if ( ! BD_License::has_valid_license( $this->addon_name, $this->addon_code ) ) { |
||
| 260 | printf( |
||
| 261 | '<input type="text" id="%s" name="%s[%s]" placeholder="%s" size="40">', |
||
| 262 | $this->addon_code, |
||
| 263 | Bulk_Delete::SETTING_OPTION_NAME, |
||
| 264 | $this->addon_code, |
||
| 265 | __( 'Enter license key', 'bulk-delete' ) |
||
| 266 | ); |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Parse the license key and activate it if needed. |
||
| 272 | * If the key is invalid, then don't save it in the setting option. |
||
| 273 | * |
||
| 274 | * @since 5.0 |
||
| 275 | * |
||
| 276 | * @param array $input |
||
| 277 | * |
||
| 278 | * @return array |
||
| 279 | */ |
||
| 280 | public function parse_license_input( $input ) { |
||
| 281 | if ( is_array( $input ) && key_exists( $this->addon_code, $input ) ) { |
||
| 282 | $license_code = trim( $input[ $this->addon_code ] ); |
||
| 283 | |||
| 284 | if ( ! empty( $license_code ) ) { |
||
| 285 | if ( ! BD_License::has_valid_license( $this->addon_name, $this->addon_code ) ) { |
||
| 286 | $activated = BD_License::activate_license( $this->addon_name, $this->addon_code, $license_code ); |
||
| 287 | if ( ! $activated ) { |
||
| 288 | unset( $input[ $this->addon_code ] ); |
||
| 289 | } |
||
| 290 | } |
||
| 291 | } else { |
||
| 292 | unset( $input[ $this->addon_code ] ); |
||
| 293 | } |
||
| 294 | } else { |
||
| 295 | if ( BD_License::has_valid_license( $this->addon_name, $this->addon_code ) ) { |
||
| 296 | $license_code = BD_License::get_license_code( $this->addon_code ); |
||
| 297 | $input[ $this->addon_code ] = $license_code; |
||
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | return $input; |
||
| 302 | } |
||
| 303 | } |
||
| 304 | ?> |
||
| 305 |