| @@ -28,9 +28,9 @@ | ||
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * Snippets custom post type instance. |
| 31 | 31 | * |
| 32 | - * @var WINP_SnippetsType | |
| 32 | + * @var WINP_SnippetsType|null | |
| 33 | 33 | */ |
| 34 | 34 | private $snippets_type; |
| 35 | 35 | |
| 36 | 36 | /** |
| @@ -55,9 +55,9 @@ | ||
| 55 | 55 | WINP_SnippetLibrary::get_instance(); |
| 56 | 56 | |
| 57 | 57 | $this->load_global(); |
| 58 | 58 | |
| 59 | - if ( is_admin() ) { | |
| 59 | + if ( is_admin() || WINP_Helper::doing_rest_api() ) { | |
| 60 | 60 | |
| 61 | 61 | if ( WINP_Helper::doing_ajax() ) { |
| 62 | 62 | require WINP_PLUGIN_DIR . '/admin/ajax/ajax.php'; |
| 63 | 63 | require WINP_PLUGIN_DIR . '/admin/ajax/snippet-library.php'; |
| @@ -70,17 +70,18 @@ | ||
| 70 | 70 | function () { |
| 71 | 71 | if ( WINP_Plugin::app()->premium->is_active() ) { |
| 72 | 72 | update_option( WINP_PLUGIN_NAMESPACE . '_logger_flag', 'yes' ); |
| 73 | 73 | } |
| 74 | - } | |
| 74 | + } | |
| 75 | 75 | ); |
| 76 | 76 | |
| 77 | 77 | add_filter( WINP_PLUGIN_NAMESPACE . '_logger_data', [ $this, 'get_logger_data' ] ); |
| 78 | + add_filter( 'themeisle_sdk_blackfriday_data', [ $this, 'add_black_friday_data' ] ); | |
| 78 | 79 | } |
| 79 | 80 | |
| 80 | 81 | /** |
| 81 | 82 | * Get plugin instance |
| 82 | - * | |
| 83 | + * | |
| 83 | 84 | * @return WINP_Plugin |
| 84 | 85 | */ |
| 85 | 86 | public static function app() { |
| 86 | 87 | return self::$app; |
| @@ -87,9 +88,9 @@ | ||
| 87 | 88 | } |
| 88 | 89 | |
| 89 | 90 | /** |
| 90 | 91 | * Survey data. |
| 91 | - * | |
| 92 | + * | |
| 92 | 93 | * @return array<string, mixed> |
| 93 | 94 | */ |
| 94 | 95 | public function get_survey_data() { |
| 95 | 96 | $install_time = get_option( WINP_PLUGIN_NAMESPACE . '_install', time() ); |
| @@ -163,15 +164,15 @@ | ||
| 163 | 164 | * @return void |
| 164 | 165 | */ |
| 165 | 166 | public function activation_hook() { |
| 166 | 167 | // Add custom capabilities to administrator role. |
| 167 | - $this->snippets_type->add_capabilities(); | |
| 168 | - | |
| 168 | + $this->get_snippets_type()->add_capabilities(); | |
| 169 | + | |
| 169 | 170 | // Create demo snippets with examples of use. |
| 170 | 171 | if ( ! get_option( 'wbcr_inp_demo_snippets_created' ) ) { |
| 171 | 172 | WINP_Helper::create_demo_snippets(); |
| 172 | 173 | } |
| 173 | - | |
| 174 | + | |
| 174 | 175 | WINP_Helper::flush_page_cache(); |
| 175 | 176 | } |
| 176 | 177 | |
| 177 | 178 | /** |
| @@ -181,12 +182,31 @@ | ||
| 181 | 182 | * @return void |
| 182 | 183 | */ |
| 183 | 184 | public function deactivation_hook() { |
| 184 | 185 | // Remove custom capabilities from administrator role. |
| 185 | - $this->snippets_type->remove_capabilities(); | |
| 186 | + $this->get_snippets_type()->remove_capabilities(); | |
| 186 | 187 | } |
| 187 | 188 | |
| 188 | 189 | /** |
| 190 | + * Get the snippets post type handler, loading it on demand. | |
| 191 | + * | |
| 192 | + * The (de)activation hooks can run outside of admin/REST requests | |
| 193 | + * (e.g. `wp plugin activate` via WP-CLI), where load_backend() and | |
| 194 | + * therefore register_types() never ran. | |
| 195 | + * | |
| 196 | + * @return WINP_SnippetsType | |
| 197 | + */ | |
| 198 | + private function get_snippets_type() { | |
| 199 | + if ( is_null( $this->snippets_type ) ) { | |
| 200 | + require_once WINP_PLUGIN_DIR . '/admin/includes/class.snippets.viewtable.php'; | |
| 201 | + require_once WINP_PLUGIN_DIR . '/admin/types/snippets-post-types.php'; | |
| 202 | + $this->snippets_type = new WINP_SnippetsType(); | |
| 203 | + } | |
| 204 | + | |
| 205 | + return $this->snippets_type; | |
| 206 | + } | |
| 207 | + | |
| 208 | + /** | |
| 189 | 209 | * Register custom post types and taxonomies. |
| 190 | 210 | * |
| 191 | 211 | * @throws \Exception Exception. |
| 192 | 212 | * @since 2.2.0 |
| @@ -291,9 +311,9 @@ | ||
| 291 | 311 | } |
| 292 | 312 | |
| 293 | 313 | /** |
| 294 | 314 | * Logger data. |
| 295 | - * | |
| 315 | + * | |
| 296 | 316 | * @return array<string, mixed> |
| 297 | 317 | */ |
| 298 | 318 | public function get_logger_data() { |
| 299 | 319 | $settings = WINP_Settings::get_instance()->get_settings(); |
| @@ -314,9 +334,9 @@ | ||
| 314 | 334 | |
| 315 | 335 | // Count snippets by type. |
| 316 | 336 | $snippet_types = [ 'php', 'css', 'js', 'text', 'html', 'advert', 'universal' ]; |
| 317 | 337 | $types_count = []; |
| 318 | - | |
| 338 | + | |
| 319 | 339 | foreach ( $snippet_types as $type ) { |
| 320 | 340 | $count = get_posts( |
| 321 | 341 | [ |
| 322 | 342 | 'post_type' => WINP_SNIPPETS_POST_TYPE, |
| @@ -326,9 +346,9 @@ | ||
| 326 | 346 | 'posts_per_page' => -1, |
| 327 | 347 | 'fields' => 'ids', |
| 328 | 348 | ] |
| 329 | 349 | ); |
| 330 | - | |
| 350 | + | |
| 331 | 351 | if ( ! empty( $count ) ) { |
| 332 | 352 | $types_count[ $type ] = count( $count ); |
| 333 | 353 | } |
| 334 | 354 | } |
| @@ -339,7 +359,69 @@ | ||
| 339 | 359 | 'total_snippets' => $total_snippets, |
| 340 | 360 | 'types' => $types_count, |
| 341 | 361 | ], |
| 342 | 362 | ]; |
| 363 | + } | |
| 364 | + | |
| 365 | + /** | |
| 366 | + * Set the black friday data. | |
| 367 | + * | |
| 368 | + * @param array<string, mixed> $configs The configuration array for the loaded products. | |
| 369 | + * | |
| 370 | + * @return array<string, mixed> The configurations. | |
| 371 | + */ | |
| 372 | + public function add_black_friday_data( $configs ) { | |
| 373 | + $config = $configs['default']; | |
| 374 | + | |
| 375 | + $message = __( 'Conditional logic, revision history, import/export. Manage your custom code properly. Exclusively for existing Woody users.', 'insert-php' ); | |
| 376 | + $cta_label = __( 'Get Woody Pro', 'insert-php' ); | |
| 377 | + | |
| 378 | + $plan = apply_filters( 'product_woody_license_plan', 0 ); | |
| 379 | + $status = apply_filters( 'product_woody_license_status', 'invalid' ); | |
| 380 | + $license = apply_filters( 'product_woody_license_key', false ); | |
| 381 | + $pro_product_slug = defined( 'WASP_PLUGIN_FILE' ) ? basename( dirname( WASP_PLUGIN_FILE ) ) : ''; | |
| 382 | + | |
| 383 | + $is_pro = 'valid' === $status; | |
| 384 | + $is_expired = 'expired' === $status || 'active-expired' === $status; | |
| 385 | + | |
| 386 | + if ( $is_pro ) { | |
| 387 | + // translators: %s is the discount percentage. | |
| 388 | + $config['plugin_meta_message'] = sprintf( __( 'Black Friday Sale - up to %s off', 'insert-php' ), '30%' ); | |
| 389 | + // translators: %1$s - discount, %2$s - discount. | |
| 390 | + $message = sprintf( __( 'Upgrade your Woody Pro plan: %1$s off this week. Already on the plan you need? Renew early and save up to %2$s.', 'insert-php' ), '30%', '20%' ); | |
| 391 | + $cta_label = __( 'See your options', 'insert-php' ); | |
| 392 | + } elseif ( $is_expired ) { | |
| 393 | + // translators: %s is the discount percentage. | |
| 394 | + $config['plugin_meta_message'] = sprintf( __( 'Black Friday Sale - %s off', 'insert-php' ), '50%' ); | |
| 395 | + $message = __( 'Your Woody Pro features are still here, just locked. Renew at a reduced rate this week.', 'insert-php' ); | |
| 396 | + $cta_label = __( 'Reactivate now', 'insert-php' ); | |
| 397 | + } else { | |
| 398 | + // translators: %s - discount. | |
| 399 | + $config['title'] = sprintf( __( 'Woody Pro: %s off this week', 'insert-php' ), '60%' ); | |
| 400 | + // translators: %s is the discount percentage. | |
| 401 | + $config['plugin_meta_message'] = sprintf( __( 'Black Friday Sale - %s off', 'insert-php' ), '60%' ); | |
| 402 | + } | |
| 403 | + | |
| 404 | + $url_params = [ | |
| 405 | + 'utm_term' => $is_pro ? 'plan-' . $plan : 'free', | |
| 406 | + 'lkey' => ! empty( $license ) ? $license : false, | |
| 407 | + 'expired' => $is_expired ? '1' : false, | |
| 408 | + ]; | |
| 409 | + | |
| 410 | + if ( ( $is_pro || $is_expired ) && ! empty( $pro_product_slug ) ) { | |
| 411 | + $config['plugin_meta_targets'] = [ $pro_product_slug ]; | |
| 412 | + } | |
| 413 | + | |
| 414 | + $config['cta_label'] = $cta_label; | |
| 415 | + $config['message'] = $message; | |
| 416 | + | |
| 417 | + $config['sale_url'] = add_query_arg( | |
| 418 | + $url_params, | |
| 419 | + tsdk_translate_link( tsdk_utmify( 'https://themeisle.link/woody-bf', 'bfcm', 'woody' ) ) | |
| 420 | + ); | |
| 421 | + | |
| 422 | + $configs[ WINP_PLUGIN_SLUG ] = $config; | |
| 423 | + | |
| 424 | + return $configs; | |
| 343 | 425 | } |
| 344 | 426 | } |
| 345 | 427 | } |