/** * Check if it's a option page or not. * * @return bool */ public function is_option_page() { if ($this->page === null) { return papi_is_option_page(); } return $this->page instanceof Papi_Option_Page; }
/** * 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]); } }
/** * Check if it's a option page or not. * * @return bool */ public function is_option_page() { if ($this->page === null) { return papi_is_option_page(); } return $this->page->is(Papi_Core_Page::TYPE_OPTION); }
/** * Update property values on the post with the given post id * or update property values on the option page. * * @param array $meta * * @return bool */ function papi_update_property_meta_value(array $meta = []) { $meta = (object) $meta; $option = papi_is_option_page(); $save_value = true; foreach (papi_to_array($meta->value) as $key => $value) { if (is_string($key)) { $save_value = false; break; } } if (!isset($meta->post_id)) { $meta->post_id = 0; } if (!$save_value && is_array($meta->value)) { $meta->value = [$meta->value]; } if (papi_is_empty($meta->value)) { papi_cache_delete($meta->slug, $meta->post_id); if ($option) { return delete_option(papi_remove_papi($meta->slug)); } else { return delete_post_meta($meta->post_id, papi_remove_papi($meta->slug)); } } $result = true; foreach (papi_to_array($meta->value) as $key => $value) { papi_cache_delete($meta->slug, $meta->post_id); if (!is_array($value)) { if ($save_value) { $value = $meta->value; } if ($option) { $out = update_option(papi_remove_papi($meta->slug), $value); $result = $out ? $result : $out; } else { $out = update_post_meta($meta->post_id, papi_remove_papi($meta->slug), $value); $result = $out ? $result : $out; } continue; } foreach ($value as $child_key => $child_value) { if (papi_is_empty($child_value)) { if ($option) { delete_option(papi_remove_papi($child_key)); } else { delete_post_meta($meta->post_id, papi_remove_papi($child_key)); } } else { if ($option) { update_option(papi_remove_papi($child_key), $child_value); } else { update_post_meta($meta->post_id, papi_remove_papi($child_key), $child_value); } } } } return $result; }
/** * Update property values on the post with the given post id * or update property values on the option page. * * @param array $meta * * @return bool */ function papi_update_property_meta_value(array $meta = []) { $meta = array_merge(['post_id' => 0, 'slug' => '', 'type' => Papi_Post_Page::TYPE, 'value' => ''], $meta); $meta = (object) $meta; $option = $meta->type === 'option' || papi_is_option_page(); $save_value = true; foreach (papi_to_array($meta->value) as $key => $value) { if (is_string($key)) { $save_value = false; break; } } if (!$save_value && is_array($meta->value)) { $meta->value = [$meta->value]; } if (papi_is_empty($meta->value)) { return papi_delete_property_meta_value($meta->post_id, $meta->slug, $meta->type); } $result = true; foreach (papi_to_array($meta->value) as $key => $value) { papi_cache_delete($meta->slug, $meta->post_id); if (!is_array($value)) { if ($save_value) { $value = $meta->value; } if ($option) { $out = update_option(unpapify($meta->slug), $value); $result = $out ? $result : $out; } else { $out = update_post_meta($meta->post_id, unpapify($meta->slug), $value); $result = $out ? $result : $out; } continue; } foreach ($value as $child_key => $child_value) { if (papi_is_empty($child_value)) { papi_delete_property_meta_value($meta->post_id, $child_key, $meta->type); } else { if ($option) { update_option(unpapify($child_key), $child_value); } else { update_post_meta($meta->post_id, unpapify($child_key), $child_value); } } } } return $result; }
/** * Get page from factory. * * @param int $post_id * @param string $type * * @return mixed */ public static function factory($post_id, $type = self::TYPE_POST) { if (papi_is_option_page()) { $type = self::TYPE_OPTION; } $class_suffix = '_' . ucfirst($type) . '_Page'; $class_name = 'Papi' . $class_suffix; if (!class_exists($class_name)) { return; } $post_id = papi_get_post_id($post_id); $page = new $class_name($post_id); $page->set_type($type); if (!$page->valid()) { return; } return $page; }
/** * Get value. * * @return mixed */ public function get_value() { $value = $this->get_option('value'); if (papi_is_empty($value)) { $slug = $this->get_slug(true); if (papi_is_option_page()) { $value = papi_get_option($slug); } else { $value = papi_get_field($this->get_post_id(), $slug); } $post_status = get_post_status($this->get_post_id()); if (papi_is_empty($value) && ($post_status === false || $post_status === 'auto-draft')) { $value = $this->get_option('default'); } } return $this->prepare_value($value); }
/** * Get property value. * * @param Papi_Core_Conditional_Rule $rule * * @return mixed */ private function get_value(Papi_Core_Conditional_Rule $rule) { if (papi_doing_ajax()) { $source = $rule->get_source(); $post_id = papi_get_post_id(); $page_type = papi_get_page_type_by_post_id($post_id); if (!papi_is_empty($source) && $page_type instanceof Papi_Page_Type !== false) { if (papi_is_property($page_type->get_property($rule->slug))) { return $this->get_deep_value($rule->slug, $source); } } } if (!papi_is_empty($rule->get_source())) { return $this->get_deep_value($rule->slug, $rule->get_source()); } $slug = $rule->get_field_slug(); if (papi_is_option_page()) { $value = papi_get_option($slug); } else { $value = papi_get_field($slug); } return $this->get_deep_value($slug, $value); }