| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This class handles the manage snippets menu |
| 5 |
* @since 2.4.0 |
| 6 |
* @package Code_Snippets |
| 7 |
*/ |
| 8 |
class Code_Snippets_Manage_Menu extends Code_Snippets_Admin_Menu { |
| 9 |
|
| 10 |
/** |
| 11 |
* Holds the list table class |
| 12 |
* @var Code_Snippets_List_Table |
| 13 |
*/ |
| 14 |
public $list_table; |
| 15 |
|
| 16 |
/** |
| 17 |
* Class constructor |
| 18 |
*/ |
| 19 |
public function __construct() { |
| 20 |
|
| 21 |
parent::__construct( 'manage', |
| 22 |
_x( 'All Snippets', 'menu label', 'code-snippets' ), |
| 23 |
__( 'Snippets', 'code-snippets' ) |
| 24 |
); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Register action and filter hooks |
| 29 |
*/ |
| 30 |
public function run() { |
| 31 |
parent::run(); |
| 32 |
|
| 33 |
if ( code_snippets()->admin->is_compact_menu() ) { |
| 34 |
add_action( 'admin_menu', array( $this, 'register_compact_menu' ), 2 ); |
| 35 |
add_action( 'network_admin_menu', array( $this, 'register_compact_menu' ), 2 ); |
| 36 |
} |
| 37 |
|
| 38 |
add_filter( 'set-screen-option', array( $this, 'save_screen_option' ), 10, 3 ); |
| 39 |
add_action( 'wp_ajax_update_code_snippet', array( $this, 'ajax_callback' ) ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Register the top-level 'Snippets' menu and associated 'Manage' subpage |
| 44 |
* |
| 45 |
* @uses add_menu_page() to register a top-level menu |
| 46 |
* @uses add_submenu_page() to register a sub-menu |
| 47 |
*/ |
| 48 |
function register() { |
| 49 |
|
| 50 |
/* Register the top-level menu */ |
| 51 |
add_menu_page( |
| 52 |
__( 'Snippets', 'code-snippets' ), |
| 53 |
_x( 'Snippets', 'top-level menu label', 'code-snippets' ), |
| 54 |
code_snippets()->get_cap(), |
| 55 |
code_snippets()->get_menu_slug(), |
| 56 |
array( $this, 'render' ), |
| 57 |
'div', // icon is added through CSS |
| 58 |
is_network_admin() ? 21 : 67 |
| 59 |
); |
| 60 |
|
| 61 |
/* Register the sub-menu */ |
| 62 |
parent::register(); |
| 63 |
} |
| 64 |
|
| 65 |
public function register_compact_menu() { |
| 66 |
|
| 67 |
if ( ! code_snippets()->admin->is_compact_menu() ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
$sub = code_snippets()->get_menu_slug( isset( $_GET['sub'] ) ? $_GET['sub'] : 'snippets' ); |
| 72 |
|
| 73 |
$classmap = array( |
| 74 |
'snippets' => 'manage', |
| 75 |
'add-snippet' => 'edit', |
| 76 |
'edit-snippet' => 'edit', |
| 77 |
'import-code-snippets' => 'import', |
| 78 |
'snippets-settings' => 'settings', |
| 79 |
); |
| 80 |
|
| 81 |
if ( isset( $classmap[ $sub ], code_snippets()->admin->menus[ $classmap[ $sub ] ] ) ) { |
| 82 |
/** @var Code_Snippets_Admin_Menu $class */ |
| 83 |
$class = code_snippets()->admin->menus[ $classmap[ $sub ] ]; |
| 84 |
} else { |
| 85 |
$class = $this; |
| 86 |
} |
| 87 |
|
| 88 |
/* Add a submenu to the Tools menu */ |
| 89 |
$hook = add_submenu_page( |
| 90 |
'tools.php', |
| 91 |
__( 'Snippets', 'code-snippets' ), |
| 92 |
_x( 'Snippets', 'tools submenu label', 'code-snippets' ), |
| 93 |
code_snippets()->get_cap(), |
| 94 |
code_snippets()->get_menu_slug(), |
| 95 |
array( $class, 'render' ) |
| 96 |
); |
| 97 |
|
| 98 |
add_action( 'load-' . $hook, array( $class, 'load' ) ); |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Executed when the admin page is loaded |
| 104 |
*/ |
| 105 |
function load() { |
| 106 |
parent::load(); |
| 107 |
|
| 108 |
/* Load the contextual help tabs */ |
| 109 |
$contextual_help = new Code_Snippets_Contextual_Help( 'manage' ); |
| 110 |
$contextual_help->load(); |
| 111 |
|
| 112 |
/* Initialize the list table class */ |
| 113 |
$this->list_table = new Code_Snippets_List_Table(); |
| 114 |
$this->list_table->prepare_items(); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Enqueue scripts and stylesheets for the admin page |
| 119 |
*/ |
| 120 |
public function enqueue_assets() { |
| 121 |
$plugin = code_snippets(); |
| 122 |
$rtl = is_rtl() ? '-rtl' : ''; |
| 123 |
|
| 124 |
wp_enqueue_style( |
| 125 |
'code-snippets-manage', |
| 126 |
plugins_url( "css/min/manage{$rtl}.css", $plugin->file ), |
| 127 |
array(), $plugin->version |
| 128 |
); |
| 129 |
|
| 130 |
wp_enqueue_script( |
| 131 |
'code-snippets-manage-js', |
| 132 |
plugins_url( 'js/min/manage.js', $plugin->file ), |
| 133 |
array(), $plugin->version, true |
| 134 |
); |
| 135 |
|
| 136 |
wp_localize_script( |
| 137 |
'code-snippets-manage-js', |
| 138 |
'code_snippets_manage_i18n', |
| 139 |
array( |
| 140 |
'activate' => __( 'Activate', 'code-snippets' ), |
| 141 |
'deactivate' => __( 'Deactivate', 'code-snippets' ), |
| 142 |
'activation_error' => __( 'An error occurred when attempting to activate', 'code-snippets' ), |
| 143 |
) |
| 144 |
); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Print the status and error messages |
| 149 |
*/ |
| 150 |
protected function print_messages() { |
| 151 |
|
| 152 |
/* Output a warning if safe mode is active */ |
| 153 |
if ( defined( 'CODE_SNIPPETS_SAFE_MODE' ) && CODE_SNIPPETS_SAFE_MODE ) { |
| 154 |
echo '<div id="message" class="error fade"><p>'; |
| 155 |
_e( '<strong>Warning:</strong> Safe mode is active and snippets will not execute! Remove the <code>CODE_SNIPPETS_SAFE_MODE</code> constant from <code>wp-config.php</code> to turn off safe mode. <a href="https://github.com/sheabunge/code-snippets/wiki/Safe-Mode" target="_blank">Help</a>', 'code-snippets' ); |
| 156 |
echo '</p></div>'; |
| 157 |
} |
| 158 |
|
| 159 |
echo $this->get_result_message( |
| 160 |
array( |
| 161 |
'executed' => __( 'Snippet <strong>executed</strong>.', 'code-snippets' ), |
| 162 |
'activated' => __( 'Snippet <strong>activated</strong>.', 'code-snippets' ), |
| 163 |
'activated-multi' => __( 'Selected snippets <strong>activated</strong>.', 'code-snippets' ), |
| 164 |
'deactivated' => __( 'Snippet <strong>deactivated</strong>.', 'code-snippets' ), |
| 165 |
'deactivated-multi' => __( 'Selected snippets <strong>deactivated</strong>.', 'code-snippets' ), |
| 166 |
'deleted' => __( 'Snippet <strong>deleted</strong>.', 'code-snippets' ), |
| 167 |
'deleted-multi' => __( 'Selected snippets <strong>deleted</strong>.', 'code-snippets' ), |
| 168 |
'cloned' => __( 'Snippet <strong>cloned</strong>.', 'code-snippets' ), |
| 169 |
'cloned-multi' => __( 'Selected snippets <strong>cloned</strong>.', 'code-snippets' ), |
| 170 |
) |
| 171 |
); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Handles saving the user's snippets per page preference |
| 176 |
* |
| 177 |
* @param mixed $status |
| 178 |
* @param string $option The screen option name |
| 179 |
* @param mixed $value |
| 180 |
* |
| 181 |
* @return mixed |
| 182 |
*/ |
| 183 |
function save_screen_option( $status, $option, $value ) { |
| 184 |
if ( 'snippets_per_page' === $option ) { |
| 185 |
return $value; |
| 186 |
} |
| 187 |
|
| 188 |
return $status; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Handle AJAX requests |
| 193 |
*/ |
| 194 |
public function ajax_callback() { |
| 195 |
check_ajax_referer( 'code_snippets_manage_ajax' ); |
| 196 |
|
| 197 |
if ( ! isset( $_POST['field'], $_POST['snippet'] ) ) { |
| 198 |
wp_send_json_error( array( |
| 199 |
'type' => 'param_error', |
| 200 |
'message' => 'incomplete request', |
| 201 |
) ); |
| 202 |
} |
| 203 |
|
| 204 |
$snippet_data = json_decode( stripslashes( $_POST['snippet'] ), true ); |
| 205 |
|
| 206 |
$snippet = new Code_Snippet( $snippet_data ); |
| 207 |
$field = $_POST['field']; |
| 208 |
|
| 209 |
if ( 'priority' === $field ) { |
| 210 |
|
| 211 |
if ( ! isset( $snippet_data['priority'] ) || ! is_numeric( $snippet_data['priority'] ) ) { |
| 212 |
wp_send_json_error( array( |
| 213 |
'type' => 'param_error', |
| 214 |
'message' => 'missing snippet priority data', |
| 215 |
) ); |
| 216 |
} |
| 217 |
|
| 218 |
global $wpdb; |
| 219 |
|
| 220 |
$wpdb->update( |
| 221 |
code_snippets()->db->get_table_name( $snippet->network ), |
| 222 |
array( 'priority' => $snippet->priority ), |
| 223 |
array( 'id' => $snippet->id ), |
| 224 |
array( '%d' ), |
| 225 |
array( '%d' ) |
| 226 |
); |
| 227 |
|
| 228 |
} elseif ( 'active' === $field ) { |
| 229 |
|
| 230 |
if ( ! isset( $snippet_data['active'] ) ) { |
| 231 |
wp_send_json_error( array( |
| 232 |
'type' => 'param_error', |
| 233 |
'message' => 'missing snippet active data', |
| 234 |
) ); |
| 235 |
} |
| 236 |
|
| 237 |
if ( $snippet->shared_network ) { |
| 238 |
$active_shared_snippets = get_option( 'active_shared_network_snippets', array() ); |
| 239 |
|
| 240 |
if ( in_array( $snippet->id, $active_shared_snippets, true ) !== $snippet->active ) { |
| 241 |
|
| 242 |
$active_shared_snippets = $snippet->active ? |
| 243 |
array_merge( $active_shared_snippets, array( $snippet->id ) ) : |
| 244 |
array_diff( $active_shared_snippets, array( $snippet->id ) ); |
| 245 |
|
| 246 |
update_option( 'active_shared_network_snippets', $active_shared_snippets ); |
| 247 |
} |
| 248 |
} else { |
| 249 |
|
| 250 |
if ( $snippet->active ) { |
| 251 |
$result = activate_snippet( $snippet->id, $snippet->network ); |
| 252 |
if ( ! $result ) { |
| 253 |
wp_send_json_error( array( |
| 254 |
'type' => 'action_error', |
| 255 |
'message' => 'error activating snippet', |
| 256 |
) ); |
| 257 |
} |
| 258 |
} else { |
| 259 |
deactivate_snippet( $snippet->id, $snippet->network ); |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
wp_send_json_success(); |
| 265 |
} |
| 266 |
} |
| 267 |
|