| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ajax requests handler |
| 4 |
* |
| 5 |
* @author Webcraftic <wordpress.webraftic@gmail.com> |
| 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 |
$result = [ |
| 137 |
'values' => [ |
| 138 |
'Basic' => [ |
| 139 |
[ |
| 140 |
'value' => 'base_web', |
| 141 |
'title' => __( 'Entire Website', 'insert-php' ), |
| 142 |
], |
| 143 |
[ |
| 144 |
'value' => 'base_sing', |
| 145 |
'title' => __( 'All Singulars', 'insert-php' ), |
| 146 |
], |
| 147 |
[ |
| 148 |
'value' => 'base_arch', |
| 149 |
'title' => __( 'All Archives', 'insert-php' ), |
| 150 |
], |
| 151 |
], |
| 152 |
'Special Pages' => [ |
| 153 |
[ |
| 154 |
'value' => 'spec_404', |
| 155 |
'title' => __( '404 Page', 'insert-php' ), |
| 156 |
], |
| 157 |
[ |
| 158 |
'value' => 'spec_search', |
| 159 |
'title' => __( 'Search Page', 'insert-php' ), |
| 160 |
], |
| 161 |
[ |
| 162 |
'value' => 'spec_blog', |
| 163 |
'title' => __( 'Blog / Posts Page', 'insert-php' ), |
| 164 |
], |
| 165 |
[ |
| 166 |
'value' => 'spec_front', |
| 167 |
'title' => __( 'Front Page', 'insert-php' ), |
| 168 |
], |
| 169 |
[ |
| 170 |
'value' => 'spec_date', |
| 171 |
'title' => __( 'Date Archive', 'insert-php' ), |
| 172 |
], |
| 173 |
[ |
| 174 |
'value' => 'spec_auth', |
| 175 |
'title' => __( 'Author Archive', 'insert-php' ), |
| 176 |
], |
| 177 |
], |
| 178 |
'Posts' => [ |
| 179 |
[ |
| 180 |
'value' => 'post_all', |
| 181 |
'title' => __( 'All Posts', 'insert-php' ), |
| 182 |
], |
| 183 |
[ |
| 184 |
'value' => 'post_arch', |
| 185 |
'title' => __( 'All Posts Archive', 'insert-php' ), |
| 186 |
], |
| 187 |
[ |
| 188 |
'value' => 'post_cat', |
| 189 |
'title' => __( 'All Categories Archive', 'insert-php' ), |
| 190 |
], |
| 191 |
[ |
| 192 |
'value' => 'post_tag', |
| 193 |
'title' => __( 'All Tags Archive', 'insert-php' ), |
| 194 |
], |
| 195 |
], |
| 196 |
'Pages' => [ |
| 197 |
[ |
| 198 |
'value' => 'page_all', |
| 199 |
'title' => __( 'All Pages', 'insert-php' ), |
| 200 |
], |
| 201 |
[ |
| 202 |
'value' => 'page_arch', |
| 203 |
'title' => __( 'All Pages Archive', 'insert-php' ), |
| 204 |
], |
| 205 |
], |
| 206 |
], |
| 207 |
]; |
| 208 |
|
| 209 |
echo json_encode( $result ); |
| 210 |
exit; |
| 211 |
} |
| 212 |
|
| 213 |
add_action( 'wp_ajax_wbcr_inp_ajax_get_page_list', 'wbcr_inp_ajax_get_page_list' ); |
| 214 |
|
| 215 |
/** |
| 216 |
* Save the Permalink slug |
| 217 |
*/ |
| 218 |
function wbcr_inp_ajax_save_permalink() { |
| 219 |
|
| 220 |
if ( ! WINP_Plugin::app()->currentUserCan() ) { |
| 221 |
wp_die( - 1, 403 ); |
| 222 |
} |
| 223 |
|
| 224 |
check_ajax_referer( 'winp-permalink', 'winp_permalink_nonce' ); |
| 225 |
|
| 226 |
$code_id = WINP_Plugin::app()->request->post( 'code_id', 0 ); |
| 227 |
$permalink = WINP_Plugin::app()->request->post( 'permalink', null, true ); |
| 228 |
$slug = WINP_Plugin::app()->request->post( 'new_slug', null, 'sanitize_file_name' ); |
| 229 |
$filetype = WINP_Plugin::app()->request->post( 'filetype', 'css', true ); |
| 230 |
|
| 231 |
WINP_Helper::updateMetaOption( $code_id, 'filetype', $filetype ); |
| 232 |
|
| 233 |
if ( empty( $slug ) ) { |
| 234 |
$slug = (string) $code_id; |
| 235 |
WINP_Helper::updateMetaOption( $code_id, 'css_js_slug', '' ); |
| 236 |
} else { |
| 237 |
WINP_Helper::updateMetaOption( $code_id, 'css_js_slug', $slug ); |
| 238 |
} |
| 239 |
WINP_Plugin::app()->get_common_object()->editFormBeforePermalink( $slug, $permalink, $filetype ); |
| 240 |
|
| 241 |
wp_die(); |
| 242 |
} |
| 243 |
|
| 244 |
add_action( 'wp_ajax_winp_permalink', 'wbcr_inp_ajax_save_permalink' ); |
| 245 |
|