|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Cimo Notice |
| 4 | + */ |
| 5 | + |
| 6 | +// Exit if accessed directly. |
| 7 | +if ( ! defined( 'ABSPATH' ) ) { |
| 8 | + exit; |
| 9 | +} |
| 10 | + |
| 11 | +if ( ! class_exists( 'Stackable_Cimo_Notice' ) ) { |
| 12 | + class Stackable_Cimo_Notice { |
| 13 | + |
| 14 | + private static $CIMO_SLUG = 'cimo-image-optimizer'; |
| 15 | + private static $CIMO_FULL_SLUG = 'cimo-image-optimizer/cimo.php'; |
| 16 | + |
| 17 | + function __construct() { |
| 18 | + if ( is_admin() ) { |
| 19 | + add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_script' ), 1 ); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + public static function is_plugin_installed() { |
| 24 | + if ( ! function_exists( 'get_plugins' ) ) { |
| 25 | + include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 26 | + } |
| 27 | + |
| 28 | + $all_plugins = get_plugins(); |
| 29 | + if ( isset( $all_plugins[ self::$CIMO_FULL_SLUG ] ) ) { |
| 30 | + return true; |
| 31 | + } |
| 32 | + |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + public static function is_plugin_activated() { |
| 37 | + if ( ! function_exists( 'is_plugin_active' ) ) { |
| 38 | + include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 39 | + } |
| 40 | + |
| 41 | + if ( is_plugin_active( self::$CIMO_FULL_SLUG ) && defined( CIMO_FILE ) ) { |
| 42 | + return true; |
| 43 | + } |
| 44 | + |
| 45 | + return false; |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + public function enqueue_script() { |
| 50 | + $cimo_state = 'activated'; |
| 51 | + $cimo_action = ''; |
| 52 | + |
| 53 | + if ( ! self::is_plugin_installed() ) { |
| 54 | + $cimo_state = 'not_installed'; |
| 55 | + $cimo_action = wp_nonce_url( |
| 56 | + add_query_arg( |
| 57 | + [ |
| 58 | + 'action' => 'install-plugin', |
| 59 | + 'plugin' => self::$CIMO_SLUG, |
| 60 | + ], |
| 61 | + admin_url( 'update.php' ) |
| 62 | + ), |
| 63 | + 'install-plugin_' . self::$CIMO_SLUG |
| 64 | + ); |
| 65 | + } else if ( ! self::is_plugin_activated() ) { |
| 66 | + $cimo_state = 'installed'; |
| 67 | + $cimo_action = wp_nonce_url( |
| 68 | + admin_url( 'plugins.php?action=activate&plugin=' . self::$CIMO_FULL_SLUG ), |
| 69 | + 'activate-plugin_' . self::$CIMO_FULL_SLUG |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + if ( $cimo_state === 'activated' && ! $cimo_action ) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + $content = sprintf('%s <a class="stk-cimo-notice" href="%s" target="_blank">%s</a>.', |
| 78 | + __( 'Optimize your images with Cimo Image Optimizer.', STACKABLE_I18N ), |
| 79 | + $cimo_action, |
| 80 | + ( $cimo_state === 'installed' ? __( 'Activate', STACKABLE_I18N ) : __( 'Install', STACKABLE_I18N ) ) |
| 81 | + . ' ' . __( 'Cimo Image Optimizer', STACKABLE_I18N ) |
| 82 | + ); |
| 83 | + |
| 84 | + $data = array( |
| 85 | + 'state' => $cimo_state, |
| 86 | + 'content' => $content, |
| 87 | + 'action' => wp_json_encode( [ 'action' => $cimo_action ] ) |
| 88 | + ); |
| 89 | + |
| 90 | + wp_enqueue_script( |
| 91 | + 'stk-cimo-notice', |
| 92 | + plugins_url( 'dist/stk_cimo_notice.js', STACKABLE_FILE ), |
| 93 | + array(), |
| 94 | + STACKABLE_VERSION, |
| 95 | + true |
| 96 | + ); |
| 97 | + |
| 98 | + add_filter( 'stackable_localize_script', function ( $args ) use( $data ) { |
| 99 | + return $this->add_localize_script( $args, $data ); |
| 100 | + } ); |
| 101 | + |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + public function add_localize_script( $args, $data ) { |
| 106 | + $args[ 'cimo-notice' ] = $data; |
| 107 | + return $args; |
| 108 | + } |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | + new Stackable_Cimo_Notice(); |
| 113 | +} |
0 commit comments