PluginProbe
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts / 2.7.8
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts v2.7.8
2.7.9 2.7.8 2.7.7 2.7.6 2.7.5 2.7.4 trunk 1.3 2.0.4 2.0.6 2.1.91 2.2.4 2.2.7 2.2.9 2.3.1 2.3.10 2.4.10 2.4.2 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.6.0 All 30 releases
← All changes | admin/ajax/ajax.php +328 -284 2.4.10 → 2.7.8 View file →
@@ -1,284 +1,328 @@
1 -<?php
2 -/**
3 - * Ajax requests handler
4 - *
5 - * @author Webcraftic <[email protected]>
6 - * @copyright (c) 2018 Webraftic Ltd
7 - * @version 1.0
8 - */
9 -
10 -// Exit if accessed directly
11 -if ( ! defined( 'ABSPATH' ) ) {
12 - exit;
13 -}
14 -
15 -/**
16 - * Returns a list of available roles.
17 - */
18 -function wbcr_inp_ajax_get_user_roles() {
19 - global $wp_roles;
20 -
21 - if ( ! WINP_Plugin::app()->currentUserCan() ) {
22 - wp_die( - 1, 403 );
23 - }
24 -
25 - $snippet_id = WINP_Plugin::app()->request->post( 'snippet_id', 0, 'intval' );
26 -
27 - check_admin_referer( 'wbcr_inp_snippet_' . $snippet_id . '_conditions_metabox' );
28 -
29 - $roles = $wp_roles->roles;
30 -
31 - $values = [];
32 - foreach ( $roles as $role_id => $role ) {
33 - $values[] = [
34 - 'value' => $role_id,
35 - 'title' => $role['name'],
36 - ];
37 - }
38 -
39 - $values[] = [
40 - 'value' => 'guest',
41 - 'title' => __( 'Guest', 'insert-php' ),
42 - ];
43 -
44 - $result = [
45 - 'values' => $values,
46 - ];
47 -
48 - echo json_encode( $result );
49 - exit;
50 -}
51 -
52 -add_action( 'wp_ajax_wbcr_inp_ajax_get_user_roles', 'wbcr_inp_ajax_get_user_roles' );
53 -
54 -/**
55 - * Returns a list of public post types.
56 - */
57 -function wbcr_inp_ajax_get_post_types() {
58 -
59 - if ( ! WINP_Plugin::app()->currentUserCan() ) {
60 - wp_die( - 1, 403 );
61 - }
62 -
63 - $snippet_id = WINP_Plugin::app()->request->post( 'snippet_id', 0, 'intval' );
64 -
65 - check_admin_referer( 'wbcr_inp_snippet_' . $snippet_id . '_conditions_metabox' );
66 -
67 - $values = [];
68 - $post_types = get_post_types( [ 'public' => true ], 'objects' );
69 - if ( ! empty( $post_types ) ) {
70 - foreach ( $post_types as $key => $value ) {
71 - $values[] = [
72 - 'value' => $key,
73 - 'title' => $value->label,
74 - ];
75 - }
76 - }
77 -
78 - $result = [
79 - 'values' => $values,
80 - ];
81 -
82 - echo json_encode( $result );
83 - exit;
84 -}
85 -
86 -add_action( 'wp_ajax_wbcr_inp_ajax_get_post_types', 'wbcr_inp_ajax_get_post_types' );
87 -
88 -/**
89 - * Returns a list of public taxonomies.
90 - */
91 -function wbcr_inp_ajax_get_taxonomies() {
92 -
93 - if ( ! WINP_Plugin::app()->currentUserCan() ) {
94 - wp_die( - 1, 403 );
95 - }
96 -
97 - $snippet_id = WINP_Plugin::app()->request->post( 'snippet_id', 0, 'intval' );
98 -
99 - check_admin_referer( 'wbcr_inp_snippet_' . $snippet_id . '_conditions_metabox' );
100 -
101 - $values = [];
102 - $categories = get_categories( [ 'hide_empty' => false ] );
103 -
104 - if ( ! empty( $categories ) ) {
105 - foreach ( $categories as $cat ) {
106 - $values[] = [
107 - 'value' => $cat->term_id,
108 - 'title' => $cat->name,
109 - ];
110 - }
111 - }
112 -
113 - $result = [
114 - 'values' => $values,
115 - ];
116 -
117 - echo json_encode( $result );
118 - exit;
119 -}
120 -
121 -add_action( 'wp_ajax_wbcr_inp_ajax_get_taxonomies', 'wbcr_inp_ajax_get_taxonomies' );
122 -
123 -/**
124 - * Returns a list of page list values
125 - */
126 -function wbcr_inp_ajax_get_page_list() {
127 -
128 - if ( ! WINP_Plugin::app()->currentUserCan() ) {
129 - wp_die( - 1, 403 );
130 - }
131 -
132 - $snippet_id = WINP_Plugin::app()->request->post( 'snippet_id', 0, 'intval' );
133 -
134 - check_admin_referer( 'wbcr_inp_snippet_' . $snippet_id . '_conditions_metabox' );
135 -
136 - $is_woo = WINP_Helper::is_woo_active();
137 - $woo_desc = $is_woo ? '' : __( '(not active)', 'insert-php' );
138 -
139 - $result = [
140 - 'values' => [
141 - __( 'Basic', 'insert-php' ) => [
142 - [
143 - 'value' => 'base_web',
144 - 'title' => __( 'Entire Website', 'insert-php' ),
145 - ],
146 - [
147 - 'value' => 'base_sing',
148 - 'title' => __( 'All Singulars', 'insert-php' ),
149 - ],
150 - [
151 - 'value' => 'base_arch',
152 - 'title' => __( 'All Archives', 'insert-php' ),
153 - ],
154 - ],
155 - __( 'Special Pages', 'insert-php' ) => [
156 - [
157 - 'value' => 'spec_404',
158 - 'title' => __( '404 Page', 'insert-php' ),
159 - ],
160 - [
161 - 'value' => 'spec_search',
162 - 'title' => __( 'Search Page', 'insert-php' ),
163 - ],
164 - [
165 - 'value' => 'spec_blog',
166 - 'title' => __( 'Blog / Posts Page', 'insert-php' ),
167 - ],
168 - [
169 - 'value' => 'spec_front',
170 - 'title' => __( 'Front Page', 'insert-php' ),
171 - ],
172 - [
173 - 'value' => 'spec_date',
174 - 'title' => __( 'Date Archive', 'insert-php' ),
175 - ],
176 - [
177 - 'value' => 'spec_auth',
178 - 'title' => __( 'Author Archive', 'insert-php' ),
179 - ],
180 - ],
181 - __( 'Posts', 'insert-php' ) => [
182 - [
183 - 'value' => 'post_all',
184 - 'title' => __( 'All Posts', 'insert-php' ),
185 - ],
186 - [
187 - 'value' => 'post_arch',
188 - 'title' => __( 'All Posts Archive', 'insert-php' ),
189 - ],
190 - [
191 - 'value' => 'post_cat',
192 - 'title' => __( 'All Categories Archive', 'insert-php' ),
193 - ],
194 - [
195 - 'value' => 'post_tag',
196 - 'title' => __( 'All Tags Archive', 'insert-php' ),
197 - ],
198 - ],
199 - __( 'Pages', 'insert-php' ) => [
200 - [
201 - 'value' => 'page_all',
202 - 'title' => __( 'All Pages', 'insert-php' ),
203 - ],
204 - [
205 - 'value' => 'page_arch',
206 - 'title' => __( 'All Pages Archive', 'insert-php' ),
207 - ],
208 - ],
209 - __( 'Woocommerce', 'insert-php' ) . $woo_desc => [
210 - [
211 - 'value' => 'woo_product',
212 - 'title' => __( 'Product', 'insert-php' ),
213 - 'disabled' => ! $is_woo,
214 - ],
215 - [
216 - 'value' => 'woo_cart',
217 - 'title' => __( 'Cart page', 'insert-php' ),
218 - 'disabled' => ! $is_woo,
219 - ],
220 - [
221 - 'value' => 'woo_checkout',
222 - 'title' => __( 'Checkout page', 'insert-php' ),
223 - 'disabled' => ! $is_woo,
224 - ],
225 - [
226 - 'value' => 'woo_checkout_pay',
227 - 'title' => __( 'Checkout pay page', 'insert-php' ),
228 - 'disabled' => ! $is_woo,
229 - ],
230 - [
231 - 'value' => 'woo_arch',
232 - 'title' => __( 'All Products page', 'insert-php' ),
233 - 'disabled' => ! $is_woo,
234 - ],
235 - [
236 - 'value' => 'woo_cat',
237 - 'title' => __( 'Products Category page', 'insert-php' ),
238 - 'disabled' => ! $is_woo,
239 - ],
240 - [
241 - 'value' => 'woo_tag',
242 - 'title' => __( 'Products Tag page', 'insert-php' ),
243 - 'disabled' => ! $is_woo,
244 - ],
245 - ],
246 - ],
247 - ];
248 -
249 - echo json_encode( $result );
250 - exit;
251 -}
252 -
253 -add_action( 'wp_ajax_wbcr_inp_ajax_get_page_list', 'wbcr_inp_ajax_get_page_list' );
254 -
255 -/**
256 - * Save the Permalink slug
257 - */
258 -function wbcr_inp_ajax_save_permalink() {
259 -
260 - if ( ! WINP_Plugin::app()->currentUserCan() ) {
261 - wp_die( - 1, 403 );
262 - }
263 -
264 - check_ajax_referer( 'winp-permalink', 'winp_permalink_nonce' );
265 -
266 - $code_id = WINP_Plugin::app()->request->post( 'code_id', 0 );
267 - $permalink = WINP_Plugin::app()->request->post( 'permalink', null, true );
268 - $slug = WINP_Plugin::app()->request->post( 'new_slug', null, 'sanitize_file_name' );
269 - $filetype = WINP_Plugin::app()->request->post( 'filetype', 'css', true );
270 -
271 - WINP_Helper::updateMetaOption( $code_id, 'filetype', $filetype );
272 -
273 - if ( empty( $slug ) ) {
274 - $slug = (string) $code_id;
275 - WINP_Helper::updateMetaOption( $code_id, 'css_js_slug', '' );
276 - } else {
277 - WINP_Helper::updateMetaOption( $code_id, 'css_js_slug', $slug );
278 - }
279 - WINP_Plugin::app()->get_common_object()->editFormBeforePermalink( $slug, $permalink, $filetype );
280 -
281 - wp_die();
282 -}
283 -
284 -add_action( 'wp_ajax_winp_permalink', 'wbcr_inp_ajax_save_permalink' );
1 +<?php
2 +/**
3 + * Ajax requests handler
4 + *
5 + * @package Woody_Code_Snippets
6 + */
7 +
8 +// Exit if accessed directly
9 +if ( ! defined( 'ABSPATH' ) ) {
10 + exit;
11 +}
12 +
13 +/**
14 + * Returns a list of available roles.
15 + */
16 +function wbcr_inp_ajax_get_user_roles() {
17 + global $wp_roles;
18 +
19 + if ( ! WINP_Plugin::app()->current_user_car() ) {
20 + wp_die( - 1, 403 );
21 + }
22 +
23 + $snippet_id = WINP_HTTP::post( 'snippet_id', 0, 'intval' );
24 +
25 + check_admin_referer( 'wbcr_inp_snippet_' . $snippet_id . '_conditions_metabox' );
26 +
27 + $roles = $wp_roles->roles;
28 +
29 + $values = [];
30 + foreach ( $roles as $role_id => $role ) {
31 + $values[] = [
32 + 'value' => $role_id,
33 + 'title' => $role['name'],
34 + ];
35 + }
36 +
37 + $values[] = [
38 + 'value' => 'guest',
39 + 'title' => __( 'Guest', 'insert-php' ),
40 + ];
41 +
42 + $result = [
43 + 'values' => $values,
44 + ];
45 +
46 + echo json_encode( $result );
47 + exit;
48 +}
49 +
50 +add_action( 'wp_ajax_wbcr_inp_ajax_get_user_roles', 'wbcr_inp_ajax_get_user_roles' );
51 +
52 +/**
53 + * Returns a list of public post types.
54 + */
55 +function wbcr_inp_ajax_get_post_types() {
56 +
57 + if ( ! WINP_Plugin::app()->current_user_car() ) {
58 + wp_die( - 1, 403 );
59 + }
60 +
61 + $snippet_id = WINP_HTTP::post( 'snippet_id', 0, 'intval' );
62 +
63 + check_admin_referer( 'wbcr_inp_snippet_' . $snippet_id . '_conditions_metabox' );
64 +
65 + $values = [];
66 + $post_types = get_post_types( [ 'public' => true ], 'objects' );
67 + if ( ! empty( $post_types ) ) {
68 + foreach ( $post_types as $key => $value ) {
69 + $values[] = [
70 + 'value' => $key,
71 + 'title' => $value->label,
72 + ];
73 + }
74 + }
75 +
76 + $result = [
77 + 'values' => $values,
78 + ];
79 +
80 + echo json_encode( $result );
81 + exit;
82 +}
83 +
84 +add_action( 'wp_ajax_wbcr_inp_ajax_get_post_types', 'wbcr_inp_ajax_get_post_types' );
85 +
86 +/**
87 + * Returns a list of public taxonomies.
88 + */
89 +function wbcr_inp_ajax_get_taxonomies() {
90 +
91 + if ( ! WINP_Plugin::app()->current_user_car() ) {
92 + wp_die( - 1, 403 );
93 + }
94 +
95 + $snippet_id = WINP_HTTP::post( 'snippet_id', 0, 'intval' );
96 +
97 + check_admin_referer( 'wbcr_inp_snippet_' . $snippet_id . '_conditions_metabox' );
98 +
99 + $values = [];
100 + $categories = get_categories( [ 'hide_empty' => false ] );
101 +
102 + if ( ! empty( $categories ) ) {
103 + foreach ( $categories as $cat ) {
104 + $values[] = [
105 + 'value' => $cat->term_id,
106 + 'title' => $cat->name,
107 + ];
108 + }
109 + }
110 +
111 + $result = [
112 + 'values' => $values,
113 + ];
114 +
115 + echo json_encode( $result );
116 + exit;
117 +}
118 +
119 +add_action( 'wp_ajax_wbcr_inp_ajax_get_taxonomies', 'wbcr_inp_ajax_get_taxonomies' );
120 +
121 +/**
122 + * Returns a list of page list values
123 + */
124 +function wbcr_inp_ajax_get_page_list() {
125 +
126 + if ( ! WINP_Plugin::app()->current_user_car() ) {
127 + wp_die( - 1, 403 );
128 + }
129 +
130 + $snippet_id = WINP_HTTP::post( 'snippet_id', 0, 'intval' );
131 +
132 + check_admin_referer( 'wbcr_inp_snippet_' . $snippet_id . '_conditions_metabox' );
133 +
134 + $is_woo = WINP_Helper::is_woo_active();
135 + $woo_desc = $is_woo ? '' : __( '(not active)', 'insert-php' );
136 +
137 + $result = [
138 + 'values' => [
139 + __( 'Basic', 'insert-php' ) => [
140 + [
141 + 'value' => 'base_web',
142 + 'title' => __( 'Entire Website', 'insert-php' ),
143 + ],
144 + [
145 + 'value' => 'base_sing',
146 + 'title' => __( 'All Single Posts & Pages', 'insert-php' ),
147 + ],
148 + [
149 + 'value' => 'base_arch',
150 + 'title' => __( 'All Archive Pages', 'insert-php' ),
151 + ],
152 + ],
153 + __( 'Special Pages', 'insert-php' ) => [
154 + [
155 + 'value' => 'spec_404',
156 + 'title' => __( '404 Page', 'insert-php' ),
157 + ],
158 + [
159 + 'value' => 'spec_search',
160 + 'title' => __( 'Search Page', 'insert-php' ),
161 + ],
162 + [
163 + 'value' => 'spec_blog',
164 + 'title' => __( 'Blog/Posts Page', 'insert-php' ),
165 + ],
166 + [
167 + 'value' => 'spec_front',
168 + 'title' => __( 'Front Page', 'insert-php' ),
169 + ],
170 + [
171 + 'value' => 'spec_date',
172 + 'title' => __( 'Date Archive', 'insert-php' ),
173 + ],
174 + [
175 + 'value' => 'spec_auth',
176 + 'title' => __( 'Author Archive', 'insert-php' ),
177 + ],
178 + ],
179 + __( 'Posts', 'insert-php' ) => [
180 + [
181 + 'value' => 'post_all',
182 + 'title' => __( 'All Posts', 'insert-php' ),
183 + ],
184 + [
185 + 'value' => 'post_arch',
186 + 'title' => __( 'All Posts Archive', 'insert-php' ),
187 + ],
188 + [
189 + 'value' => 'post_cat',
190 + 'title' => __( 'All Categories Archive', 'insert-php' ),
191 + ],
192 + [
193 + 'value' => 'post_tag',
194 + 'title' => __( 'All Tags Archive', 'insert-php' ),
195 + ],
196 + ],
197 + __( 'Pages', 'insert-php' ) => [
198 + [
199 + 'value' => 'page_all',
200 + 'title' => __( 'All Pages', 'insert-php' ),
201 + ],
202 + [
203 + 'value' => 'page_arch',
204 + 'title' => __( 'All Pages Archive', 'insert-php' ),
205 + ],
206 + ],
207 + __( 'WooCommerce', 'insert-php' ) . $woo_desc => [
208 + [
209 + 'value' => 'woo_product',
210 + 'title' => __( 'Product', 'insert-php' ),
211 + 'disabled' => ! $is_woo,
212 + ],
213 + [
214 + 'value' => 'woo_cart',
215 + 'title' => __( 'Cart Page', 'insert-php' ),
216 + 'disabled' => ! $is_woo,
217 + ],
218 + [
219 + 'value' => 'woo_checkout',
220 + 'title' => __( 'Checkout Page', 'insert-php' ),
221 + 'disabled' => ! $is_woo,
222 + ],
223 + [
224 + 'value' => 'woo_checkout_pay',
225 + 'title' => __( 'Checkout Payment Page', 'insert-php' ),
226 + 'disabled' => ! $is_woo,
227 + ],
228 + [
229 + 'value' => 'woo_arch',
230 + 'title' => __( 'All Products Page', 'insert-php' ),
231 + 'disabled' => ! $is_woo,
232 + ],
233 + [
234 + 'value' => 'woo_cat',
235 + 'title' => __( 'Product Category Page', 'insert-php' ),
236 + 'disabled' => ! $is_woo,
237 + ],
238 + [
239 + 'value' => 'woo_tag',
240 + 'title' => __( 'Product Tag Page', 'insert-php' ),
241 + 'disabled' => ! $is_woo,
242 + ],
243 + ],
244 + ],
245 + ];
246 +
247 + echo json_encode( $result );
248 + exit;
249 +}
250 +
251 +add_action( 'wp_ajax_wbcr_inp_ajax_get_page_list', 'wbcr_inp_ajax_get_page_list' );
252 +
253 +/**
254 + * Save the Permalink slug
255 + */
256 +function wbcr_inp_ajax_save_permalink() {
257 +
258 + if ( ! WINP_Plugin::app()->current_user_car() ) {
259 + wp_die( - 1, 403 );
260 + }
261 +
262 + check_ajax_referer( 'winp-permalink', 'winp_permalink_nonce' );
263 +
264 + $code_id = WINP_HTTP::post( 'code_id', 0 );
265 + $permalink = WINP_HTTP::post( 'permalink', null, true );
266 + $slug = WINP_HTTP::post( 'new_slug', null, 'sanitize_file_name' );
267 + $filetype = WINP_HTTP::post( 'filetype', 'css', true );
268 +
269 + WINP_Helper::updateMetaOption( $code_id, 'filetype', $filetype );
270 +
271 + if ( empty( $slug ) ) {
272 + $slug = (string) $code_id;
273 + WINP_Helper::updateMetaOption( $code_id, 'css_js_slug', '' );
274 + } else {
275 + WINP_Helper::updateMetaOption( $code_id, 'css_js_slug', $slug );
276 + }
277 + WINP_Plugin::app()->get_common_object()->edit_form_before_permalink( $slug, $permalink, $filetype );
278 +
279 + wp_die();
280 +}
281 +
282 +add_action( 'wp_ajax_winp_permalink', 'wbcr_inp_ajax_save_permalink' );
283 +
284 +/**
285 + * Validate snippet code before saving (AJAX).
286 + *
287 + * @return void
288 + */
289 +function wbcr_inp_ajax_validate_snippet() {
290 + if ( ! WINP_Plugin::app()->current_user_car() ) {
291 + wp_send_json_error( [ 'message' => __( 'You don\'t have permission to perform this action. Contact your administrator.', 'insert-php' ) ], 403 );
292 + }
293 +
294 + $post_id = WINP_HTTP::post( 'post_id', 0, 'intval' );
295 +
296 + check_ajax_referer( 'winp_validate_snippet_' . $post_id, 'nonce' );
297 +
298 + $snippet_code = WINP_HTTP::post( 'snippet_code', '', false );
299 + $snippet_type = WINP_HTTP::post( 'snippet_type', WINP_SNIPPET_TYPE_PHP, true );
300 +
301 + // Only validate executable PHP snippets (not text, ad, css, js, html).
302 + if ( WINP_SNIPPET_TYPE_TEXT !== $snippet_type &&
303 + WINP_SNIPPET_TYPE_AD !== $snippet_type &&
304 + WINP_SNIPPET_TYPE_CSS !== $snippet_type &&
305 + WINP_SNIPPET_TYPE_JS !== $snippet_type &&
306 + WINP_SNIPPET_TYPE_HTML !== $snippet_type ) {
307 +
308 + $snippet_code = stripslashes( $snippet_code );
309 +
310 + $validation = WINP_Code_Validator::validate_code( $snippet_code, $snippet_type );
311 +
312 + if ( ! $validation['valid'] ) {
313 + wp_send_json_error(
314 + [
315 + 'valid' => false,
316 + 'message' => $validation['message'],
317 + ]
318 + );
319 + }
320 +
321 + wp_send_json_success( [ 'valid' => true ] );
322 + } else {
323 + // No validation needed for this type.
324 + wp_send_json_success( [ 'valid' => true ] );
325 + }
326 +}
327 +
328 +add_action( 'wp_ajax_wbcr_inp_ajax_validate_snippet', 'wbcr_inp_ajax_validate_snippet' );