PluginProbe
Code Snippets / 3.6.6
Code Snippets v3.6.6
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 3.0.0 3.0.1 All 64 releases
code-snippets / php / admin-menus / class-manage-menu.php

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

374 lines 11.1 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 Code_Snippets\Cloud\Cloud_Search_List_Table;
6 use function Code_Snippets\Settings\get_setting;
7
8 /**
9 * This class handles the manage snippets menu
10 *
11 * @since 2.4.0
12 * @package Code_Snippets
13 */
14 class Manage_Menu extends Admin_Menu {
15
16 /**
17 * Holds the list table class
18 *
19 * @var List_Table
20 */
21 public List_Table $list_table;
22
23 /**
24 * Instance of the cloud list table class for search results.
25 *
26 * @var Cloud_Search_List_Table
27 */
28 public Cloud_Search_List_Table $cloud_search_list_table;
29
30 /**
31 * Class constructor
32 */
33 public function __construct() {
34
35 parent::__construct(
36 'manage',
37 _x( 'All Snippets', 'menu label', 'code-snippets' ),
38 __( 'Snippets', 'code-snippets' )
39 );
40 }
41
42 /**
43 * Register action and filter hooks
44 */
45 public function run() {
46 parent::run();
47
48 if ( code_snippets()->is_compact_menu() ) {
49 add_action( 'admin_menu', array( $this, 'register_compact_menu' ), 2 );
50 add_action( 'network_admin_menu', array( $this, 'register_compact_menu' ), 2 );
51 }
52
53 add_action( 'admin_menu', array( $this, 'register_upgrade_menu' ), 500 );
54 add_filter( 'set-screen-option', array( $this, 'save_screen_option' ), 10, 3 );
55 add_action( 'wp_ajax_update_code_snippet', array( $this, 'ajax_callback' ) );
56 }
57
58 /**
59 * Register the top-level 'Snippets' menu and associated 'Manage' subpage
60 */
61 public function register() {
62 $icon_xml = '<svg xmlns="http://www.w3.org/2000/svg" width="1024" height="1024"><path fill="transparent" d="M191.968 464.224H350.88c23.68 0 42.656 19.2 42.656 42.656 0 11.488-4.48 21.984-11.968 29.632l.192.448-108.768 108.736c-75.104 75.136-75.104 196.512 0 271.584 74.88 74.848 196.448 74.848 271.552 0 74.88-75.104 74.88-196.48 0-271.584-21.76-21.504-47.36-37.12-74.464-46.272l28.608-28.576h365.248c87.04 0 157.856-74.016 159.968-166.4 0-1.472.224-2.752 0-4.256-2.112-23.904-22.368-42.656-46.912-42.656h-264.96L903.36 166.208c17.504-17.504 18.56-45.024 3.2-63.36-1.024-1.28-2.08-2.144-3.2-3.2-66.528-63.552-169.152-65.92-230.56-4.48L410.432 357.536h-46.528c12.8-25.6 20.032-54.624 20.032-85.344 0-106.016-85.952-192-192-192-106.016 0-191.968 85.984-191.968 192 .032 106.08 85.984 192.032 192 192.032zm85.344-191.968c0 47.136-38.176 85.344-85.344 85.344-47.136 0-85.312-38.176-85.312-85.344s38.176-85.344 85.312-85.344c47.168 0 85.344 38.208 85.344 85.344zm191.776 449.056c33.28 33.248 33.28 87.264 0 120.512-33.28 33.472-87.264 33.472-120.736 0-33.28-33.248-33.28-87.264 0-120.512 33.472-33.504 87.456-33.504 120.736 0z"/></svg>';
63 // phpcs:disable WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
64 $encoded_icon = base64_encode( $icon_xml );
65
66 // Register the top-level menu.
67 add_menu_page(
68 __( 'Snippets', 'code-snippets' ),
69 _x( 'Snippets', 'top-level menu label', 'code-snippets' ),
70 code_snippets()->get_cap(),
71 code_snippets()->get_menu_slug(),
72 array( $this, 'render' ),
73 "data:image/svg+xml;base64,$encoded_icon",
74 apply_filters( 'code_snippets/admin/menu_position', is_network_admin() ? 21 : 67 )
75 );
76
77 // Register the sub-menu.
78 parent::register();
79 }
80
81 /**
82 * Register the 'upgrade' menu item.
83 *
84 * @return void
85 */
86 public function register_upgrade_menu() {
87 if ( get_setting( 'general', 'hide_upgrade_menu' ) ) {
88 return;
89 }
90
91 $menu_title = sprintf(
92 '<span class="button button-primary code-snippets-upgrade-button">%s %s</span>',
93 _x( 'Go Pro', 'top-level menu label', 'code-snippets' ),
94 '<span class="dashicons dashicons-external"></span>'
95 );
96
97 $hook = add_submenu_page(
98 code_snippets()->get_menu_slug(),
99 __( 'Upgrade to Pro', 'code-snippets' ),
100 $menu_title,
101 code_snippets()->get_cap(),
102 'code_snippets_upgrade',
103 '__return_empty_string',
104 100
105 );
106
107 add_action( "load-$hook", [ $this, 'load_upgrade_menu' ] );
108 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_menu_button_css' ] );
109 }
110
111 /**
112 * Print CSS required for the upgrade button.
113 *
114 * @return void
115 */
116 public function enqueue_menu_button_css() {
117 wp_enqueue_style(
118 'code-snippets-menu-button',
119 plugins_url( 'dist/menu-button.css', PLUGIN_FILE ),
120 [],
121 PLUGIN_VERSION
122 );
123 }
124
125 /**
126 * Redirect the user upon opening the upgrade menu.
127 *
128 * @return void
129 */
130 public function load_upgrade_menu() {
131 wp_safe_redirect( 'https://snipco.de/JE2f' );
132 exit;
133 }
134
135 /**
136 * Add menu pages for the compact menu
137 */
138 public function register_compact_menu() {
139
140 if ( ! code_snippets()->is_compact_menu() ) {
141 return;
142 }
143
144 $sub = code_snippets()->get_menu_slug( isset( $_GET['sub'] ) ? sanitize_key( $_GET['sub'] ) : 'snippets' );
145
146 $classmap = array(
147 'snippets' => 'manage',
148 'add-snippet' => 'edit',
149 'edit-snippet' => 'edit',
150 'import-code-snippets' => 'import',
151 'snippets-settings' => 'settings',
152 );
153
154 $menus = code_snippets()->admin->menus;
155 $class = isset( $classmap[ $sub ], $menus[ $classmap[ $sub ] ] ) ? $menus[ $classmap[ $sub ] ] : $this;
156
157 /* Add a submenu to the Tools menu */
158 $hook = add_submenu_page(
159 'tools.php',
160 __( 'Snippets', 'code-snippets' ),
161 _x( 'Snippets', 'tools submenu label', 'code-snippets' ),
162 code_snippets()->get_cap(),
163 code_snippets()->get_menu_slug(),
164 array( $class, 'render' )
165 );
166
167 add_action( 'load-' . $hook, array( $class, 'load' ) );
168 }
169
170 /**
171 * Executed when the admin page is loaded
172 */
173 public function load() {
174 parent::load();
175
176 $contextual_help = new Contextual_Help( 'manage' );
177 $contextual_help->load();
178
179 $this->cloud_search_list_table = new Cloud_Search_List_Table();
180 $this->cloud_search_list_table->prepare_items();
181
182 $this->list_table = new List_Table();
183 $this->list_table->prepare_items();
184 }
185
186 /**
187 * Enqueue scripts and stylesheets for the admin page
188 */
189 public function enqueue_assets() {
190 $plugin = code_snippets();
191 $rtl = is_rtl() ? '-rtl' : '';
192
193 wp_enqueue_style(
194 'code-snippets-manage',
195 plugins_url( "dist/manage$rtl.css", $plugin->file ),
196 [],
197 $plugin->version
198 );
199
200 wp_enqueue_script(
201 'code-snippets-manage-js',
202 plugins_url( 'dist/manage.js', $plugin->file ),
203 [ 'wp-i18n' ],
204 $plugin->version,
205 true
206 );
207
208 wp_set_script_translations( 'code-snippets-manage-js', 'code-snippets' );
209
210 if ( 'cloud' === $this->get_current_type() || 'cloud_search' === $this->get_current_type() ) {
211 Front_End::enqueue_all_prism_themes();
212 }
213 }
214
215 /**
216 * Get the currently displayed snippet type.
217 *
218 * @return string
219 */
220 protected function get_current_type(): string {
221 $types = Plugin::get_types();
222 $current_type = isset( $_GET['type'] ) ? sanitize_key( wp_unslash( $_GET['type'] ) ) : 'all';
223 return isset( $types[ $current_type ] ) ? $current_type : 'all';
224 }
225
226 /**
227 * Display a Go Pro badge.
228 *
229 * @return void
230 */
231 public function print_pro_message() {
232 echo '<span class="go-pro-badge">', esc_html_x( 'Pro', 'go pro badge', 'code-snippets' ), '</span>';
233 }
234
235 /**
236 * Print the status and error messages
237 */
238 protected function print_messages() {
239 // Output a warning if safe mode is active.
240 if ( defined( 'CODE_SNIPPETS_SAFE_MODE' ) && CODE_SNIPPETS_SAFE_MODE ) {
241 echo '<div id="message" class="error fade"><p>';
242 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' ) );
243 echo '</p></div>';
244 }
245
246 $this->print_result_message(
247 apply_filters(
248 'code_snippets/manage/result_messages',
249 array(
250 'executed' => __( 'Snippet <strong>executed</strong>.', 'code-snippets' ),
251 'activated' => __( 'Snippet <strong>activated</strong>.', 'code-snippets' ),
252 'activated-multi' => __( 'Selected snippets <strong>activated</strong>.', 'code-snippets' ),
253 'deactivated' => __( 'Snippet <strong>deactivated</strong>.', 'code-snippets' ),
254 'deactivated-multi' => __( 'Selected snippets <strong>deactivated</strong>.', 'code-snippets' ),
255 'deleted' => __( 'Snippet <strong>deleted</strong>.', 'code-snippets' ),
256 'deleted-multi' => __( 'Selected snippets <strong>deleted</strong>.', 'code-snippets' ),
257 'cloned' => __( 'Snippet <strong>cloned</strong>.', 'code-snippets' ),
258 'cloned-multi' => __( 'Selected snippets <strong>cloned</strong>.', 'code-snippets' ),
259 )
260 )
261 );
262 }
263
264 /**
265 * Handles saving the user's snippets per page preference
266 *
267 * @param mixed $status Current screen option status.
268 * @param string $option The screen option name.
269 * @param mixed $value Screen option value.
270 *
271 * @return mixed
272 */
273 public function save_screen_option( $status, string $option, $value ) {
274 return 'snippets_per_page' === $option ? $value : $status;
275 }
276
277 /**
278 * Update the priority value for a snippet.
279 *
280 * @param Snippet $snippet Snippet to update.
281 *
282 * @return void
283 */
284 private function update_snippet_priority( Snippet $snippet ) {
285 global $wpdb;
286 $table = code_snippets()->db->get_table_name( $snippet->network );
287
288 $wpdb->update(
289 $table,
290 array( 'priority' => $snippet->priority ),
291 array( 'id' => $snippet->id ),
292 array( '%d' ),
293 array( '%d' )
294 );
295
296 clean_snippets_cache( $table );
297 }
298
299 /**
300 * Handle AJAX requests
301 */
302 public function ajax_callback() {
303 check_ajax_referer( 'code_snippets_manage_ajax' );
304
305 if ( ! isset( $_POST['field'], $_POST['snippet'] ) ) {
306 wp_send_json_error(
307 array(
308 'type' => 'param_error',
309 'message' => 'incomplete request',
310 )
311 );
312 }
313
314 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
315 $snippet_data = array_map( 'sanitize_text_field', json_decode( wp_unslash( $_POST['snippet'] ), true ) );
316
317 $snippet = new Snippet( $snippet_data );
318 $field = sanitize_key( $_POST['field'] );
319
320 if ( 'priority' === $field ) {
321
322 if ( ! isset( $snippet_data['priority'] ) || ! is_numeric( $snippet_data['priority'] ) ) {
323 wp_send_json_error(
324 array(
325 'type' => 'param_error',
326 'message' => 'missing snippet priority data',
327 )
328 );
329 }
330
331 $this->update_snippet_priority( $snippet );
332
333 } elseif ( 'active' === $field ) {
334
335 if ( ! isset( $snippet_data['active'] ) ) {
336 wp_send_json_error(
337 array(
338 'type' => 'param_error',
339 'message' => 'missing snippet active data',
340 )
341 );
342 }
343
344 if ( $snippet->shared_network ) {
345 $active_shared_snippets = get_option( 'active_shared_network_snippets', array() );
346
347 if ( in_array( $snippet->id, $active_shared_snippets, true ) !== $snippet->active ) {
348
349 $active_shared_snippets = $snippet->active ?
350 array_merge( $active_shared_snippets, array( $snippet->id ) ) :
351 array_diff( $active_shared_snippets, array( $snippet->id ) );
352
353 update_option( 'active_shared_network_snippets', $active_shared_snippets );
354 clean_active_snippets_cache( code_snippets()->db->ms_table );
355 }
356 } elseif ( $snippet->active ) {
357 $result = activate_snippet( $snippet->id, $snippet->network );
358 if ( is_string( $result ) ) {
359 wp_send_json_error(
360 array(
361 'type' => 'action_error',
362 'message' => $result,
363 )
364 );
365 }
366 } else {
367 deactivate_snippet( $snippet->id, $snippet->network );
368 }
369 }
370
371 wp_send_json_success();
372 }
373 }
374