Nessuna descrizione

Activator.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Popup Maker Extension Activation Handler Class
  10. *
  11. * @version 2.1
  12. */
  13. class PUM_Extension_Activator {
  14. public $extension_class_name;
  15. /**
  16. * @var string
  17. */
  18. public $extension_name;
  19. /**
  20. * @var string
  21. */
  22. public $extension_slug;
  23. /**
  24. * @var int
  25. */
  26. public $extension_id;
  27. /**
  28. * @var string
  29. */
  30. public $extension_version;
  31. /**
  32. * @var bool|string
  33. */
  34. public $extension_wp_repo = false;
  35. /**
  36. * @var string
  37. */
  38. public $extension_file;
  39. public $required_core_version;
  40. /**
  41. * @var bool
  42. */
  43. public $core_installed = false;
  44. public $core_path;
  45. /**
  46. * @param $class_name
  47. * @param $prop_name
  48. *
  49. * @return null|mixed
  50. */
  51. public function get_static_prop( $class_name, $prop_name ) {
  52. if ( version_compare( PHP_VERSION, '5.3', '<' ) ) {
  53. try {
  54. $class = new ReflectionClass( $class_name );
  55. return $class->getStaticPropertyValue( $prop_name );
  56. } catch ( ReflectionException $e ) {
  57. return null;
  58. }
  59. }
  60. return property_exists( $class_name, $prop_name ) ? $class_name::$$prop_name : null;
  61. }
  62. /**
  63. * Setup the activator class
  64. *
  65. * @param $class_name
  66. */
  67. public function __construct( $class_name ) {
  68. // We need plugin.php!
  69. require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  70. // Validate extension class is valid.
  71. if ( in_array( false, array(
  72. class_exists( $class_name ),
  73. property_exists( $class_name, 'NAME' ),
  74. property_exists( $class_name, 'REQUIRED_CORE_VER' ),
  75. method_exists( $class_name, 'instance' ),
  76. ) ) ) {
  77. return;
  78. }
  79. $this->extension_class_name = $class_name;
  80. $this->extension_id = $this->get_static_prop( $class_name, 'ID' );
  81. $this->extension_wp_repo = $this->get_static_prop( $class_name, 'WP_REPO' );
  82. $this->extension_name = $this->get_static_prop( $class_name, 'NAME' );
  83. $this->extension_version = $this->get_static_prop( $class_name, 'VER' );
  84. $this->required_core_version = $this->get_static_prop( $class_name, 'REQUIRED_CORE_VER' );
  85. $popup_maker_data = get_plugin_data( WP_PLUGIN_DIR . '/popup-maker/popup-maker.php', false, false );
  86. if ( $popup_maker_data['Name'] == 'Popup Maker' ) {
  87. $this->core_installed = true;
  88. $this->core_path = 'popup-maker/popup-maker.php';
  89. }
  90. }
  91. /**
  92. * @return string
  93. */
  94. public function get_status() {
  95. if ( $this->core_installed && ! class_exists( 'Popup_Maker' ) ) {
  96. return 'not_activated';
  97. } elseif ( $this->core_installed && isset( $this->required_core_version ) && version_compare( Popup_Maker::$VER, $this->required_core_version, '<' ) ) {
  98. return 'not_updated';
  99. } elseif ( ! $this->core_installed ) {
  100. return 'not_installed';
  101. }
  102. return 'active';
  103. }
  104. /**
  105. * Process plugin deactivation
  106. *
  107. * @access public
  108. */
  109. public function run() {
  110. if ( $this->get_status() != 'active' ) {
  111. // Display notice
  112. add_action( 'admin_notices', array( $this, 'missing_popmake_notice' ) );
  113. } else {
  114. $class_name = $this->extension_class_name;
  115. // Generate an instance of the extension class in a PHP 5.2 compatible way.
  116. call_user_func( array( $class_name, 'instance' ) );
  117. $this->extension_file = $this->get_static_prop( $class_name, 'FILE' );
  118. $plugin_slug = explode( '/', plugin_basename( $this->extension_file ), 2 );
  119. $this->extension_slug = str_replace( array( 'popup-maker-', 'pum-' ), '', $plugin_slug[0] );
  120. // Handle licensing for extensions with valid ID & not wp repo extensions.
  121. if ( $this->extension_id > 0 && ! $this->extension_wp_repo && class_exists( 'PUM_Extension_License' ) ) {
  122. new PUM_Extension_License( $this->extension_file, $this->extension_name, $this->extension_version, 'Popup Maker', null, null, $this->extension_id );
  123. }
  124. add_filter( 'pum_enabled_extensions', array( $this, 'enabled_extensions' ) );
  125. }
  126. }
  127. /**
  128. * Display notice if Popup Maker isn't installed
  129. */
  130. public function missing_popmake_notice() {
  131. switch ( $this->get_status() ) {
  132. case 'not_activated':
  133. $url = esc_url( wp_nonce_url( admin_url( 'plugins.php?action=activate&plugin=' . $this->core_path ), 'activate-plugin_' . $this->core_path ) );
  134. $link = '<a href="' . $url . '">' . __( 'activate it' ) . '</a>';
  135. echo '<div class="error"><p>' . sprintf( __( 'The plugin "%s" requires %s! Please %s to continue!' ), $this->extension_name, '<strong>' . __( 'Popup Maker' ) . '</strong>', $link ) . '</p></div>';
  136. break;
  137. case 'not_updated':
  138. $url = esc_url( wp_nonce_url( admin_url( 'update.php?action=upgrade-plugin&plugin=' . $this->core_path ), 'upgrade-plugin_' . $this->core_path ) );
  139. $link = '<a href="' . $url . '">' . __( 'update it' ) . '</a>';
  140. echo '<div class="error"><p>' . sprintf( __( 'The plugin "%s" requires %s v%s or higher! Please %s to continue!' ), $this->extension_name, '<strong>' . __( 'Popup Maker' ) . '</strong>', '<strong>' . $this->required_core_version . '</strong>', $link ) . '</p></div>';
  141. break;
  142. case 'not_installed':
  143. $url = esc_url( wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=popup-maker' ), 'install-plugin_popup-maker' ) );
  144. $link = '<a href="' . $url . '">' . __( 'install it' ) . '</a>';
  145. echo '<div class="error"><p>' . sprintf( __( 'The plugin "%s" requires %s! Please %s to continue!' ), $this->extension_name, '<strong>' . __( 'Popup Maker' ) . '</strong>', $link ) . '</p></div>';
  146. break;
  147. case 'active':
  148. default:
  149. return;
  150. }
  151. }
  152. /**
  153. * @param array $enabled_extensions
  154. *
  155. * @return array
  156. */
  157. public function enabled_extensions( $enabled_extensions = array() ) {
  158. $enabled_extensions[ $this->extension_slug ] = $this->extension_class_name;
  159. return $enabled_extensions;
  160. }
  161. }