PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/code-snippets/php/class-admin.php

https://bitbucket.org/chillidee/aroundmidnight.com
PHP | 235 lines | 139 code | 34 blank | 62 comment | 10 complexity | 52b29695c6dd1e965dcfc74564a2f97d MD5 | raw file
Possible License(s): MIT, AGPL-1.0, GPL-2.0
  1. <?php
  2. /**
  3. * Functions specific to the administration interface
  4. *
  5. * @package Code_Snippets
  6. */
  7. class Code_Snippets_Admin {
  8. public $menus = array();
  9. function __construct() {
  10. if ( is_admin() ) {
  11. $this->run();
  12. }
  13. }
  14. public function load_classes() {
  15. $this->menus['manage'] = new Code_Snippets_Manage_Menu();
  16. $this->menus['edit'] = new Code_Snippets_Edit_Menu();
  17. $this->menus['import'] = new Code_Snippets_Import_Menu();
  18. if ( is_network_admin() === code_snippets_unified_settings() ) {
  19. $this->menus['settings'] = new Code_Snippets_Settings_Menu();
  20. }
  21. foreach ( $this->menus as $menu ) {
  22. $menu->run();
  23. }
  24. }
  25. public function run() {
  26. add_action( 'init', array( $this, 'load_classes' ), 11 );
  27. add_filter( 'mu_menu_items', array( $this, 'mu_menu_items' ) );
  28. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_stylesheet' ) );
  29. add_filter( 'plugin_action_links_' . plugin_basename( CODE_SNIPPETS_FILE ), array( $this, 'plugin_settings_link' ) );
  30. add_filter( 'plugin_row_meta', array( $this, 'plugin_meta_links' ), 10, 2 );
  31. add_action( 'code_snippets/admin/manage', array( $this, 'survey_message' ) );
  32. if ( isset( $_POST['save_snippet'] ) && $_POST['save_snippet'] ) {
  33. add_action( 'code_snippets/allow_execute_snippet', array( $this, 'prevent_exec_on_save' ), 10, 3 );
  34. }
  35. }
  36. /**
  37. * Allow super admins to control site admin access to
  38. * snippet admin menus
  39. *
  40. * Adds a checkbox to the *Settings > Network Settings*
  41. * network admin menu
  42. *
  43. * @since 1.7.1
  44. *
  45. * @param array $menu_items The current mu menu items
  46. *
  47. * @return array The modified mu menu items
  48. */
  49. function mu_menu_items( $menu_items ) {
  50. $menu_items['snippets'] = __( 'Snippets', 'code-snippets' );
  51. $menu_items['snippets_settings'] = __( 'Snippets &raquo; Settings', 'code-snippets' );
  52. return $menu_items;
  53. }
  54. /**
  55. * Enqueue the stylesheet for a snippet menu
  56. *
  57. * @since 2.2.0
  58. * @uses wp_enqueue_style() to add the stylesheet to the queue
  59. * @uses get_user_option() to check if MP6 mode is active
  60. * @uses plugins_url() to retrieve a URL to assets
  61. *
  62. * @param string $hook the current page hook
  63. */
  64. function enqueue_admin_stylesheet( $hook ) {
  65. $pages = array( 'manage', 'add', 'edit', 'settings' );
  66. $hooks = array_map( 'code_snippets_get_menu_hook', $pages );
  67. /* First, load the menu icon stylesheet */
  68. wp_enqueue_style(
  69. 'menu-icon-snippets',
  70. plugins_url( 'css/min/menu-icon.css', CODE_SNIPPETS_FILE ),
  71. false,
  72. CODE_SNIPPETS_VERSION
  73. );
  74. /* Only load the stylesheet on the right snippets page */
  75. if ( ! in_array( $hook, $hooks ) ) {
  76. return;
  77. }
  78. $hooks = array_combine( $hooks, $pages );
  79. $page = $hooks[ $hook ];
  80. // add snippet page uses edit stylesheet
  81. 'add' === $page && $page = 'edit';
  82. wp_enqueue_style(
  83. "code-snippets-$page",
  84. plugins_url( "css/min/$page.css", CODE_SNIPPETS_FILE ),
  85. false,
  86. CODE_SNIPPETS_VERSION
  87. );
  88. }
  89. /**
  90. * Prevent the snippet currently being saved from being executed
  91. * so it is not run twice (once normally, once
  92. *
  93. * @param bool $exec Whether the snippet will be executed
  94. * @param int $exec_id The ID of the snippet being executed
  95. * @param string $table_name
  96. *
  97. * @return bool Whether the snippet will be executed
  98. */
  99. function prevent_exec_on_save( $exec, $exec_id, $table_name ) {
  100. if ( ! isset( $_POST['save_snippet'], $_POST['snippet_id'] ) ) {
  101. return $exec;
  102. }
  103. if ( code_snippets()->db->get_table_name() !== $table_name ) {
  104. return $exec;
  105. }
  106. $id = intval( $_POST['snippet_id'] );
  107. if ( $id === $exec_id ) {
  108. return false;
  109. }
  110. return $exec;
  111. }
  112. /**
  113. * Adds a link pointing to the Manage Snippets page
  114. *
  115. * @since 2.0
  116. *
  117. * @param array $links The existing plugin action links
  118. *
  119. * @return array The modified plugin action links
  120. */
  121. function plugin_settings_link( $links ) {
  122. array_unshift( $links, sprintf(
  123. '<a href="%1$s" title="%2$s">%3$s</a>',
  124. code_snippets()->get_menu_url(),
  125. __( 'Manage your existing snippets', 'code-snippets' ),
  126. __( 'Snippets', 'code-snippets' )
  127. ) );
  128. return $links;
  129. }
  130. /**
  131. * Adds extra links related to the plugin
  132. *
  133. * @since 2.0
  134. *
  135. * @param array $links The existing plugin info links
  136. * @param string $file The plugin the links are for
  137. *
  138. * @return array The modified plugin info links
  139. */
  140. function plugin_meta_links( $links, $file ) {
  141. /* We only want to affect the Code Snippets plugin listing */
  142. if ( plugin_basename( CODE_SNIPPETS_FILE ) !== $file ) {
  143. return $links;
  144. }
  145. $format = '<a href="%1$s" title="%2$s">%3$s</a>';
  146. /* array_merge appends the links to the end */
  147. return array_merge( $links, array(
  148. sprintf( $format,
  149. 'http://wordpress.org/plugins/code-snippets/',
  150. __( 'Visit the WordPress.org plugin page', 'code-snippets' ),
  151. __( 'About', 'code-snippets' )
  152. ),
  153. sprintf( $format,
  154. 'http://wordpress.org/support/plugin/code-snippets/',
  155. __( 'Visit the support forums', 'code-snippets' ),
  156. __( 'Support', 'code-snippets' )
  157. ),
  158. sprintf( $format,
  159. 'http://bungeshea.com/donate/',
  160. __( "Support this plugin's development", 'code-snippets' ),
  161. __( 'Donate', 'code-snippets' )
  162. ),
  163. ) );
  164. }
  165. /**
  166. * Print a notice inviting people to participate in the Code Snippets Survey
  167. *
  168. * @since 1.9
  169. * @return void
  170. */
  171. function survey_message() {
  172. global $current_user;
  173. $key = 'ignore_code_snippets_survey_message';
  174. /* Bail now if the user has dismissed the message */
  175. if ( get_user_meta( $current_user->ID, $key ) ) {
  176. return;
  177. } elseif ( isset( $_GET[ $key ], $_REQUEST['_wpnonce'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], $key ) ) {
  178. add_user_meta( $current_user->ID, $key, true, true );
  179. return;
  180. }
  181. ?>
  182. <br/>
  183. <div class="updated"><p>
  184. <?php _e( "<strong>Have feedback on Code Snippets?</strong> Please take the time to answer a short survey on how you use this plugin and what you'd like to see changed or added in the future.", 'code-snippets' ); ?>
  185. <a href="http://sheabunge.polldaddy.com/s/code-snippets-feedback" class="button secondary"
  186. target="_blank" style="margin: auto .5em;">
  187. <?php _e( 'Take the survey now', 'code-snippets' ); ?>
  188. </a>
  189. <a href="<?php echo esc_url( wp_nonce_url( add_query_arg( $key, true ), $key ) ); ?>"><?php esc_html_e( 'Dismiss', 'code-snippets' ); ?></a>
  190. </p></div>
  191. <?php
  192. }
  193. }