robin-image-optimizer
/
libs
/
factory
/
core
/
includes
/
components
/
class-install-component-button.php
class-install-component-button.php
432 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WBCR\Factory_600\Components; |
| 4 | |
| 5 | /** |
| 6 | * This file groups the settings for quick setup |
| 7 | * |
| 8 | * @version 1.0 |
| 9 | */ |
| 10 | |
| 11 | // Exit if accessed directly |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; |
| 14 | } |
| 15 | |
| 16 | class Install_Button { |
| 17 | |
| 18 | public $plugin; |
| 19 | protected $type; |
| 20 | protected $plugin_slug; |
| 21 | |
| 22 | protected $classes = [ |
| 23 | 'button', |
| 24 | 'wfactory-600-process-button', |
| 25 | ]; |
| 26 | protected $data = []; |
| 27 | protected $base_path; |
| 28 | |
| 29 | protected $action; |
| 30 | |
| 31 | protected $url; |
| 32 | |
| 33 | /** |
| 34 | * @param string $group_name |
| 35 | * |
| 36 | * @throws \Exception |
| 37 | * @since 4.3.3 |
| 38 | */ |
| 39 | public function __construct( \Wbcr_Factory600_Plugin $plugin, $type, $plugin_slug ) { |
| 40 | if ( empty( $type ) || ! is_string( $plugin_slug ) ) { |
| 41 | throw new \Exception( 'Empty type or plugin_slug attribute.' ); |
| 42 | } |
| 43 | |
| 44 | $this->plugin = $plugin; |
| 45 | $this->type = $type; |
| 46 | $this->plugin_slug = $plugin_slug; |
| 47 | |
| 48 | if ( $this->type == 'WordPress' || $this->type == 'creativemotion' ) { |
| 49 | if ( strpos( rtrim( trim( $this->plugin_slug ) ), '/' ) !== false ) { |
| 50 | $this->base_path = $this->plugin_slug; |
| 51 | $base_path_parts = explode( '/', $this->base_path ); |
| 52 | if ( sizeof( $base_path_parts ) === 2 ) { |
| 53 | $this->plugin_slug = $base_path_parts[0]; |
| 54 | } |
| 55 | } else { |
| 56 | $this->base_path = $this->get_plugin_base_path_by_slug( $this->plugin_slug ); |
| 57 | } |
| 58 | |
| 59 | $this->build_wordpress(); |
| 60 | } elseif ( $this->type == 'internal' ) { |
| 61 | $this->build_internal(); |
| 62 | } else { |
| 63 | throw new \Exception( 'Invalid button type.' ); |
| 64 | } |
| 65 | |
| 66 | // Set default data |
| 67 | $this->add_data( 'storage', $this->type ); |
| 68 | $this->add_data( 'i18n', \WBCR\Factory_Templates_759\Helpers::getEscapeJson( $this->get_i18n() ) ); |
| 69 | $this->add_data( 'wpnonce', wp_create_nonce( 'updates' ) ); |
| 70 | } |
| 71 | |
| 72 | // Удалить, через несколько версий! |
| 73 | // Добавлено в 4.3.3 |
| 74 | // -------------------------------------------------------- |
| 75 | |
| 76 | /** |
| 77 | * todo: для совместимости со старыми плагинами |
| 78 | * |
| 79 | * @return string |
| 80 | * @throws \Exception |
| 81 | * @since 4.3.3 |
| 82 | */ |
| 83 | public function getLink() { |
| 84 | return $this->get_link(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * todo: для совместимости со старыми плагинами |
| 89 | * |
| 90 | * @return string |
| 91 | * @since 4.3.3 |
| 92 | */ |
| 93 | public function getButton() { |
| 94 | return $this->get_button(); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Print an install a link |
| 99 | * todo: для совместимости со старыми плагинами |
| 100 | * |
| 101 | * @throws \Exception |
| 102 | * @since 4.3.3 |
| 103 | */ |
| 104 | public function renderLink() { |
| 105 | echo esc_html( $this->get_link() ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Print an install button |
| 110 | * todo: для совместимости со старыми плагинами |
| 111 | * |
| 112 | * @throws \Exception |
| 113 | * @since 4.3.3 |
| 114 | */ |
| 115 | public function renderButton() { |
| 116 | $this->render_button(); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * todo: для совместимости со старыми плагинами |
| 121 | * |
| 122 | * @param $class |
| 123 | * |
| 124 | * @throws \Exception |
| 125 | * @since 4.3.3 |
| 126 | */ |
| 127 | public function addClass( $class ) { |
| 128 | $this->add_class( $class ); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * todo: для совместимости со старыми плагинами |
| 133 | * |
| 134 | * @param $class |
| 135 | * |
| 136 | * @return bool |
| 137 | * @throws \Exception |
| 138 | * @since 4.3.3 |
| 139 | */ |
| 140 | public function removeClass( $class ) { |
| 141 | return $this->remove_class( $class ); |
| 142 | } |
| 143 | |
| 144 | // -------------------------------------------------------- |
| 145 | |
| 146 | |
| 147 | /** |
| 148 | * @return bool |
| 149 | * @since 4.3.3 |
| 150 | */ |
| 151 | public function is_plugin_activate() { |
| 152 | if ( ( $this->type == 'WordPress' || $this->type == 'creativemotion' ) && $this->is_plugin_install() ) { |
| 153 | require_once ABSPATH . '/wp-admin/includes/plugin.php'; |
| 154 | |
| 155 | return is_plugin_active( $this->base_path ); |
| 156 | } elseif ( $this->type == 'internal' ) { |
| 157 | $preinsatall_components = $this->plugin->getPopulateOption( 'deactive_preinstall_components', [] ); |
| 158 | |
| 159 | return ! in_array( $this->plugin_slug, $preinsatall_components ); |
| 160 | } |
| 161 | |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * @return bool |
| 167 | * @since 4.3.3 |
| 168 | */ |
| 169 | public function is_plugin_install() { |
| 170 | if ( $this->type == 'WordPress' || $this->type == 'creativemotion' ) { |
| 171 | if ( empty( $this->base_path ) ) { |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | // Check if the function get_plugins() is registered. It is necessary for the front-end |
| 176 | // usually get_plugins() only works in the admin panel. |
| 177 | if ( ! function_exists( 'get_plugins' ) ) { |
| 178 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 179 | } |
| 180 | |
| 181 | $plugins = get_plugins(); |
| 182 | |
| 183 | if ( isset( $plugins[ $this->base_path ] ) ) { |
| 184 | return true; |
| 185 | } |
| 186 | } elseif ( $this->type == 'internal' ) { |
| 187 | return true; |
| 188 | } |
| 189 | |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * @param $class |
| 195 | * |
| 196 | * @throws \Exception |
| 197 | * @since 4.3.3 |
| 198 | */ |
| 199 | public function add_class( $class ) { |
| 200 | if ( ! is_string( $class ) ) { |
| 201 | throw new \Exception( 'Attribute class must be a string.' ); |
| 202 | } |
| 203 | $this->classes[] = $class; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * @param $class |
| 208 | * |
| 209 | * @return bool |
| 210 | * @throws \Exception |
| 211 | * @since 4.3.3 |
| 212 | */ |
| 213 | public function remove_class( $class ) { |
| 214 | if ( ! is_string( $class ) ) { |
| 215 | throw new \Exception( 'Attribute class must be a string.' ); |
| 216 | } |
| 217 | $key = array_search( $class, $this->classes ); |
| 218 | if ( isset( $this->classes[ $key ] ) ) { |
| 219 | unset( $this->classes[ $key ] ); |
| 220 | |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * @param $name |
| 229 | * @param $value |
| 230 | * |
| 231 | * @throws \Exception |
| 232 | * @since 4.3.3 |
| 233 | */ |
| 234 | public function add_data( $name, $value ) { |
| 235 | if ( ! is_string( $name ) || ! is_string( $value ) ) { |
| 236 | throw new \Exception( 'Attributes name and value must be a string.' ); |
| 237 | } |
| 238 | |
| 239 | $this->data[ $name ] = $value; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * @param $name |
| 244 | * |
| 245 | * @return bool |
| 246 | * @throws \Exception |
| 247 | * @since 4.3.3 |
| 248 | */ |
| 249 | public function remove_data( $name ) { |
| 250 | if ( ! is_string( $name ) ) { |
| 251 | throw new \Exception( 'Attribute name must be a string.' ); |
| 252 | } |
| 253 | |
| 254 | if ( isset( $this->data[ $name ] ) ) { |
| 255 | unset( $this->data[ $name ] ); |
| 256 | |
| 257 | return true; |
| 258 | } |
| 259 | |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Print an install button |
| 265 | * |
| 266 | * @throws \Exception |
| 267 | * @since 4.3.3 |
| 268 | */ |
| 269 | public function render_button() { |
| 270 | echo $this->get_button(); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * @return string |
| 275 | * @since 4.3.3 |
| 276 | */ |
| 277 | public function get_button() { |
| 278 | $i18n = $this->get_i18n(); |
| 279 | |
| 280 | $button = '<a href="#" class="' . implode( ' ', $this->get_classes() ) . '" ' . implode( ' ', $this->get_data() ) . '>' . $i18n[ $this->action ] . '</a>'; |
| 281 | |
| 282 | return $button; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * @return string |
| 287 | * @throws \Exception |
| 288 | * @since 4.3.3 |
| 289 | */ |
| 290 | public function get_link() { |
| 291 | $this->remove_class( 'button' ); |
| 292 | $this->remove_class( 'button-default' ); |
| 293 | $this->remove_class( 'button-primary' ); |
| 294 | |
| 295 | // $this->add_class('link'); |
| 296 | $this->add_class( 'button-link' ); |
| 297 | |
| 298 | return $this->get_button(); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Print an install a link |
| 303 | * |
| 304 | * @throws \Exception |
| 305 | * @since 4.3.3 |
| 306 | */ |
| 307 | public function render_link() { |
| 308 | echo $this->get_link(); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * @return array |
| 313 | * @since 4.3.3 |
| 314 | */ |
| 315 | protected function get_data() { |
| 316 | $data_to_print = []; |
| 317 | |
| 318 | foreach ( (array) $this->data as $key => $value ) { |
| 319 | $data_to_print[ $key ] = 'data-' . esc_attr( $key ) . '="' . esc_attr( $value ) . '"'; |
| 320 | } |
| 321 | |
| 322 | return $data_to_print; |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * @return array |
| 327 | * @since 4.3.3 |
| 328 | */ |
| 329 | protected function get_classes() { |
| 330 | return array_map( 'esc_attr', $this->classes ); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * @throws \Exception |
| 335 | * @since 4.3.3 |
| 336 | */ |
| 337 | protected function build_wordpress() { |
| 338 | if ( ( 'WordPress' === $this->type || 'creativemotion' === $this->type ) && ! empty( $this->base_path ) ) { |
| 339 | |
| 340 | $this->action = 'install'; |
| 341 | |
| 342 | if ( $this->is_plugin_install() ) { |
| 343 | $this->action = 'deactivate'; |
| 344 | if ( ! $this->is_plugin_activate() ) { |
| 345 | $this->action = 'activate'; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | $this->add_data( 'plugin-action', $this->action ); |
| 350 | $this->add_data( 'slug', $this->plugin_slug ); |
| 351 | $this->add_data( 'plugin', $this->base_path ); |
| 352 | |
| 353 | if ( $this->action == 'activate' ) { |
| 354 | $this->add_class( 'button-primary' ); |
| 355 | } else { |
| 356 | $this->add_class( 'button-default' ); |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Configurate button of internal components |
| 363 | * |
| 364 | * @throws \Exception |
| 365 | * @since 4.3.3 |
| 366 | */ |
| 367 | protected function build_internal() { |
| 368 | if ( $this->type != 'internal' ) { |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | $this->action = 'activate'; |
| 373 | |
| 374 | if ( $this->is_plugin_activate() ) { |
| 375 | $this->action = 'deactivate'; |
| 376 | } |
| 377 | |
| 378 | $this->add_data( 'plugin-action', $this->action ); |
| 379 | $this->add_data( 'plugin', $this->plugin_slug ); |
| 380 | |
| 381 | if ( $this->action == 'activate' ) { |
| 382 | $this->add_class( 'button-primary' ); |
| 383 | } else { |
| 384 | $this->add_class( 'button-default' ); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Internalization for action buttons |
| 390 | * |
| 391 | * @return array |
| 392 | * @since 4.3.3 |
| 393 | */ |
| 394 | protected function get_i18n() { |
| 395 | return [ |
| 396 | 'activate' => __( 'Activate', 'robin-image-optimizer' ), |
| 397 | 'install' => __( 'Install', 'robin-image-optimizer' ), |
| 398 | 'deactivate' => __( 'Deactivate', 'robin-image-optimizer' ), |
| 399 | 'delete' => __( 'Delete', 'robin-image-optimizer' ), |
| 400 | 'loading' => __( 'Please wait...', 'robin-image-optimizer' ), |
| 401 | 'preparation' => __( 'Preparation...', 'robin-image-optimizer' ), |
| 402 | 'read' => __( 'Read more', 'robin-image-optimizer' ), |
| 403 | ]; |
| 404 | } |
| 405 | |
| 406 | |
| 407 | /** |
| 408 | * Allows you to get the base path to the plugin in the directory wp-content/plugins/ |
| 409 | * |
| 410 | * @param $slug - slug for example "clearfy", "hide-login-page" |
| 411 | * |
| 412 | * @return int|null|string - "clearfy/clearfy.php" |
| 413 | */ |
| 414 | protected function get_plugin_base_path_by_slug( $slug ) { |
| 415 | // Check if the function get_plugins() is registered. It is necessary for the front-end |
| 416 | // usually get_plugins() only works in the admin panel. |
| 417 | if ( ! function_exists( 'get_plugins' ) ) { |
| 418 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 419 | } |
| 420 | |
| 421 | $plugins = get_plugins(); |
| 422 | |
| 423 | foreach ( $plugins as $base_path => $plugin ) { |
| 424 | if ( strpos( $base_path, rtrim( trim( $slug ) ) ) !== false ) { |
| 425 | return $base_path; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | return null; |
| 430 | } |
| 431 | } |
| 432 |