/** * Save options with a post id of zero. */ public function save_options() { if (!papi_is_method('post') || !papi_is_option_page()) { return; } // Check if our nonce is vailed. if (!wp_verify_nonce(papi_get_sanitized_post('papi_meta_nonce'), 'papi_save_data')) { return; } // Get properties data. $data = $this->get_post_data(); // Prepare properties data. $data = $this->prepare_properties_data($data, 0); foreach ($data as $key => $value) { papi_update_property_meta_value(['post_id' => 0, 'slug' => $key, 'type' => Papi_Option_Page::TYPE, 'value' => $value]); } }
/** * Save properties with a post id of zero. */ public function save_properties() { if ($_SERVER['REQUEST_METHOD'] !== 'POST' || papi_get_meta_type() !== 'option') { return; } // Check if our nonce is vailed. if (!wp_verify_nonce(papi_get_sanitized_post('papi_meta_nonce'), 'papi_save_data')) { return; } // Get properties data. $data = $this->get_post_data(); // Prepare properties data. $data = $this->prepare_properties_data($data, 0); foreach ($data as $key => $value) { papi_update_property_meta_value(['id' => 0, 'slug' => $key, 'type' => 'option', 'value' => $value]); } /** * Fire `save_properties` action when all is done. * * @param int $id * @param string $meta_type */ do_action('papi/save_properties', 0, 'option'); }
/** * Get value from $_GET or $_POST with the given key. * * @param string $key * * @return string */ function papi_get_or_post($key) { if (!is_string($key)) { return; } if ($value = papi_get_qs($key)) { return $value; } if ($value = papi_get_sanitized_post($key)) { return $value; } }
/** * Check if post id is valid or not. * * @param int $post_id * * @return bool */ private function valid_post_id($post_id) { $key = papi_get_sanitized_post('action') === 'save-attachment-compat' ? 'id' : 'post_ID'; return papi_get_sanitized_post($key) !== strval($post_id); }
/** * Get page type id. * * @param int $post_id * * @return string */ function papi_get_page_type_id($post_id = 0) { $post_id = papi_get_post_id($post_id); $key = papi_get_page_type_key(); $page_type = ''; if ($post_id !== 0) { $meta_value = get_post_meta($post_id, $key, true); $page_type = empty($meta_value) ? '' : $meta_value; } if (empty($page_type)) { $page_type = str_replace('papi/', '', papi_get_qs('page_type')); } if (empty($page_type)) { $page_type = papi_get_sanitized_post(papi_get_page_type_key()); } // Load right page type from a post query string if (empty($page_type)) { $meta_value = get_post_meta(papi_get_parent_post_id(), $key, true); $page_type = empty($meta_value) ? '' : $meta_value; } // Load page type id from the container if it exists or // load it from `papi_get_all_page_types`. if (empty($page_type)) { $post_type = papi_get_post_type(); $load_once = papi_filter_core_load_one_type_on(); $collection_key = 'core.page_type.' . $post_type; if (in_array($post_type, $load_once)) { if (papi()->exists($collection_key)) { return papi()->make($collection_key); } if ($page_types = papi_get_all_page_types(false, $post_type)) { return $page_types[0]->get_id(); } } } return $page_type; }
/** * Get rules result via GET. * * GET /papi-ajax/?action=get_rules_result */ public function get_rules_result() { if (!papi_get_sanitized_post('data')) { $this->render_error('No rule found'); return; } $data = json_decode(stripslashes(papi_get_sanitized_post('data')), true); if (empty($data) || !is_array($data) || !isset($data['slug'])) { $this->render_error('No rule found'); return; } $page_type = papi_get_page_type_by_post_id(); if ($page_type instanceof Papi_Page_Type === false) { $this->render_error('No rule found'); return; } if (preg_match('/\\[\\]$/', $data['slug'])) { $data['slug'] = preg_replace('/\\[\\]$/', '', $data['slug']); } if ($property = $page_type->get_property($data['slug'])) { wp_send_json(['render' => $property->render_is_allowed_by_rules($data['rules'])]); } else { $this->render_error('No rule found'); } }
/** * Switch page type if all checks pass. * * @param int $post_id * @param WP_post $post */ public function save_post($post_id, $post) { // Check if post id and post object is empty or not. if (empty($post_id) || empty($post)) { return false; } // Check if our nonce is vailed. if (!wp_verify_nonce(papi_get_sanitized_post('papi_meta_nonce'), 'papi_save_data')) { return false; } // Check if so both page type keys exists. if (empty($_POST[papi_get_page_type_key()]) || empty($_POST[papi_get_page_type_key('switch')])) { return false; } // Page type information. $page_type_id = sanitize_text_field($_POST[papi_get_page_type_key()]); $page_type_switch_id = sanitize_text_field($_POST[papi_get_page_type_key('switch')]); // Don't update if the same ids. if ($page_type_id === $page_type_switch_id) { return false; } $page_type = papi_get_entry_type_by_id($page_type_id); $page_type_switch = papi_get_entry_type_by_id($page_type_switch_id); $post_type_object = get_post_type_object(papi_get_post_type()); // Check if page type and post type is not empty. if (empty($page_type_switch) || empty($post_type_object)) { return false; } // Check if autosave. if (wp_is_post_autosave($post_id)) { return false; } // Check if revision. if (wp_is_post_revision($post_id)) { return false; } // Check if revision post type. if (in_array($post->post_type, ['revision', 'nav_menu_item'], true)) { return false; } // Check so page type has the post type. if (!$page_type->has_post_type($post->post_type) || !$page_type_switch->has_post_type($post->post_type)) { return false; } // Check page type capabilities. if (!papi_current_user_is_allowed($page_type_switch->capabilities)) { return false; } // Check so user can edit posts and that the user can publish posts on the post type. if (!current_user_can('edit_post', $post_id) || !current_user_can($post_type_object->cap->publish_posts)) { return false; } // Get properties. $properties = $page_type->get_properties(); $properties_switch = $page_type_switch->get_properties(); // Delete only properties that don't have the same type and slug. foreach ($properties as $property) { $delete = true; // Check if the properties are the same or not. foreach ($properties_switch as $property_switch) { if ($property_switch->type === $property->type && $property_switch->match_slug($property->get_slug())) { $delete = false; break; } } if (!$delete) { continue; } // Delete property values. $property->delete_value($property->get_slug(true), $post_id, papi_get_meta_type()); } // Update page type id. return papi_set_page_type_id($post_id, $page_type_switch_id); }
/** * Check if post id is valid or not. * * @param int $post_id * * @return bool */ protected function valid_post_id($post_id) { $key = papi_get_sanitized_post('action') === 'save-attachment-compat' ? 'id' : 'post_ID'; $val = papi_get_sanitized_post($key); // When autosave is in place the post id is located deeper in the post data array. if (isset($_POST['data'], $_POST['data']['wp_autosave'], $_POST['data']['wp_autosave']['post_id'])) { $val = sanitize_text_field($_POST['data']['wp_autosave']['post_id']); } return $val !== strval($post_id); }