PluginProbe
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts / 2.6.0
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts v2.6.0
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
insert-php / admin / ajax / ajax.php

ajax.php in Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts 2.6.0, at admin/ajax/ajax.php

285 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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' );
285