SimCal_WP_Requirements

The Simple Calendar SimCal WP Requirements class.

Defined (1)

The class is defined in the following location(s).

/includes/wp-requirements.php  
  1. class SimCal_WP_Requirements { 
  2.  
  3. /** 
  4. * Plugin name. 
  5. * @access private 
  6. * @var string 
  7. */ 
  8. private $name = ''; 
  9.  
  10. /** 
  11. * Plugin main file. 
  12. * plugin_basename( __FILE__ ) 
  13. * @access private 
  14. * @var string 
  15. */ 
  16. private $plugin = ''; 
  17.  
  18. /** 
  19. * WordPress. 
  20. * @access private 
  21. * @var bool 
  22. */ 
  23. private $wp = true; 
  24.  
  25. /** 
  26. * PHP. 
  27. * @access private 
  28. * @var bool 
  29. */ 
  30. private $php = true; 
  31.  
  32. /** 
  33. * PHP Extensions. 
  34. * @access private 
  35. * @var bool 
  36. */ 
  37. private $extensions = true; 
  38.  
  39. /** 
  40. * Requirements to check. 
  41. * @access private 
  42. * @var array 
  43. */ 
  44. private $requirements = array(); 
  45.  
  46. /** 
  47. * Results failures. 
  48. * Associative array with requirements results. 
  49. * @access private 
  50. * @var array 
  51. */ 
  52. private $failures = array(); 
  53.  
  54. /** 
  55. * Admin notice. 
  56. * @access private 
  57. * @var string 
  58. */ 
  59. private $notice = ''; 
  60.  
  61. /** 
  62. * Run checks. 
  63. * @param string $name The plugin name. 
  64. * @param string $plugin Output of `plugin_basename( __FILE__ )`. 
  65. * @param array $requirements Associative array with requirements. 
  66. */ 
  67. public function __construct( $name, $plugin, $requirements ) { 
  68.  
  69. $this->name = htmlspecialchars( strip_tags( $name ) ); 
  70. $this->plugin = $plugin; 
  71. $this->requirements = $requirements; 
  72.  
  73. if ( ! empty( $requirements ) && is_array( $requirements ) ) { 
  74.  
  75. $failures = $extensions = array(); 
  76.  
  77. $requirements = array_merge( 
  78. array( 
  79. 'WordPress' => '',  
  80. 'PHP' => '',  
  81. 'Extensions' => '',  
  82. ), $requirements 
  83. ); 
  84.  
  85. // Check for WordPress version. 
  86. if ( $requirements['WordPress'] && is_string( $requirements['WordPress'] ) ) { 
  87. if ( function_exists( 'get_bloginfo' ) ) { 
  88. $wp_version = get_bloginfo( 'version' ); 
  89. if ( version_compare( $wp_version, $requirements['WordPress'] ) === - 1 ) { 
  90. $failures['WordPress'] = $wp_version; 
  91. $this->wp = false; 
  92.  
  93. // Check fo PHP version. 
  94. if ( $requirements['PHP'] && is_string( $requirements['PHP'] ) ) { 
  95. if ( version_compare( PHP_VERSION, $requirements['PHP'] ) === -1 ) { 
  96. $failures['PHP'] = PHP_VERSION; 
  97. $this->php = false; 
  98.  
  99. // Check fo PHP Extensions. 
  100. if ( $requirements['Extensions'] && is_array( $requirements['Extensions'] ) ) { 
  101. foreach ( $requirements['Extensions'] as $extension ) { 
  102. if ( $extension && is_string( $extension ) ) { 
  103. $extensions[ $extension ] = extension_loaded( $extension ); 
  104. if ( in_array( false, $extensions ) ) { 
  105. foreach ( $extensions as $extension_name => $found ) { 
  106. if ( $found === false ) { 
  107. $failures['Extensions'][ $extension_name ] = $extension_name; 
  108. $this->extensions = false; 
  109.  
  110. $this->failures = $failures; 
  111.  
  112. } else { 
  113.  
  114. trigger_error( 'WP Requirements: the requirements are invalid.', E_USER_ERROR ); 
  115.  
  116.  
  117. /** 
  118. * Get requirements results. 
  119. * @return array 
  120. */ 
  121. public function failures() { 
  122. return $this->failures; 
  123.  
  124. /** 
  125. * Check if versions check pass. 
  126. * @return bool 
  127. */ 
  128. public function pass() { 
  129. if ( in_array( false, array( 
  130. $this->wp,  
  131. $this->php,  
  132. $this->extensions,  
  133. ) ) ) { 
  134. return false; 
  135. return true; 
  136.  
  137. /** 
  138. * Notice message. 
  139. * @param string $message An additional message. 
  140. * @return string 
  141. */ 
  142. public function get_notice( $message = '' ) { 
  143.  
  144. $notice = ''; 
  145. $name = $this->name; 
  146. $failures = $this->failures; 
  147.  
  148. if ( ! empty( $failures ) && is_array( $failures ) ) { 
  149.  
  150. $notice = '<div class="error">' . "\n"; 
  151. $notice .= "\t" . '<p>' . "\n"; 
  152. $notice .= '<strong>' . sprintf( '%s could not be activated.', $name ) . '</strong><br>'; 
  153.  
  154. foreach ( $failures as $requirement => $found ) { 
  155.  
  156. $required = $this->requirements[ $requirement ]; 
  157.  
  158. if ( 'Extensions' == $requirement ) { 
  159. if ( is_array( $found ) ) { 
  160. $notice .= sprintf(  
  161. 'Required PHP Extension(s) not found: %s.',  
  162. join( ', ', $found )  
  163. ) . '<br>'; 
  164. } else { 
  165. $notice .= sprintf(  
  166. 'Required %1$s version: %2$s - Version found: %3$s',  
  167. $requirement,  
  168. $required,  
  169. $found  
  170. ) . '<br>'; 
  171.  
  172.  
  173. $notice .= '<em>' . sprintf( 'Please update to meet %s requirements.', $name ) . '</em>' . "\n"; 
  174. $notice .= "\t" . '</p>' . "\n"; 
  175. if ( $message ) { 
  176. $notice .= $message; 
  177. $notice .= '</div>'; 
  178.  
  179. return $notice; 
  180.  
  181. /** 
  182. * Print notice. 
  183. */ 
  184. public function print_notice() { 
  185. echo $this->notice; 
  186.  
  187. /** 
  188. * Deactivate plugin. 
  189. */ 
  190. public function deactivate_plugin() { 
  191. if ( function_exists( 'deactivate_plugins' ) && function_exists( 'plugin_basename' ) ) { 
  192. deactivate_plugins( $this->plugin ); 
  193.  
  194. /** 
  195. * Deactivate plugin and display admin notice. 
  196. * @param string $message An additional message in notice. 
  197. */ 
  198. public function halt( $message = '' ) { 
  199.  
  200. $this->notice = $this->get_notice( $message ); 
  201.  
  202. if ( $this->notice && function_exists( 'add_action' ) ) { 
  203.  
  204. add_action( 'admin_notices', array( $this, 'print_notice' ) ); 
  205. add_action( 'admin_init', array( $this, 'deactivate_plugin' ) ); 
  206.  
  207. if ( isset( $_GET['activate'] ) ) { 
  208. unset( $_GET['activate'] ); 
  209.