class-controller.php
243 lines
| 1 | <?php |
| 2 | /** |
| 3 | * BLC admin modal for legacy screens. Called from WPMUDEV_BLC\App\Admin_Pages\Local_Submenu\View. |
| 4 | * |
| 5 | * @link https://wordpress.org/plugins/broken-link-checker/ |
| 6 | * @since 2.1 |
| 7 | * |
| 8 | * @author WPMUDEV (https://wpmudev.com) |
| 9 | * @package WPMUDEV_BLC\App\Admin_Notices\Legacy |
| 10 | * |
| 11 | * @copyright (c) 2022, Incsub (http://incsub.com) |
| 12 | */ |
| 13 | |
| 14 | namespace WPMUDEV_BLC\App\Admin_Modals\Local; |
| 15 | |
| 16 | // Abort if called directly. |
| 17 | defined( 'WPINC' ) || die; |
| 18 | |
| 19 | use WPMUDEV_BLC\App\Options\Settings\Model as Settings; |
| 20 | |
| 21 | use WPMUDEV_BLC\Core\Utils\Utilities; |
| 22 | use WPMUDEV_BLC\Core\Utils\Abstracts\Base; |
| 23 | use WPMUDEV_BLC\Core\Traits\Enqueue; |
| 24 | use WPMUDEV_BLC\Core\Traits\Dashboard_API; |
| 25 | |
| 26 | /** |
| 27 | * Class Controller |
| 28 | * |
| 29 | * @package WPMUDEV_BLC\App\Admin_Modals\Local |
| 30 | */ |
| 31 | class Controller extends Base { |
| 32 | /** |
| 33 | * Use the Enqueue and Dashboard_API Traits. |
| 34 | * |
| 35 | * @since 2.1 |
| 36 | */ |
| 37 | use Enqueue, Dashboard_API; |
| 38 | |
| 39 | /** |
| 40 | * The unique id that can be used by react. |
| 41 | * |
| 42 | * @since 2.1 |
| 43 | * @var int $unique_id |
| 44 | * |
| 45 | */ |
| 46 | public static $unique_id = null; |
| 47 | |
| 48 | /** |
| 49 | * The admin pages the notice will be visible at. |
| 50 | * |
| 51 | * @since 2.1 |
| 52 | * @var array $admin_pages |
| 53 | * |
| 54 | */ |
| 55 | protected $admin_pages = array(); |
| 56 | |
| 57 | /** |
| 58 | * Init function. |
| 59 | * |
| 60 | * @since 2.1 |
| 61 | */ |
| 62 | public function init() { |
| 63 | add_action( 'current_screen', array( $this, 'boot' ), 11 ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Boots modal parts. |
| 68 | */ |
| 69 | public function boot() { |
| 70 | if ( $this->can_boot() ) { |
| 71 | add_filter( 'admin_body_class', array( $this, 'admin_body_classes' ), 999 ); |
| 72 | //add_action( 'admin_footer', array( $this, 'output' ) ); |
| 73 | |
| 74 | self::$unique_id = Utilities::get_unique_id(); |
| 75 | |
| 76 | View::instance()::$unique_id = self::$unique_id; |
| 77 | |
| 78 | $this->prepare_props(); |
| 79 | $this->prepare_scripts(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Checks if admin page actions/scripts should load in current screen. |
| 85 | * |
| 86 | * @since 2.1 |
| 87 | * @return boolean Checks if admin page actions/scripts should load. Useful for enqueuing scripts. |
| 88 | */ |
| 89 | public function can_boot() { |
| 90 | /* |
| 91 | * Show modal only when Local BLC is active. |
| 92 | */ |
| 93 | //if ( boolval( self::site_connected() ) || empty( boolval( Settings::instance()->get( 'use_legacy_blc_version' ) ) ) ) { |
| 94 | if ( ! current_user_can( 'manage_options' ) || empty( Settings::instance()->get( 'use_legacy_blc_version' ) ) ) { |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | We use the Utilities::$value_provider array variable. |
| 100 | This variable can hold values that can be used from different classes which should help avoid checking |
| 101 | same conditions multiple times. |
| 102 | In this case we are using `boot_admin_local_page` key which is also used in |
| 103 | WPMUDEV_BLC\App\Admin_Notices\Legacy\Controller class. |
| 104 | */ |
| 105 | if ( ! isset( Utilities::$value_provider['boot_admin_local_page'] ) ) { |
| 106 | Utilities::$value_provider['boot_admin_local_page'] = Utilities::is_admin_screen( $this->admin_pages() ); |
| 107 | } |
| 108 | |
| 109 | $legacy_option = Settings::instance()->get( 'use_legacy_blc_version' ); |
| 110 | |
| 111 | return Utilities::$value_provider['boot_admin_local_page'] && ! empty( $legacy_option ); |
| 112 | } |
| 113 | |
| 114 | protected function admin_pages() { |
| 115 | return array( |
| 116 | 'blc_local', |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Prepares the properties of the Admin Page. |
| 122 | * |
| 123 | * @since 2.1 |
| 124 | * @return void |
| 125 | */ |
| 126 | public function prepare_props() { |
| 127 | |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Register scripts for the admin page. |
| 132 | * |
| 133 | * @since 2.1 |
| 134 | * @return array Register scripts for the admin page. |
| 135 | */ |
| 136 | public function set_admin_scripts() { |
| 137 | $script_data = include WPMUDEV_BLC_DIR . 'assets/dist/local.asset.php'; |
| 138 | $dependencies = $script_data['dependencies'] ?? array( |
| 139 | 'react', |
| 140 | 'wp-element', |
| 141 | 'wp-i18n', |
| 142 | 'wp-is-shallow-equal', |
| 143 | 'wp-polyfill', |
| 144 | ); |
| 145 | $version = ! empty( $script_data['version'] ) ? WPMUDEV_BLC_SCIPTS_VERSION . '-' . $script_data['version'] : WPMUDEV_BLC_SCIPTS_VERSION; |
| 146 | $signup_url = add_query_arg( |
| 147 | array( |
| 148 | 'utm_source' => 'blc', |
| 149 | 'utm_medium' => 'plugin', |
| 150 | 'utm_campaign' => 'blc_plugin_onboarding', |
| 151 | ), |
| 152 | Utilities::signup_url() |
| 153 | ); |
| 154 | |
| 155 | return array( |
| 156 | 'blc_local_modal' => array( |
| 157 | 'src' => $this->scripts_dir . 'local-topnav.js', |
| 158 | 'deps' => $dependencies, |
| 159 | 'ver' => $version, |
| 160 | 'in_footer' => true, |
| 161 | 'localize' => array( |
| 162 | 'blc_local_modal' => array( |
| 163 | 'data' => array( |
| 164 | 'rest_url' => esc_url_raw( rest_url() ), |
| 165 | 'settings_endpoint' => '/wpmudev_blc/v1/settings', |
| 166 | 'unique_id' => self::$unique_id, |
| 167 | 'nonce' => wp_create_nonce( 'wp_rest' ), |
| 168 | 'legacy_active' => boolval( Settings::instance()->get( 'use_legacy_blc_version' ) ), |
| 169 | 'local_blc_url' => admin_url( 'admin.php?page=blc_local' ), |
| 170 | 'cloud_blc_url' => admin_url( 'admin.php?page=blc_dash' ), |
| 171 | 'assets_url' => WPMUDEV_BLC_ASSETS_URL, |
| 172 | 'site_connected' => (bool) self::site_connected(), |
| 173 | 'expired_membership' => boolval( Utilities::membership_expired() ), |
| 174 | 'hub_signup_url' => $signup_url, |
| 175 | 'dash_installed' => boolval( Utilities::dash_plugin_installed() ), |
| 176 | 'dash_active' => boolval( Utilities::dash_plugin_active() ), |
| 177 | ), |
| 178 | 'labels' => array( |
| 179 | 'error_messages' => array( |
| 180 | 'general' => __( 'Something went wrong here.', 'broken-link-checker' ), |
| 181 | ), |
| 182 | ), |
| 183 | ), |
| 184 | ), |
| 185 | 'translate' => true, |
| 186 | ), |
| 187 | // END OF blc_activation_popup. |
| 188 | ); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Register css files for admin page. |
| 193 | * |
| 194 | * @return array |
| 195 | */ |
| 196 | public function set_admin_styles() { |
| 197 | return array( |
| 198 | 'blc_sui' => array( |
| 199 | 'src' => $this->styles_dir . 'shared-ui-' . BLC_SHARED_UI_VERSION_NUMBER . '.min.css', |
| 200 | 'ver' => $this->scripts_version(), |
| 201 | ), |
| 202 | 'blc_legacy_modal' => array( |
| 203 | //'src' => $this->styles_dir . 'local-modal.min.css', |
| 204 | 'src' => $this->scripts_dir . 'style-local-topnav.css', |
| 205 | 'ver' => $this->scripts_version(), |
| 206 | ), |
| 207 | ); |
| 208 | } |
| 209 | |
| 210 | protected function scripts_version() { |
| 211 | static $scripts_version = null; |
| 212 | |
| 213 | if ( is_null( $scripts_version ) ) { |
| 214 | $script_data = include WPMUDEV_BLC_DIR . 'assets/dist/local-topnav.asset.php'; |
| 215 | $scripts_version = ! empty( $script_data['version'] ) ? WPMUDEV_BLC_SCIPTS_VERSION . '-' . $script_data['version'] : WPMUDEV_BLC_SCIPTS_VERSION; |
| 216 | } |
| 217 | |
| 218 | return $scripts_version; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Admin Menu Callback. |
| 223 | * |
| 224 | * @since 2.1 |
| 225 | * @return void The callback function of the Admin Menu Page. |
| 226 | */ |
| 227 | public function output() { |
| 228 | View::instance()->render( array( 'unique_id' => self::$unique_id ) ); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Adds the blc legacy class to body classes for styling purposes. |
| 233 | * |
| 234 | * @param string $classes The body classes. |
| 235 | * |
| 236 | * @since 2.1 |
| 237 | * @return string Returns the body classes. |
| 238 | */ |
| 239 | public function admin_body_classes( string $classes = '' ) { |
| 240 | return $classes . ' blc-show-local-modal'; |
| 241 | } |
| 242 | } |
| 243 |