| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets; |
| 4 |
|
| 5 |
/** |
| 6 |
* Functions specific to the administration interface |
| 7 |
* |
| 8 |
* @package Code_Snippets |
| 9 |
*/ |
| 10 |
class Admin { |
| 11 |
|
| 12 |
/** |
| 13 |
* Admin_Menu class instances |
| 14 |
* |
| 15 |
* @var array |
| 16 |
*/ |
| 17 |
public $menus = array(); |
| 18 |
|
| 19 |
/** |
| 20 |
* Class constructor |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
if ( is_admin() ) { |
| 24 |
$this->run(); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Initialise classes |
| 30 |
*/ |
| 31 |
public function load_classes() { |
| 32 |
$this->menus['manage'] = new Manage_Menu(); |
| 33 |
$this->menus['edit'] = new Edit_Menu(); |
| 34 |
$this->menus['import'] = new Import_Menu(); |
| 35 |
|
| 36 |
if ( is_network_admin() === Settings\are_settings_unified() ) { |
| 37 |
$this->menus['settings'] = new Settings_Menu(); |
| 38 |
} |
| 39 |
|
| 40 |
foreach ( $this->menus as $menu ) { |
| 41 |
$menu->run(); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Register action and filter hooks |
| 47 |
*/ |
| 48 |
public function run() { |
| 49 |
add_action( 'init', array( $this, 'load_classes' ), 11 ); |
| 50 |
|
| 51 |
add_filter( 'mu_menu_items', array( $this, 'mu_menu_items' ) ); |
| 52 |
add_filter( 'plugin_action_links_' . plugin_basename( PLUGIN_FILE ), array( $this, 'plugin_settings_link' ) ); |
| 53 |
add_filter( 'plugin_row_meta', array( $this, 'plugin_meta_links' ), 10, 2 ); |
| 54 |
add_filter( 'debug_information', array( $this, 'debug_information' ) ); |
| 55 |
add_action( 'code_snippets/admin/manage', array( $this, 'survey_message' ) ); |
| 56 |
add_action( 'admin_enqueue_scripts', array( $this, 'load_admin_menu_icon' ) ); |
| 57 |
|
| 58 |
if ( ! empty( $_POST['save_snippet'] ) ) { |
| 59 |
add_action( 'code_snippets/allow_execute_snippet', array( $this, 'prevent_exec_on_save' ), 10, 3 ); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Allow super admins to control site admin access to |
| 65 |
* snippet admin menus |
| 66 |
* |
| 67 |
* Adds a checkbox to the *Settings > Network Settings* |
| 68 |
* network admin menu |
| 69 |
* |
| 70 |
* @param array $menu_items Current mu menu items. |
| 71 |
* |
| 72 |
* @return array The modified mu menu items |
| 73 |
* |
| 74 |
* @since 1.7.1 |
| 75 |
*/ |
| 76 |
public function mu_menu_items( $menu_items ) { |
| 77 |
$menu_items['snippets'] = __( 'Snippets', 'code-snippets' ); |
| 78 |
$menu_items['snippets_settings'] = __( 'Snippets » Settings', 'code-snippets' ); |
| 79 |
|
| 80 |
return $menu_items; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Load the stylesheet for the admin menu icon |
| 85 |
*/ |
| 86 |
public function load_admin_menu_icon() { |
| 87 |
wp_enqueue_style( |
| 88 |
'menu-icon-snippets', |
| 89 |
plugins_url( 'css/min/menu-icon.css', code_snippets()->file ), |
| 90 |
array(), |
| 91 |
code_snippets()->version |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Prevent the snippet currently being saved from being executed |
| 97 |
* so that it is not run twice (once normally, once when validated) |
| 98 |
* |
| 99 |
* @param bool $exec Whether the snippet will be executed. |
| 100 |
* @param int $exec_id ID of the snippet being executed. |
| 101 |
* @param string $table_name Name of the database table the snippet is stored in. |
| 102 |
* |
| 103 |
* @return bool Whether the snippet will be executed. |
| 104 |
*/ |
| 105 |
public function prevent_exec_on_save( $exec, $exec_id, $table_name ) { |
| 106 |
|
| 107 |
if ( ! isset( $_POST['save_snippet'], $_POST['snippet_id'] ) ) { |
| 108 |
return $exec; |
| 109 |
} |
| 110 |
|
| 111 |
if ( code_snippets()->db->get_table_name() !== $table_name ) { |
| 112 |
return $exec; |
| 113 |
} |
| 114 |
|
| 115 |
$id = intval( $_POST['snippet_id'] ); |
| 116 |
|
| 117 |
if ( $id === $exec_id ) { |
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
return $exec; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Adds a link pointing to the Manage Snippets page |
| 126 |
* |
| 127 |
* @param array $links Existing plugin action links. |
| 128 |
* |
| 129 |
* @return array Modified plugin action links |
| 130 |
* @since 2.0.0 |
| 131 |
*/ |
| 132 |
public function plugin_settings_link( $links ) { |
| 133 |
$format = '<a href="%1$s" title="%2$s">%3$s</a>'; |
| 134 |
|
| 135 |
array_unshift( |
| 136 |
$links, |
| 137 |
sprintf( |
| 138 |
$format, |
| 139 |
esc_url( code_snippets()->get_menu_url( 'settings' ) ), |
| 140 |
esc_html__( 'Change plugin settings', 'code-snippets' ), |
| 141 |
esc_html__( 'Settings', 'code-snippets' ) |
| 142 |
) |
| 143 |
); |
| 144 |
|
| 145 |
array_unshift( |
| 146 |
$links, |
| 147 |
sprintf( |
| 148 |
$format, |
| 149 |
esc_url( code_snippets()->get_menu_url() ), |
| 150 |
esc_html__( 'Manage your existing snippets', 'code-snippets' ), |
| 151 |
esc_html__( 'Snippets', 'code-snippets' ) |
| 152 |
) |
| 153 |
); |
| 154 |
|
| 155 |
return $links; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Adds extra links related to the plugin |
| 160 |
* |
| 161 |
* @param array $links Existing plugin info links. |
| 162 |
* @param string $file The plugin the links are for. |
| 163 |
* |
| 164 |
* @return array The modified plugin info links. |
| 165 |
* @since 2.0.0 |
| 166 |
*/ |
| 167 |
public function plugin_meta_links( $links, $file ) { |
| 168 |
|
| 169 |
/* We only want to affect the Code Snippets plugin listing */ |
| 170 |
if ( plugin_basename( PLUGIN_FILE ) !== $file ) { |
| 171 |
return $links; |
| 172 |
} |
| 173 |
|
| 174 |
$format = '<a href="%1$s" title="%2$s" target="_blank">%3$s</a>'; |
| 175 |
|
| 176 |
/* array_merge appends the links to the end */ |
| 177 |
|
| 178 |
return array_merge( |
| 179 |
$links, |
| 180 |
array( |
| 181 |
sprintf( |
| 182 |
$format, |
| 183 |
'https://codesnippets.pro/about/', |
| 184 |
esc_attr__( 'Find out more about Code Snippets', 'code-snippets' ), |
| 185 |
esc_html__( 'About', 'code-snippets' ) |
| 186 |
), |
| 187 |
sprintf( |
| 188 |
$format, |
| 189 |
'https://codesnippets.pro/support/', |
| 190 |
esc_attr__( 'Find out how to get support with Code Snippets', 'code-snippets' ), |
| 191 |
esc_html__( 'Support', 'code-snippets' ) |
| 192 |
), |
| 193 |
sprintf( |
| 194 |
'<a href="%1$s" title="%2$s" style="color: #d46f4d;">%3$s</a>', |
| 195 |
'https://codesnippets.pro/', |
| 196 |
esc_attr__( 'Upgrade to Code Snippets Pro', 'code-snippets' ), |
| 197 |
esc_html__( 'Upgrade to Pro', 'code-snippets' ) |
| 198 |
), |
| 199 |
) |
| 200 |
); |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Add Code Snippets information to Site Health information. |
| 205 |
* |
| 206 |
* @param array $info The Site Health information. |
| 207 |
* |
| 208 |
* @return array The updated Site Health information. |
| 209 |
* @author sc0ttkclark |
| 210 |
*/ |
| 211 |
public function debug_information( $info ) { |
| 212 |
$fields = array(); |
| 213 |
|
| 214 |
// fetch all active snippets. |
| 215 |
$args = array( |
| 216 |
'active_only' => true, |
| 217 |
'limit' => 100, |
| 218 |
); |
| 219 |
$snippet_objects = get_snippets( array(), null, $args ); |
| 220 |
|
| 221 |
// build the debug information from snippet data. |
| 222 |
foreach ( $snippet_objects as $snippet ) { |
| 223 |
$values = [ $snippet->scope_name ]; |
| 224 |
$debug = []; |
| 225 |
|
| 226 |
if ( $snippet->name ) { |
| 227 |
$debug[] = 'name: ' . $snippet->name; |
| 228 |
} |
| 229 |
|
| 230 |
$debug[] = 'scope: ' . $snippet->scope; |
| 231 |
|
| 232 |
if ( $snippet->modified ) { |
| 233 |
/* translators: %s: formatted last modified date */ |
| 234 |
$values[] = sprintf( __( 'Last modified %s', 'code-snippets' ), $snippet->format_modified( false ) ); |
| 235 |
$debug[] = 'modified: ' . $snippet->modified; |
| 236 |
} |
| 237 |
|
| 238 |
if ( $snippet->tags ) { |
| 239 |
$values[] = $snippet->tags_list; |
| 240 |
$debug[] = 'tags: [' . $snippet->tags_list . ']'; |
| 241 |
} |
| 242 |
|
| 243 |
$fields[ 'snippet-' . $snippet->id ] = [ |
| 244 |
'label' => $snippet->display_name, |
| 245 |
'value' => implode( "\n | ", $values ), |
| 246 |
'debug' => implode( ', ', $debug ), |
| 247 |
]; |
| 248 |
} |
| 249 |
|
| 250 |
$snippets_info = array( |
| 251 |
'label' => __( 'Active Snippets', 'code-snippets' ), |
| 252 |
'show_count' => true, |
| 253 |
'fields' => $fields, |
| 254 |
); |
| 255 |
|
| 256 |
// attempt to insert the new section right after the Inactive Plugins section. |
| 257 |
$index = array_search( 'wp-plugins-inactive', array_keys( $info ), true ); |
| 258 |
|
| 259 |
if ( false === $index ) { |
| 260 |
$info['code-snippets'] = $snippets_info; |
| 261 |
} else { |
| 262 |
$info = array_merge( |
| 263 |
array_slice( $info, 0, $index + 1 ), |
| 264 |
[ 'code-snippets' => $snippets_info ], |
| 265 |
array_slice( $info, $index + 1 ) |
| 266 |
); |
| 267 |
} |
| 268 |
|
| 269 |
return $info; |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Print a notice inviting people to participate in the Code Snippets Survey |
| 274 |
* |
| 275 |
* @return void |
| 276 |
* @since 1.9 |
| 277 |
*/ |
| 278 |
public function survey_message() { |
| 279 |
global $current_user; |
| 280 |
|
| 281 |
$key = 'ignore_code_snippets_survey_message'; |
| 282 |
|
| 283 |
/* Bail now if the user has dismissed the message */ |
| 284 |
if ( get_user_meta( $current_user->ID, $key ) ) { |
| 285 |
return; |
| 286 |
} elseif ( isset( $_GET[ $key ], $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), $key ) ) { |
| 287 |
add_user_meta( $current_user->ID, $key, true, true ); |
| 288 |
|
| 289 |
return; |
| 290 |
} |
| 291 |
|
| 292 |
?> |
| 293 |
|
| 294 |
<br/> |
| 295 |
|
| 296 |
<div class="updated code-snippets-survey-message"> |
| 297 |
<p> |
| 298 |
<?php |
| 299 |
echo wp_kses( |
| 300 |
__( "<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' ), |
| 301 |
array( 'strong' => array() ) |
| 302 |
); |
| 303 |
?> |
| 304 |
|
| 305 |
<a href="https://codesnippets.pro/survey/" class="button secondary" |
| 306 |
target="_blank" style="margin: auto .5em;"> |
| 307 |
<?php esc_html_e( 'Take the survey now', 'code-snippets' ); ?> |
| 308 |
</a> |
| 309 |
|
| 310 |
<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( $key, true ), $key ) ); ?>"> |
| 311 |
<?php esc_html_e( 'Dismiss', 'code-snippets' ); ?> |
| 312 |
</a> |
| 313 |
|
| 314 |
</p> |
| 315 |
</div> |
| 316 |
<?php |
| 317 |
} |
| 318 |
} |
| 319 |
|