/** * Check if the page type should be displayed or not. * * @param string|object $page_type * * @return bool */ function papi_display_page_type($page_type) { $post_type = papi_get_post_type(); if (empty($post_type)) { return false; } if (is_string($page_type)) { $page_type = papi_get_entry_type_by_id($page_type); } if (!is_object($page_type)) { return false; } if (!in_array($post_type, $page_type->post_type, true)) { return false; } $display = $page_type->display($post_type); if (!is_bool($display) || $display === false) { return false; } if (preg_match('/papi\\-standard\\-\\w+\\-type/', $page_type->get_id())) { return true; } $parent_page_type = papi_get_entry_type_by_meta_id(papi_get_parent_post_id()); if ($parent_page_type instanceof Papi_Page_Type) { $child_types = $parent_page_type->get_child_types(); if (!empty($child_types)) { return in_array($page_type, $parent_page_type->get_child_types(), true); } } // Run show page type filter. return papi_filter_settings_show_page_type($post_type, $page_type); }
/** * Filter the post. * * Register all properties with `register_meta` and prepare response filter. * * @param WP_Post $post * * @return WP_Post */ public function get_post(WP_Post $post) { if (!($page_type = papi_get_entry_type_by_meta_id($post->ID))) { return $post; } // Register all properties fields with register meta. foreach ($page_type->get_properties() as $property) { $property->register(); } // Get the post store for a page type. $this->store = papi_get_meta_store($post->ID); // Add filter to prepare the response for a post type. add_filter('rest_prepare_' . $post->post_type, [$this, 'prepare_response']); return $post; }
/** * Get rule slug. * * @param Papi_Core_Conditional_Rule $rule * @param Papi_Core_Property $property * * @return string */ protected function get_rule_slug($rule, $property) { $arr_reg = '/\\[\\d+\\](\\[\\w+\\])$/'; $slug = $property->get_slug(); $page_type = papi_get_entry_type_by_meta_id(); if ($page_type instanceof Papi_Page_Type === false) { return $rule->slug; } if (preg_match($arr_reg, $slug, $out)) { $slug = str_replace($out[1], '[' . unpapify($rule->slug) . ']', $slug); $property = $page_type->get_property($slug); if (papi_is_property($property)) { return $slug; } } return $rule->slug; }
/** * Get fields that exists on a term. * * ## OPTIONS * * <term> * : Term ID * * [--field=<field>] * : Instead of returning the whole term fields, returns the value of a single fields. * * [--fields=<fields>] * : Get a specific subset of the term's fields. * * [--format=<format>] * : Accepted values: table, json, csv. Default: table. * * ## AVAILABLE FIELDS * * These fields are available for get command: * * * slug * * type * * exists * * ## EXAMPLES * * wp papi term get 123 --format=json * * wp papi term get 123 --field=slug * * @param array $args * @param array $assoc_args */ public function get($args, $assoc_args) { try { // Set query string that we need. $_GET['meta_type'] = 'term'; // Get the taxonomy type that the term has. $entry_type = papi_get_entry_type_by_meta_id($args[0]); if (empty($entry_type) || $entry_type instanceof Papi_Taxonomy_Type === false) { WP_CLI::error('No taxonomy type exists on the term'); } $properties = []; foreach ($entry_type->get_boxes() as $box) { foreach ($box->properties as $property) { $properties[] = ['slug' => $property->get_slug(true), 'type' => $property->type, 'has value' => $property->get_value() !== null ? 'true' : 'false', 'box' => $box->title]; } } // Render types as a table. $formatter = $this->get_formatter($assoc_args); $formatter->display_items($properties); } catch (WC_CLI_Exception $e) { WP_CLI::error($e->getMessage()); } }
/** * Render property html. */ public function html() { $post_id = papi_get_post_id(); $settings = $this->get_settings(); // Create query array for every page type. $page_types = array_map(function ($page_type) { return ['key' => papi_get_page_type_key(), 'value' => $page_type, 'compare' => 'LIKE']; }, papi_to_array($settings->page_type)); // Add relation. $page_types['relation'] = 'OR'; // Prepare arguments for WP_Query. $args = ['post_type' => 'any', 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, 'meta_query' => $page_types]; $query = new WP_Query($args); $posts = $query->get_posts(); $values = []; foreach (papi_to_array($settings->slug) as $slug) { foreach ($posts as $post) { $val = papi_get_field($post->ID, $slug); $val = array_filter(papi_to_array($val), function ($item) use($post_id) { return is_object($item) && $item->ID === $post_id; }); if (empty($val)) { continue; } $page_type = papi_get_entry_type_by_meta_id($post->ID); if (empty($page_type)) { continue; } // Create the array if (!isset($values[$post->post_type])) { $values[$post->post_type] = []; } if (!isset($values[$post->post_type][$page_type->name])) { $values[$post->post_type][$page_type->name] = []; } // Add the post if (!isset($values[$post->post_type][$page_type->name][$post->ID]) && !empty($val)) { $values[$post->post_type][$page_type->name][$post->ID] = $post; } } } ?> <ul class="papi-property-reference" data-papi-rule="<?php echo esc_attr($this->html_name()); ?> "> <?php if (empty($values)) { ?> <p> <?php esc_html_e('No references exists', 'papi'); ?> </p> <?php } ksort($values); foreach ($values as $title => $val) { $post_type = get_post_type_object($title); ?> <li> <h3><?php echo esc_html($post_type->labels->name); ?> </h3> <div class="handlediv" title="Click to toggle"><br></div> </li> <li> <div class="page-types"> <ul> <?php ksort($val); foreach ($val as $name => $posts) { ?> <li class="heading-border"> <h4><?php echo esc_html($name); ?> </h4> <div class="handlediv" title="Click to toggle"><br></div> </li> <li> <div class="box"> <?php $i = 0; foreach ($posts as $post) { ?> <a href="<?php echo esc_attr(get_edit_post_link($post->ID)); ?> "><?php echo esc_html($post->post_title); ?> </a> <?php $i++; } ?> <div class="clear"></div> </div> </li> <?php } ?> </ul> <div class="clear"></div> </div> </li> <?php } ?> </ul> <?php }
/** * Import data to Papi. * * @param mixed $options * @param array $fields * * @return bool */ public function import($options, array $fields = []) { $options = $this->get_import_options($options); $meta_id = empty($options['meta_id']) ? $options['post_id'] : $options['meta_id']; $meta_type = $options['meta_type']; $entry_type = $options['page_type']; if (isset($options['update_arrays'])) { $this->driver->set_options(['update_array' => $options['update_arrays']]); } if (empty($meta_id) || empty($fields)) { return false; } if (empty($entry_type)) { $entry_type = papi_get_entry_type_by_meta_id($meta_id, $meta_type); } if (is_string($entry_type)) { $entry_type = papi_get_entry_type_by_id($entry_type); } if ($entry_type instanceof Papi_Entry_Type === false) { return false; } update_metadata($meta_type, $meta_id, papi_get_page_type_key(), $entry_type->get_id()); $result = true; foreach ($fields as $slug => $value) { if (!is_string($slug) || papi_is_empty($value)) { continue; } $property = $entry_type->get_property($slug); if (!papi_is_property($property)) { $result = false; continue; } $value = $this->fire_filter(['filter' => 'driver:value', 'type' => 'before', 'value' => [$value, $slug]]); $value = $this->get_value(['post_id' => $meta_id, 'property' => $property, 'slug' => $slug, 'value' => $value]); $value = $this->fire_filter(['filter' => 'driver:value', 'type' => 'after', 'value' => [$value, $slug]]); $out = papi_update_property_meta_value(['id' => $meta_id, 'slug' => $slug, 'type' => $meta_type, 'value' => $value]); $result = $out ? $result : $out; } return $result; }
/** * 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; } $entry_type = papi_get_entry_type_by_meta_id(); if ($entry_type instanceof Papi_Entry_Type === false) { $this->render_error('No rule found'); return; } if (preg_match('/\\[\\]$/', $data['slug'])) { $data['slug'] = preg_replace('/\\[\\]$/', '', $data['slug']); } if ($property = $entry_type->get_property($data['slug'])) { wp_send_json(['render' => $property->render_is_allowed_by_rules($data['rules'])]); return; } $this->render_error('No rule found'); }
/** * Get entry template file from meta id. * * @param int $id * @param string $type * * @return null|string */ function papi_get_entry_type_template($id = 0, $type = null) { if (empty($id) && !is_numeric($id)) { return; } $data = papi_get_entry_type_by_meta_id($id, $type); if (isset($data, $data->template)) { return papi_get_template_file_name($data->template); } }
/** * 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(); $meta_id = papi_get_meta_id(); $entry_type = papi_get_entry_type_by_meta_id($meta_id); if (!papi_is_empty($source) && $entry_type instanceof Papi_Entry_Type !== false) { if (papi_is_property($entry_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(); $type = papi_get_meta_type(); $value = papi_get_field($slug, null, $type); return $this->get_deep_value($slug, $value); }
/** * Add custom table column to page or taxonomy type. * * @param string $column_name * @param int $post_id * @param int $term_id */ public function manage_page_type_posts_custom_column($column_name, $post_id, $term_id = null) { if (!in_array($this->post_type, papi_get_post_types(), true) && !in_array($this->taxonomy, papi_get_taxonomies(), true)) { return; } /** * Hide column for post type. Default is false. * * @param string $post_type */ if (apply_filters('papi/settings/column_hide_' . $this->get_meta_type_value(), false)) { return; } // Column name most be `entry_type`. On taxomy the column name is `post_id` variable. if ($column_name !== 'entry_type' && $post_id !== 'entry_type') { return; } // Get the entry type for the post or term. $entry_type = papi_get_entry_type_by_meta_id(is_numeric($post_id) ? $post_id : $term_id, papi_get_meta_type()); if (!is_null($entry_type)) { echo esc_html($entry_type->name); } else { $post = !empty($this->post_type) && empty($this->taxonomy); $arg = $post ? papi_get_post_type() : papi_get_taxonomy(); $type = $post ? 'page' : 'taxonomy'; echo esc_html(call_user_func_array("papi_filter_settings_standard_{$type}_type_name", [$arg])); } }