/** * Get rule slug. * * @param Papi_Core_Conditional_Rule $rule * @param Papi_Core_Property $property * * @return string */ private function get_rule_slug($rule, $property) { $arrReg = '/\\[\\d+\\](\\[\\w+\\])$/'; $slug = $property->get_slug(); $page_type = papi_get_page_type_by_post_id(); if ($page_type instanceof Papi_Page_Type === false) { return $rule->slug; } if (preg_match($arrReg, $slug, $out)) { $slug = str_replace($out[1], '[' . papi_remove_papi($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 post. * * ## OPTIONS * * <post> * : Post ID * * [--field=<field>] * : Instead of returning the whole post fields, returns the value of a single fields. * * [--fields=<fields>] * : Get a specific subset of the post'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 post get 123 --format=json * * wp papi post get 123 --field=slug * * @param array $args * @param array $assoc_args */ public function get($args, $assoc_args) { try { // Set post query string to post id. $_GET['post'] = $args[0]; // Get the page type that the post has. $page_type = papi_get_page_type_by_post_id($args[0]); if (empty($page_type)) { WP_CLI::error('No page type exists on the post'); } $properties = []; foreach ($page_type->get_boxes() as $box) { foreach ($box->properties as $property) { $properties[] = ['slug' => $property->get_slug(true), 'type' => $property->type, 'exists' => $property->get_value() !== null]; } } // 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()); } }
/** * Get template file from post id. * * @param int|string $post_id * * @return null|string */ function papi_get_page_type_template($post_id = 0) { if (empty($post_id) && !is_numeric($post_id)) { return; } $data = papi_get_page_type_by_post_id($post_id); if (isset($data) && isset($data->template)) { $template = $data->template; $extension = '.php'; $ext_reg = '/(' . $extension . ')+$/'; if (preg_match('/\\.\\w+$/', $template, $matches) && preg_match($ext_reg, $matches[0])) { return str_replace('.', '/', preg_replace('/' . $matches[0] . '$/', '', $template)) . $matches[0]; } else { $template = str_replace('.', '/', $template); return substr($template, -strlen($extension)) === $extension ? $template : $template . $extension; } } }
/** * 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'); } }
/** * Add custom table column to page type. * * @param string $column_name * @param int $post_id */ public function manage_page_type_posts_custom_column($column_name, $post_id) { if (!in_array($this->post_type, papi_get_post_types())) { return; } /** * Hide column for post type. Default is false. * * @param string $post_type */ if (apply_filters('papi/settings/column_hide_' . $this->post_type, false)) { return; } if ($column_name === 'page_type') { $page_type = papi_get_page_type_by_post_id($post_id); if (!is_null($page_type)) { echo esc_html($page_type->name); } else { echo esc_html(papi_filter_settings_standard_page_name(papi_get_post_type())); } } }
/** * Import data to Papi. * * @param mixed $options * @param array $fields * * @return bool */ public function import($options, array $fields = []) { $options = $this->get_import_options($options); $post_id = $options['post_id']; $page_type = $options['page_type']; if (isset($options['update_arrays'])) { $this->driver->set_options(['update_array' => $options['update_arrays']]); } if (empty($post_id) || empty($fields)) { return false; } if (empty($page_type)) { $page_type = papi_get_page_type_by_post_id($options['post_id']); } if (is_string($page_type)) { $page_type = papi_get_page_type_by_id($page_type); } if (!papi_is_page_type($page_type)) { return false; } $result = true; foreach ($fields as $slug => $value) { if (!is_string($slug) || papi_is_empty($value)) { continue; } $property = $page_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' => $post_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(['post_id' => $post_id, 'slug' => $slug, 'value' => $value]); $result = $out ? $result : $out; } return $result; }
/** * 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_page_type_by_post_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 $this->html_name(); ?> "> <?php if (empty($values)) { ?> <p> <?php _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 $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 $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 get_edit_post_link($post->ID); ?> "><?php echo $post->post_title; ?> </a> <?php $i++; } ?> <div class="clear"></div> </div> </li> <?php } ?> </ul> <div class="clear"></div> </div> </li> <?php } ?> </ul> <?php }
/** * Add custom table column to page type. * * @param string $column_name * @param int $post_id */ public function manage_page_type_posts_custom_column($column_name, $post_id) { if (!in_array($this->post_type, papi_get_post_types())) { return; } if ($column_name === 'page_type') { $page_type = papi_get_page_type_by_post_id($post_id); if (!is_null($page_type)) { echo esc_html($page_type->name); } else { echo esc_html(papi_filter_settings_standard_page_name(papi_get_post_type())); } } }
/** * 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); }