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