PluginProbe
Code Snippets / 3.0.0
Code Snippets v3.0.0
4.0.0-beta.3 4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 All 66 releases
code-snippets / trunk / php / admin-menus / class-manage-menu.php

class-manage-menu.php in Code Snippets 3.0.0, at trunk/php/admin-menus/class-manage-menu.php

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