PluginProbe
Code Snippets / 2.14.4
Code Snippets v2.14.4
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 2.14.4, at php/admin-menus/class-manage-menu.php

280 lines 7.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 public 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'] ) ? sanitize_key( $_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 public 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(),
128 $plugin->version
129 );
130
131 wp_enqueue_script(
132 'code-snippets-manage-js',
133 plugins_url( 'js/min/manage.js', $plugin->file ),
134 array(),
135 $plugin->version,
136 true
137 );
138
139 wp_localize_script(
140 'code-snippets-manage-js',
141 'code_snippets_manage_i18n',
142 array(
143 'activate' => __( 'Activate', 'code-snippets' ),
144 'deactivate' => __( 'Deactivate', 'code-snippets' ),
145 'activation_error' => __( 'An error occurred when attempting to activate', 'code-snippets' ),
146 )
147 );
148 }
149
150 /**
151 * Print the status and error messages
152 */
153 protected function print_messages() {
154
155 /* Output a warning if safe mode is active */
156 if ( defined( 'CODE_SNIPPETS_SAFE_MODE' ) && CODE_SNIPPETS_SAFE_MODE ) {
157 echo '<div id="message" class="error fade"><p>';
158 echo wp_kses_post(
159 __( '<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' ),
160 );
161 echo '</p></div>';
162 }
163
164 $this->print_result_message(
165 array(
166 'executed' => __( 'Snippet <strong>executed</strong>.', 'code-snippets' ),
167 'activated' => __( 'Snippet <strong>activated</strong>.', 'code-snippets' ),
168 'activated-multi' => __( 'Selected snippets <strong>activated</strong>.', 'code-snippets' ),
169 'deactivated' => __( 'Snippet <strong>deactivated</strong>.', 'code-snippets' ),
170 'deactivated-multi' => __( 'Selected snippets <strong>deactivated</strong>.', 'code-snippets' ),
171 'deleted' => __( 'Snippet <strong>deleted</strong>.', 'code-snippets' ),
172 'deleted-multi' => __( 'Selected snippets <strong>deleted</strong>.', 'code-snippets' ),
173 'cloned' => __( 'Snippet <strong>cloned</strong>.', 'code-snippets' ),
174 'cloned-multi' => __( 'Selected snippets <strong>cloned</strong>.', 'code-snippets' ),
175 )
176 );
177 }
178
179 /**
180 * Handles saving the user's snippets per page preference
181 *
182 * @param mixed $status
183 * @param string $option The screen option name
184 * @param mixed $value
185 *
186 * @return mixed
187 */
188 public function save_screen_option( $status, $option, $value ) {
189 if ( 'snippets_per_page' === $option ) {
190 return $value;
191 }
192
193 return $status;
194 }
195
196 /**
197 * Handle AJAX requests
198 */
199 public function ajax_callback() {
200 check_ajax_referer( 'code_snippets_manage_ajax' );
201
202 if ( ! isset( $_POST['field'], $_POST['snippet'] ) ) {
203 wp_send_json_error(
204 array(
205 'type' => 'param_error',
206 'message' => 'incomplete request',
207 )
208 );
209 }
210
211 $snippet_data = map_deep( json_decode( stripslashes( $_POST['snippet'] ), true ), 'sanitize_text_field' );
212
213 $snippet = new Code_Snippet( $snippet_data );
214 $field = sanitize_key( $_POST['field'] );
215
216 if ( 'priority' === $field ) {
217
218 if ( ! isset( $snippet_data['priority'] ) || ! is_numeric( $snippet_data['priority'] ) ) {
219 wp_send_json_error(
220 array(
221 'type' => 'param_error',
222 'message' => 'missing snippet priority data',
223 )
224 );
225 }
226
227 global $wpdb;
228
229 $wpdb->update(
230 code_snippets()->db->get_table_name( $snippet->network ),
231 array( 'priority' => $snippet->priority ),
232 array( 'id' => $snippet->id ),
233 array( '%d' ),
234 array( '%d' )
235 );
236
237 } elseif ( 'active' === $field ) {
238
239 if ( ! isset( $snippet_data['active'] ) ) {
240 wp_send_json_error(
241 array(
242 'type' => 'param_error',
243 'message' => 'missing snippet active data',
244 )
245 );
246 }
247
248 if ( $snippet->shared_network ) {
249 $active_shared_snippets = get_option( 'active_shared_network_snippets', array() );
250
251 if ( in_array( $snippet->id, $active_shared_snippets, true ) !== $snippet->active ) {
252
253 $active_shared_snippets = $snippet->active ?
254 array_merge( $active_shared_snippets, array( $snippet->id ) ) :
255 array_diff( $active_shared_snippets, array( $snippet->id ) );
256
257 update_option( 'active_shared_network_snippets', $active_shared_snippets );
258 }
259 } else {
260
261 if ( $snippet->active ) {
262 $result = activate_snippet( $snippet->id, $snippet->network );
263 if ( ! $result ) {
264 wp_send_json_error(
265 array(
266 'type' => 'action_error',
267 'message' => 'error activating snippet',
268 )
269 );
270 }
271 } else {
272 deactivate_snippet( $snippet->id, $snippet->network );
273 }
274 }
275 }
276
277 wp_send_json_success();
278 }
279 }
280