PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/opcache/opcache.php

https://bitbucket.org/daniellbraun/wildernessatthesmokies
PHP | 438 lines | 363 code | 63 blank | 12 comment | 43 complexity | c6c5fff8c76093c2a5241b7d674a35ea MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-3.0, GPL-2.0, GPL-3.0, MIT
  1. <?php
  2. /*
  3. * Plugin Name: OPcache Dashboard
  4. * Plugin URI: http://wordpress.org/plugins/opcache/
  5. * Description: OPcache dashboard designed for WordPress
  6. * Version: 0.3.1
  7. * Author: Daisuke Takahashi(Extend Wings)
  8. * Author URI: http://www.extendwings.com
  9. * License: AGPLv3 or later
  10. * Text Domain: opcache
  11. * Domain Path: /languages/
  12. */
  13. if( !function_exists('add_action') ) {
  14. echo 'Hi there! I\'m just a plugin, not much I can do when called directly.';
  15. exit;
  16. }
  17. if( version_compare( get_bloginfo('version'), '3.8', '<') ) {
  18. require_once( ABSPATH . 'wp-admin/includes/plugin.php');
  19. deactivate_plugins( __FILE__ );
  20. }
  21. if( !class_exists('WP_List_Table') )
  22. require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
  23. add_action('init', array('OPcache_dashboard', 'init') );
  24. class OPcache_dashboard {
  25. static $instance;
  26. const PHP_URL = 'http://php.shield-9.org';
  27. const VERSION = '0.3.1';
  28. static $PLUGIN_URL;
  29. static $PLUGIN_DIR;
  30. static $PLUGIN_FILE;
  31. private $data;
  32. private $hooks;
  33. static function init() {
  34. if( !self::$instance ) {
  35. self::$PLUGIN_URL = plugin_dir_url( __FILE__ );
  36. self::$PLUGIN_DIR = plugin_dir_path( __FILE__ );
  37. self::$PLUGIN_FILE = __FILE__;
  38. if( did_action('plugins_loaded') )
  39. self::plugin_textdomain();
  40. else
  41. add_action('plugins_loaded', array( __CLASS__, 'plugin_textdomain') );
  42. self::$instance = new OPcache_dashboard;
  43. }
  44. return self::$instance;
  45. }
  46. private function __construct() {
  47. add_action('admin_menu', array( &$this, 'add_admin_menu') );
  48. if( is_multisite() && is_network_admin() )
  49. add_action('network_admin_menu', array( &$this, 'add_admin_menu') );
  50. add_action('wp_loaded', array( &$this, 'register_assets') );
  51. add_filter('plugin_row_meta', array( &$this, 'plugin_row_meta'), 10, 2);
  52. add_action('wp_dashboard_setup', array( &$this, 'add_dashboard_widgets') );
  53. // Reset all cache when Upgrader Process complete
  54. add_action('upgrader_process_complete', array( &$this, 'version_up_reset'), 10, 2);
  55. }
  56. function version_up_reset() {
  57. opcache_reset();
  58. $hook_extra = array(
  59. 'action' => 'update',
  60. 'type' => 'core',
  61. 'bulk' => true,
  62. );
  63. if( func_num_args() >= 2)
  64. $hook_extra = array_merge( $hook_extra, func_get_arg(1) );
  65. trigger_error("Your WordPress is successfully updated! Detail:\n" . var_export( $hook_extra, true), E_USER_NOTICE );
  66. }
  67. function register_assets() {
  68. if( is_admin() ) {
  69. if( !wp_script_is('d3js', 'registered') )
  70. wp_register_script(
  71. 'd3js',
  72. self::$PLUGIN_URL . 'js/d3.min.js',
  73. false,
  74. '3.4.4'
  75. );
  76. if( !wp_script_is('opcache', 'registered'))
  77. wp_register_script(
  78. 'opcache',
  79. self::$PLUGIN_URL . 'js/chart.js',
  80. array('jquery', 'd3js'),
  81. self::VERSION,
  82. true
  83. );
  84. if( !wp_script_is('jquery-center', 'registered') )
  85. wp_register_script(
  86. 'jquery-center',
  87. self::$PLUGIN_URL . 'js/jquery.center.min.js',
  88. array('jquery'),
  89. '1.1.1'
  90. );
  91. if( !wp_style_is('opcache', 'registered') )
  92. wp_register_style(
  93. 'opcache',
  94. self::$PLUGIN_URL . 'css/style.css',
  95. false,
  96. self::VERSION
  97. );
  98. if( !wp_style_is('genericons', 'registered') )
  99. wp_register_style(
  100. 'genericons',
  101. self::$PLUGIN_URL . 'css/genericons.css',
  102. false,
  103. '3.0.3'
  104. );
  105. }
  106. }
  107. function add_admin_menu() {
  108. $this->hooks[] = add_menu_page(
  109. __('OPcache Dashboard', 'opcache'), // page_title
  110. __('OPcache', 'opcache'), // menu_title
  111. 'manage_options', // capability
  112. 'opcache', // menu_slug
  113. array( &$this, 'render_admin_page'), // function
  114. 'dashicons-backup', // icon_url
  115. '3.14159265359' // position
  116. );
  117. $this->hooks[] = add_submenu_page(
  118. 'opcache', // parent_slug,
  119. __('OPcache Dashboard', 'opcache'), // page_title
  120. __('Dashboard', 'opcache'), // menu_title,
  121. 'manage_options', // capability,
  122. 'opcache', // menu_slug,
  123. array( &$this, 'render_admin_page') // function
  124. );
  125. $this->hooks[] = add_submenu_page(
  126. 'opcache', // parent_slug,
  127. __('Status', 'opcache'), // page_title
  128. __('Status', 'opcache'), // menu_title,
  129. 'manage_options', // capability,
  130. 'opcache-status', // menu_slug,
  131. array( &$this, 'render_admin_status_page') // function
  132. );
  133. $this->hooks[] = add_submenu_page(
  134. 'opcache', // parent_slug,
  135. __('Scripts', 'opcache'), // page_title
  136. __('Scripts', 'opcache'), // menu_title,
  137. 'manage_options', // capability,
  138. 'opcache-scripts', // menu_slug,
  139. array( &$this, 'render_admin_scripts_page') // function
  140. );
  141. $this->hooks[] = add_submenu_page(
  142. 'opcache', // parent_slug,
  143. __('Configuration', 'opcache'), // page_title
  144. __('Configuration', 'opcache'), // menu_title,
  145. 'manage_options', // capability,
  146. 'opcache-config', // menu_slug,
  147. array( &$this, 'render_admin_config_page') // function
  148. );
  149. if( version_compare( PHP_VERSION, '5.5.5') >= 0)
  150. $this->hooks[] = add_submenu_page(
  151. 'opcache', // parent_slug,
  152. __('Manual Cache Control', 'opcache'), // page_title
  153. __('Manual Control', 'opcache'), // menu_title,
  154. 'manage_options', // capability,
  155. 'opcache-manual', // menu_slug,
  156. array( &$this, 'render_admin_manual_page') // function
  157. );
  158. add_action('admin_enqueue_scripts', array( &$this, 'admin_menu_assets') );
  159. }
  160. function admin_menu_assets( $hook ) {
  161. if( in_array( $hook, $this->hooks ) ) {
  162. wp_enqueue_style('opcache');
  163. wp_enqueue_style('genericons');
  164. }
  165. switch( $hook ) {
  166. case 'toplevel_page_opcache':
  167. wp_enqueue_script('opcache');
  168. wp_enqueue_script('jquery-center');
  169. wp_enqueue_script('postbox');
  170. case 'opcache_page_opcache-scripts':
  171. case 'opcache_page_opcache-status':
  172. case 'opcache_page_opcache-config':
  173. case 'opcache_page_opcache-manual':
  174. }
  175. }
  176. function load_view( $template, $data = array() ) {
  177. $views_dir = self::$PLUGIN_DIR . 'views/';
  178. if( file_exists( $views_dir . $template ) ) {
  179. require_once( $views_dir . $template );
  180. return true;
  181. }
  182. error_log( "OPcache Dashboard: Unable to find view file $views_dir$template" );
  183. return false;
  184. }
  185. function render_admin_page() {
  186. $screen = get_current_screen();
  187. if( isset( $_GET['action'] ) && isset( $_GET['_wpnonce'] ) && check_admin_referer('opcache_ctrl','_wpnonce') ) {
  188. $url = sprintf('admin.php?page=%1$s', $_REQUEST['page'] );
  189. $url = is_network_admin() ? network_admin_url( $url ) : admin_url( $url );
  190. switch($_GET['action']) {
  191. case 'reset':
  192. opcache_reset();
  193. break;
  194. case 'invalidate':
  195. $status = opcache_get_status();
  196. foreach( $status['scripts'] as $script )
  197. opcache_invalidate( $script['full_path'] );
  198. break;
  199. case 'invalidate_force':
  200. $status = opcache_get_status();
  201. foreach( $status['scripts'] as $script )
  202. opcache_invalidate( $script['full_path'], true);
  203. break;
  204. }
  205. wp_safe_redirect($url);
  206. }
  207. $config = $this->data['config'] = opcache_get_configuration();
  208. $status = $this->data['status'] = opcache_get_status(false);
  209. add_meta_box(
  210. 'version-info', // widget_id
  211. sprintf(
  212. 'PHP: %1$s and OPcache: %2$s',
  213. phpversion(),
  214. $config['version']['version']
  215. ), // widget_name
  216. array( &$this, 'render_widget_version_info'), // callback
  217. $screen->id, // screen
  218. 'normal' // location
  219. );
  220. add_meta_box(
  221. 'ctrl', // widget_id
  222. esc_html__('Reset/Invalidate', 'opcache'), // widget_name
  223. array( &$this, 'render_widget_ctrl'), // callback
  224. $screen->id, // screen
  225. 'normal' // location
  226. );
  227. add_meta_box(
  228. 'info-widget', // widget_id
  229. esc_html__('Information', 'opcache'), // widget_name
  230. array( &$this, 'render_widget_info_widget'), // callback
  231. $screen->id, // screen
  232. 'normal' // location
  233. );
  234. add_meta_box(
  235. 'graphbox', // widget_id
  236. 'Status Graph', // widget_name
  237. array( &$this, 'render_widget_graph'), // callback
  238. $screen->id, // screen
  239. 'side' // location
  240. );
  241. $data = array(
  242. 'screen' => $screen,
  243. 'status' => $status,
  244. );
  245. $this->load_view('admin.php', $data );
  246. }
  247. function render_widget_version_info() {
  248. $this->load_view('widgets/version-info.php', $this->data );
  249. }
  250. function render_widget_ctrl() {
  251. $this->load_view('widgets/ctrl.php');
  252. }
  253. function render_widget_info_widget() {
  254. $this->load_view('widgets/info.php');
  255. }
  256. function render_widget_graph() {
  257. $this->load_view('widgets/graph.php');
  258. }
  259. function render_admin_status_page() {
  260. $raw_status = opcache_get_status( false );
  261. require_once( self::$PLUGIN_DIR . 'class.status-list-table.php');
  262. foreach( $raw_status as $key => $value ) {
  263. if( $key === 'scripts')
  264. continue;
  265. if( is_bool( $value ) )
  266. $value = ( $value === true ) ? 'true' : 'false';
  267. if( is_array( $value ) ) {
  268. foreach( $value as $k => $v ) {
  269. if( is_bool( $v ) ) $v = ( $v === true ) ? 'true' : 'false';
  270. $status[] = array('name' => $k, 'value' => $v );
  271. }
  272. } else
  273. $status[] = array('name' => $key, 'value' => $value );
  274. }
  275. $list_table = new OPcache_List_Table( $status );
  276. $list_table->prepare_items();
  277. $this->load_view('admin-status.php', $list_table );
  278. }
  279. function render_admin_scripts_page() {
  280. $status = opcache_get_status();
  281. require_once( self::$PLUGIN_DIR . 'class.script-list-table.php');
  282. $list_table = new OPcache_List_Table( $status['scripts'] );
  283. $list_table->prepare_items();
  284. $this->load_view('admin-scripts.php', $list_table );
  285. }
  286. function render_admin_config_page() {
  287. $raw_config = opcache_get_configuration();
  288. require_once( self::$PLUGIN_DIR . 'class.config-list-table.php');
  289. foreach( $raw_config as $key => $value ) {
  290. if( is_array( $value ) ) {
  291. foreach( $value as $k => $v ) {
  292. if( is_bool( $v ) ) $v = ( $v === true ) ? 'true' : 'false';
  293. $config[] = array('name' => $key.'.'.$k, 'value' => $v );
  294. }
  295. }
  296. }
  297. $list_table = new OPcache_List_Table( $config );
  298. $list_table->prepare_items();
  299. $this->load_view('admin-config.php', $list_table );
  300. }
  301. function render_admin_manual_page() {
  302. if(isset( $_POST['action'] ) && isset( $_POST['_wpnonce'] ) && check_admin_referer('opcache_ctrl','_wpnonce') ) {
  303. switch( $_POST['action'] ) {
  304. case 'compile':
  305. if( isset( $_POST['file'] ) && file_exists( $_POST['file'] ) && !is_dir( $_POST['file'] ) ) {
  306. if( version_compare( PHP_VERSION, '5.5.11') < 0 or !opcache_is_script_cached( $_POST['file'] ) ) {
  307. opcache_compile_file( $_POST['file'] );
  308. printf('<div class="updated"><p>%s</p></div>', esc_html__('Compiled!', 'opcache') );
  309. } else
  310. printf('<div class="error"><p>%s</p></div>', esc_html__('The script is already cached.', 'opcache') );
  311. } else
  312. printf('<div class="error"><p>%s</p></div>', esc_html__('No such file or directory.', 'opcache') );
  313. break;
  314. case 'invalidate':
  315. if( isset( $_POST['file'] ) && file_exists( $_POST['file'] ) && !is_dir( $_POST['file'] ) ) {
  316. if( version_compare( PHP_VERSION, '5.5.11') < 0 or opcache_is_script_cached( $_POST['file'] ) ) {
  317. if( isset( $_POST['force'] ) && $_POST['force'] == 'on') {
  318. opcache_invalidate( $_POST['file'], true );
  319. printf('<div class="updated"><p>%s</p></div>', esc_html__('Force Invalidated!', 'opcache') );
  320. } else {
  321. opcache_invalidate( $_POST['file'] );
  322. printf('<div class="updated"><p>%s</p></div>', esc_html__('Invalidated!', 'opcache') );
  323. }
  324. } else
  325. printf('<div class="error"><p>%s</p></div>', esc_html__('The script is not cached yet.', 'opcache') );
  326. } else
  327. printf('<div class="error"><p>%s</p></div>', esc_html__('No such file or directory.', 'opcache') );
  328. break;
  329. }
  330. }
  331. $this->load_view('admin-manual.php');
  332. }
  333. function add_dashboard_widgets() {
  334. wp_add_dashboard_widget(
  335. 'opcache_graph', // slug
  336. esc_html__('OPcahce Status', 'opcache'), // title
  337. array( &$this, 'render_dashboard_widget') // display function
  338. );
  339. }
  340. function render_dashboard_widget() {
  341. $this->data['config'] = opcache_get_configuration();
  342. $this->data['status'] = opcache_get_status(false);
  343. $this->load_view('widgets/dashboard.php', $this->data );
  344. }
  345. static function plugin_textdomain() {
  346. load_plugin_textdomain('opcache', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/');
  347. }
  348. static function size( $size ) {
  349. $si_units = array('', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
  350. $i = 0;
  351. while( $size >= 1024 && $i < count( $si_units ) ) {
  352. $size = round( $size / 1024, 2);
  353. $i++;
  354. }
  355. return OPcache_dashboard::number_format( $size ) . $si_units[ $i ] . 'B';
  356. }
  357. static function number_format( $number, $decimals = 2) {
  358. return number_format( $number, $decimals, '.', ',');
  359. }
  360. function plugin_row_meta( $links, $file ) {
  361. if( plugin_basename( __FILE__ ) === $file ) {
  362. $links[] = sprintf(
  363. '<a href="%s">%s</a>',
  364. ( is_network_admin() ? network_admin_url('admin.php?page=opcache') : admin_url('admin.php?page=opcache') ),
  365. __('Dashboard', 'opcache')
  366. );
  367. $links[] = sprintf(
  368. '<a href="%s">%s</a>',
  369. esc_url('http://www.extendwings.com/donate/'),
  370. __('Donate', 'opcache')
  371. );
  372. }
  373. return $links;
  374. }
  375. }