PluginProbe
Code Snippets / 4.0.0-beta.1
Code Snippets v4.0.0-beta.1
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 3.0.0 All 65 releases
code-snippets / php / admin-menus / class-manage-menu.php

class-manage-menu.php in Code Snippets 4.0.0-beta.1, at php/admin-menus/class-manage-menu.php

338 lines 8.3 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 * Instance of 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 parent::__construct(
35 'manage',
36 _x( 'All Snippets', 'menu label', 'code-snippets' ),
37 __( 'Snippets', 'code-snippets' )
38 );
39 }
40
41 /**
42 * Register action and filter hooks
43 */
44 public function run() {
45 parent::run();
46
47 if ( code_snippets()->is_compact_menu() ) {
48 add_action( 'admin_menu', array( $this, 'register_compact_menu' ), 2 );
49 add_action( 'network_admin_menu', array( $this, 'register_compact_menu' ), 2 );
50 }
51
52 add_action( 'admin_menu', array( $this, 'register_upgrade_menu' ), 500 );
53 add_filter( 'set-screen-option', array( $this, 'save_screen_option' ), 10, 3 );
54 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_menu_css' ] );
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 add_menu_page(
63 __( 'Snippets', 'code-snippets' ),
64 _x( 'Snippets', 'top-level menu label', 'code-snippets' ),
65 code_snippets()->get_cap(),
66 code_snippets()->get_menu_slug(),
67 array( $this, 'render' ),
68 'none', // Added through CSS as a mask to prevent loading 'blinking'.
69 apply_filters( 'code_snippets/admin/menu_position', is_network_admin() ? 21 : 67 )
70 );
71
72 // Register the sub-menu.
73 parent::register();
74 }
75
76 /**
77 * Register the 'upgrade' menu item.
78 *
79 * @return void
80 */
81 public function register_upgrade_menu() {
82 if ( code_snippets()->licensing->is_licensed() || get_setting( 'general', 'hide_upgrade_menu' ) ) {
83 return;
84 }
85
86 $menu_title = sprintf(
87 '<span class="button button-primary code-snippets-upgrade-button">%s %s</span>',
88 _x( 'Go Pro', 'top-level menu label', 'code-snippets' ),
89 '<span class="dashicons dashicons-external"></span>'
90 );
91
92 $hook = add_submenu_page(
93 code_snippets()->get_menu_slug(),
94 __( 'Upgrade to Pro', 'code-snippets' ),
95 $menu_title,
96 code_snippets()->get_cap(),
97 'code_snippets_upgrade',
98 '__return_empty_string',
99 100
100 );
101
102 add_action( "load-$hook", [ $this, 'load_upgrade_menu' ] );
103 }
104
105 /**
106 * Print CSS required for the admin menu icon.
107 *
108 * @return void
109 */
110 public function enqueue_menu_css() {
111 wp_enqueue_style(
112 'code-snippets-menu',
113 plugins_url( 'dist/menu.css', PLUGIN_FILE ),
114 [],
115 PLUGIN_VERSION
116 );
117 }
118
119 /**
120 * Redirect the user upon opening the upgrade menu.
121 *
122 * @return void
123 */
124 public function load_upgrade_menu() {
125 wp_safe_redirect( 'https://snipco.de/JE2f' );
126 exit;
127 }
128
129 /**
130 * Add menu pages for the compact menu
131 */
132 public function register_compact_menu() {
133
134 if ( ! code_snippets()->is_compact_menu() ) {
135 return;
136 }
137
138 $sub = code_snippets()->get_menu_slug( isset( $_GET['sub'] ) ? sanitize_key( $_GET['sub'] ) : 'snippets' );
139
140 $classmap = array(
141 'snippets' => 'manage',
142 'add-snippet' => 'edit',
143 'edit-snippet' => 'edit',
144 'import-code-snippets' => 'import',
145 'snippets-settings' => 'settings',
146 );
147
148 $menus = code_snippets()->admin->menus;
149 $class = isset( $classmap[ $sub ], $menus[ $classmap[ $sub ] ] ) ? $menus[ $classmap[ $sub ] ] : $this;
150
151 /* Add a submenu to the Tools menu */
152 $hook = add_submenu_page(
153 'tools.php',
154 __( 'Snippets', 'code-snippets' ),
155 _x( 'Snippets', 'tools submenu label', 'code-snippets' ),
156 code_snippets()->get_cap(),
157 code_snippets()->get_menu_slug(),
158 array( $class, 'render' )
159 );
160
161 add_action( 'load-' . $hook, array( $class, 'load' ) );
162 }
163
164 /**
165 * Executed when the admin page is loaded
166 */
167 public function load() {
168 parent::load();
169
170 $contextual_help = new Contextual_Help( 'manage' );
171 $contextual_help->load();
172
173 $this->cloud_search_list_table = new Cloud_Search_List_Table();
174 $this->cloud_search_list_table->prepare_items();
175
176 $this->list_table = new List_Table();
177 $this->list_table->prepare_items();
178 }
179
180 /**
181 * Enqueue scripts and stylesheets for the admin page.
182 */
183 public function enqueue_assets() {
184 $plugin = code_snippets();
185
186 wp_enqueue_style(
187 'code-snippets-manage',
188 plugins_url( 'dist/manage.css', $plugin->file ),
189 [],
190 $plugin->version
191 );
192
193 wp_enqueue_script(
194 'code-snippets-manage-js',
195 plugins_url( 'dist/manage.js', $plugin->file ),
196 [ 'wp-i18n' ],
197 $plugin->version,
198 true
199 );
200
201 wp_set_script_translations( 'code-snippets-manage-js', 'code-snippets' );
202
203 if ( 'cloud' === $this->get_current_type() || 'cloud_search' === $this->get_current_type() ) {
204 Front_End::enqueue_all_prism_themes();
205 }
206 }
207
208 /**
209 * Get the currently displayed snippet type.
210 *
211 * @return string
212 */
213 protected function get_current_type(): string {
214 $types = Plugin::get_types();
215 $current_type = isset( $_GET['type'] ) ? sanitize_key( wp_unslash( $_GET['type'] ) ) : 'all';
216 return isset( $types[ $current_type ] ) ? $current_type : 'all';
217 }
218
219 /**
220 * Print the status and error messages
221 *
222 * @return void
223 */
224 protected function print_messages() {
225 $this->render_view( 'partials/list-table-notices' );
226 }
227
228 /**
229 * Handles saving the user's snippets per page preference
230 *
231 * @param mixed $status Current screen option status.
232 * @param string $option The screen option name.
233 * @param mixed $value Screen option value.
234 *
235 * @return mixed
236 */
237 public function save_screen_option( $status, string $option, $value ) {
238 return 'snippets_per_page' === $option ? $value : $status;
239 }
240
241 /**
242 * Update the priority value for a snippet.
243 *
244 * @param Snippet $snippet Snippet to update.
245 *
246 * @return void
247 */
248 private function update_snippet_priority( Snippet $snippet ) {
249 global $wpdb;
250 $table = code_snippets()->db->get_table_name( $snippet->network );
251
252 $wpdb->update(
253 $table,
254 array( 'priority' => $snippet->priority ),
255 array( 'id' => $snippet->id ),
256 array( '%d' ),
257 array( '%d' )
258 );
259
260 clean_snippets_cache( $table );
261 }
262
263 /**
264 * Handle AJAX requests
265 */
266 public function ajax_callback() {
267 check_ajax_referer( 'code_snippets_manage_ajax' );
268
269 if ( ! isset( $_POST['field'], $_POST['snippet'] ) ) {
270 wp_send_json_error(
271 array(
272 'type' => 'param_error',
273 'message' => 'incomplete request',
274 )
275 );
276 }
277
278 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
279 $snippet_data = array_map( 'sanitize_text_field', json_decode( wp_unslash( $_POST['snippet'] ), true ) );
280
281 $snippet = new Snippet( $snippet_data );
282 $field = sanitize_key( $_POST['field'] );
283
284 if ( 'priority' === $field ) {
285
286 if ( ! isset( $snippet_data['priority'] ) || ! is_numeric( $snippet_data['priority'] ) ) {
287 wp_send_json_error(
288 array(
289 'type' => 'param_error',
290 'message' => 'missing snippet priority data',
291 )
292 );
293 }
294
295 $this->update_snippet_priority( $snippet );
296
297 } elseif ( 'active' === $field ) {
298
299 if ( ! isset( $snippet_data['active'] ) ) {
300 wp_send_json_error(
301 array(
302 'type' => 'param_error',
303 'message' => 'missing snippet active data',
304 )
305 );
306 }
307
308 if ( $snippet->shared_network ) {
309 $active_shared_snippets = get_option( 'active_shared_network_snippets', array() );
310
311 if ( in_array( $snippet->id, $active_shared_snippets, true ) !== $snippet->active ) {
312
313 $active_shared_snippets = $snippet->active ?
314 array_merge( $active_shared_snippets, array( $snippet->id ) ) :
315 array_diff( $active_shared_snippets, array( $snippet->id ) );
316
317 update_option( 'active_shared_network_snippets', $active_shared_snippets );
318 clean_active_snippets_cache( code_snippets()->db->ms_table );
319 }
320 } elseif ( $snippet->active ) {
321 $result = activate_snippet( $snippet->id, $snippet->network );
322 if ( is_string( $result ) ) {
323 wp_send_json_error(
324 array(
325 'type' => 'action_error',
326 'message' => $result,
327 )
328 );
329 }
330 } else {
331 deactivate_snippet( $snippet->id, $snippet->network );
332 }
333 }
334
335 wp_send_json_success();
336 }
337 }
338