Collaboration
3 weeks ago
Apps.php
3 weeks ago
Collection.php
1 month ago
Comments.php
2 months ago
DynamicContent.php
1 month ago
ExportImport.php
3 days ago
Form.php
3 weeks ago
Media.php
3 days ago
Page.php
3 days ago
PageSettings.php
3 weeks ago
RBAC.php
3 weeks ago
Symbol.php
3 weeks ago
Taxonomy.php
2 months ago
TemplateExportImport.php
2 months ago
UserData.php
3 weeks ago
Users.php
2 months ago
Walkthrough.php
2 months ago
WordpressData.php
2 months ago
WpAdmin.php
3 days ago
PageSettings.php
311 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Single post or page kirki settings |
| 4 | * |
| 5 | * @package kirki |
| 6 | */ |
| 7 | |
| 8 | namespace Kirki\Ajax; |
| 9 | |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | exit; // Exit if accessed directly. |
| 12 | } |
| 13 | |
| 14 | use Kirki\API\ContentManager\ContentManagerHelper; |
| 15 | use Kirki\HelperFunctions; |
| 16 | |
| 17 | |
| 18 | /** |
| 19 | * PageSettings API Class |
| 20 | */ |
| 21 | class PageSettings { |
| 22 | |
| 23 | /** |
| 24 | * Save page settings data |
| 25 | * |
| 26 | * @return void wp_send_json |
| 27 | * @deprecated |
| 28 | * @see Kirki\App\Services\PageSettingsService::update_page_settings() |
| 29 | */ |
| 30 | public static function save_page_setting_data() { |
| 31 | //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 32 | $id = (int) HelperFunctions::sanitize_text( isset( $_POST['id'] ) ? $_POST['id'] : '' ); |
| 33 | //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 34 | $page_name = HelperFunctions::sanitize_text( isset( $_POST['page_title'] ) ? $_POST['page_title'] : null ); |
| 35 | //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 36 | $slug = HelperFunctions::sanitize_text( isset( $_POST['slug'] ) ? $_POST['slug'] : null ); |
| 37 | //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 38 | $post_status = HelperFunctions::sanitize_text( isset( $_POST['post_status'] ) ? $_POST['post_status'] : null ); |
| 39 | //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 40 | $page_desc = HelperFunctions::sanitize_text( isset( $_POST['page_description'] ) ? $_POST['page_description'] : '' ); |
| 41 | $featured_image_url = HelperFunctions::sanitize_text( isset( $_POST['featured_image'] ) ? $_POST['featured_image'] : '' ); |
| 42 | //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated,WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 43 | $seo_settings = json_decode( stripslashes( $_POST['seo_settings'] ), true ); |
| 44 | //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated,WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 45 | $custom_code = json_decode( stripslashes( $_POST['custom_code'] ), true ); |
| 46 | |
| 47 | $the_post = array( |
| 48 | 'ID' => $id, |
| 49 | 'post_title' => $page_name, |
| 50 | 'post_name' => $slug, |
| 51 | 'post_excerpt' => $page_desc, |
| 52 | ); |
| 53 | |
| 54 | if ( $post_status ) { |
| 55 | $the_post['post_status'] = $post_status; |
| 56 | } |
| 57 | |
| 58 | $post_id = wp_update_post( $the_post ); |
| 59 | |
| 60 | update_post_meta( $id, KIRKI_PAGE_SEO_SETTINGS_META_KEY, $seo_settings ); |
| 61 | update_post_meta( $id, KIRKI_PAGE_CUSTOM_CODE, $custom_code ); |
| 62 | |
| 63 | $image_id = attachment_url_to_postid($featured_image_url); |
| 64 | |
| 65 | if ($image_id > 0) { |
| 66 | set_post_thumbnail($post_id, (int) $image_id); |
| 67 | } else { |
| 68 | delete_post_thumbnail($post_id); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | $the_post = get_post( $post_id ); |
| 73 | $the_post_perma = get_permalink( $id ); |
| 74 | |
| 75 | wp_send_json( |
| 76 | array( |
| 77 | 'page_title' => $the_post->post_title, |
| 78 | 'slug' => $the_post->post_name, |
| 79 | 'page_description' => $the_post->post_excerpt, |
| 80 | 'post_status' => $the_post->post_status, |
| 81 | 'post_url' => str_replace( get_http_origin(), '', $the_post_perma ), |
| 82 | // NB: These are sent debugging purpose, please don't remove them without being sure. |
| 83 | 'page_full_url' => $the_post_perma, |
| 84 | 'home_url' => home_url(), |
| 85 | 'origin' => get_http_origin(), |
| 86 | 'site_url' => site_url(), |
| 87 | ) |
| 88 | ); |
| 89 | |
| 90 | die(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get page settings data |
| 95 | * |
| 96 | * @return void wp_send_json |
| 97 | */ |
| 98 | public static function get_page_settings_data() { |
| 99 | //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 100 | $post_id = (int) HelperFunctions::sanitize_text( isset( $_GET['id'] ) ? $_GET['id'] : '' ); |
| 101 | |
| 102 | $post = get_post( $post_id ); |
| 103 | $post_title = $post->post_title; |
| 104 | $slug = $post->post_name; |
| 105 | $og_image = ''; |
| 106 | $featured_img = ''; |
| 107 | $page_desc = $post->post_excerpt; |
| 108 | |
| 109 | $seo_post_data = ''; |
| 110 | $seo_settings_post_meta = get_post_meta( $post_id, KIRKI_PAGE_SEO_SETTINGS_META_KEY, true ); |
| 111 | $custom_code_post_meta = get_post_meta( $post_id, KIRKI_PAGE_CUSTOM_CODE, true ); |
| 112 | |
| 113 | if ( isset( $seo_settings_post_meta['openGraph']['openGraphImage']['value'] ) ) { |
| 114 | $og_image = $seo_settings_post_meta['openGraph']['openGraphImage']['value']; |
| 115 | } |
| 116 | |
| 117 | $featured_img_url = get_the_post_thumbnail_url( $post_id ); |
| 118 | if ( $featured_img_url ) { |
| 119 | $featured_img = $featured_img_url; |
| 120 | } |
| 121 | |
| 122 | $result = array( |
| 123 | 'page_title' => ! empty( $post_title ) ? $post_title : '', |
| 124 | 'slug' => ! empty( $slug ) ? $slug : '', |
| 125 | 'og_image' => ! empty( $og_image ) ? $og_image : '', |
| 126 | 'page_description' => ! empty( $page_desc ) ? $page_desc : '', |
| 127 | 'post_status' => $post->post_status, |
| 128 | 'seo_settings' => $seo_settings_post_meta, |
| 129 | 'custom_code' => $custom_code_post_meta, |
| 130 | 'featured_image' => empty( $featured_img ) ? '' : $featured_img, |
| 131 | ); |
| 132 | |
| 133 | $seo_post_data = self::get_seo_post_data( $post ); |
| 134 | $result['seo_post_data'] = $seo_post_data; |
| 135 | |
| 136 | wp_send_json( $result ); |
| 137 | |
| 138 | die(); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Get custom code |
| 143 | * |
| 144 | * @return void wp_send_json |
| 145 | */ |
| 146 | public static function get_custom_code() { |
| 147 | $post_id = HelperFunctions::get_post_id_if_possible_from_url(); |
| 148 | |
| 149 | $custom_code_post_meta = get_post_meta( $post_id, KIRKI_PAGE_CUSTOM_CODE, true ); |
| 150 | |
| 151 | $result = $custom_code_post_meta; |
| 152 | |
| 153 | wp_send_json( $result ); |
| 154 | |
| 155 | die(); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Save custom code |
| 160 | * |
| 161 | * @return void wp_send_json |
| 162 | * |
| 163 | * @deprecated |
| 164 | * @see Kirki\App\Services\PageSettingsService::update_page_custom_code() |
| 165 | */ |
| 166 | public static function save_custom_code() { |
| 167 | $post_id = HelperFunctions::get_post_id_if_possible_from_url(); |
| 168 | //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated,WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 169 | $custom_code = json_decode( stripslashes( $_POST['custom_code'] ), true ); |
| 170 | |
| 171 | update_post_meta( $post_id, KIRKI_PAGE_CUSTOM_CODE, $custom_code ); |
| 172 | |
| 173 | wp_send_json( true ); |
| 174 | |
| 175 | die(); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Get SEO data |
| 180 | * |
| 181 | * @param object $post post object. |
| 182 | * |
| 183 | * @return void wp_send_json |
| 184 | */ |
| 185 | private static function get_seo_post_data( $post ) { |
| 186 | $post_conditions = get_post_meta( $post->ID,'kirki_template_conditions', true ); |
| 187 | $condition = $post_conditions[0]; |
| 188 | $res = array(); |
| 189 | |
| 190 | $condition_type = isset( $condition['type'] ) ? $condition['type'] : ''; // post, user, term; |
| 191 | if ( isset( $condition['from'] ) && $condition['from'] === 'term' ) { |
| 192 | $condition_type = 'term'; |
| 193 | } |
| 194 | |
| 195 | // check if the type is post |
| 196 | if ( $condition_type == 'post' ) { |
| 197 | $res = self::get_post_type_seo_response( $post, $condition ); |
| 198 | } elseif ( $condition_type == 'user' ) { |
| 199 | $res = self::get_user_type_seo_response(); |
| 200 | } elseif ( $condition_type == 'term' ) { |
| 201 | $res = self::get_term_type_seo_response(); |
| 202 | } |
| 203 | |
| 204 | return $res; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Get SEO data for post type |
| 209 | * |
| 210 | * @param object $post post object. |
| 211 | * @param array $condition condition array. |
| 212 | * |
| 213 | * @return array |
| 214 | */ |
| 215 | private static function get_post_type_seo_response( $post, $condition ) { |
| 216 | $curr_seo_post = $post; |
| 217 | |
| 218 | if ( isset( $condition['post_type'] ) && strpos( $condition['post_type'], KIRKI_CONTENT_MANAGER_PREFIX ) !== false ) { |
| 219 | // content manager related post |
| 220 | $post_parent = str_replace( KIRKI_CONTENT_MANAGER_PREFIX . '_', '', $condition['post_type'] ); |
| 221 | |
| 222 | $args = array( |
| 223 | 'post_parent' => $post_parent, |
| 224 | 'page' => 1, |
| 225 | 'posts_per_page' => 1, |
| 226 | ); |
| 227 | |
| 228 | $res = ContentManagerHelper::get_all_child_items( $args ); |
| 229 | |
| 230 | if ( $res && $res[0] ) { |
| 231 | $curr_seo_post = (object) $res[0]; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | $res = array( |
| 236 | 'post_id' => isset( $curr_seo_post->ID ) ? $curr_seo_post->ID : '', |
| 237 | 'post_title' => isset( $curr_seo_post->post_title ) ? $curr_seo_post->post_title : '', |
| 238 | 'post_author' => get_the_author_meta( 'display_name', $curr_seo_post->post_author ), |
| 239 | 'post_date' => get_the_date( '', $curr_seo_post->ID ), |
| 240 | 'post_time' => get_the_time( '', $curr_seo_post->ID ), |
| 241 | 'post_excerpt' => isset( $curr_seo_post->post_excerpt ) ? $curr_seo_post->post_excerpt : '', |
| 242 | 'post_meta' => isset( $curr_seo_post->post_meta ) ? $curr_seo_post->post_meta : '', |
| 243 | 'featured_image' => array( |
| 244 | 'url' => get_the_post_thumbnail_url( $curr_seo_post->ID ), |
| 245 | ), |
| 246 | ); |
| 247 | |
| 248 | // check if post has fields |
| 249 | if ( isset( $curr_seo_post->fields ) ) { |
| 250 | foreach ( $curr_seo_post->fields as $key => $field ) { |
| 251 | $res[ $key ] = $field; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | return $res; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Get SEO data for user type |
| 260 | * |
| 261 | * @param object $post post object. |
| 262 | * |
| 263 | * @return array |
| 264 | */ |
| 265 | private static function get_user_type_seo_response() { |
| 266 | // get user id from post context |
| 267 | $user_id = HelperFunctions::get_user_id_if_possible_from_url(); |
| 268 | |
| 269 | $user = get_user_by( 'ID', $user_id ); |
| 270 | |
| 271 | $res = array( |
| 272 | 'user_id' => isset( $user->ID ) ? $user->ID : '', |
| 273 | 'user_name' => isset( $user->display_name ) ? $user->display_name : '', |
| 274 | 'user_nicename' => isset( $user->user_nicename ) ? $user->user_nicename : '', |
| 275 | 'user_login' => isset( $user->user_login ) ? $user->user_login : '', |
| 276 | 'user_email' => isset( $user->user_email ) ? $user->user_email : '', |
| 277 | 'user_registered' => isset( $user->user_registered ) ? $user->user_registered : '', |
| 278 | 'user_url' => isset( $user->user_url ) ? $user->user_url : '', |
| 279 | 'featured_image' => array( |
| 280 | 'url' => get_avatar_url( $user->ID ), |
| 281 | ), |
| 282 | ); |
| 283 | |
| 284 | return $res; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Get SEO data for term type |
| 289 | * |
| 290 | * @param object $post post object. |
| 291 | * |
| 292 | * @return array |
| 293 | */ |
| 294 | private static function get_term_type_seo_response() { |
| 295 | // get term id from post context |
| 296 | $term_id = HelperFunctions::get_term_id_if_possible_from_url(); |
| 297 | |
| 298 | $term = get_term( $term_id ); |
| 299 | |
| 300 | $res = array( |
| 301 | 'term_id' => isset( $term->term_id ) ? $term->term_id : '', |
| 302 | 'term_name' => isset( $term->name ) ? $term->name : '', |
| 303 | 'term_slug' => isset( $term->slug ) ? $term->slug : '', |
| 304 | 'featured_image' => array( |
| 305 | 'url' => get_term_meta( $term->term_id, 'thumbnail_id', true ), |
| 306 | ), |
| 307 | ); |
| 308 | |
| 309 | return $res; |
| 310 | } |
| 311 | } |