/** * The constructor. * * @param int $id */ public function __construct($id = 0) { $this->id = papi_get_post_id($id); $this->post = get_post($this->id); $id = papi_get_page_type_id($this->id); $this->type_class = papi_get_entry_type_by_id($id); }
/** * Get WordPress post type in various ways. * * @param int $post_id * * @return string */ function papi_get_post_type($post_id = null) { if ($post_type = papi_get_or_post('post_type')) { return $post_type; } $post_id = papi_get_post_id($post_id); if ($post_id !== 0) { return strtolower(get_post_type($post_id)); } $page = papi_get_qs('page'); if (is_string($page) && strpos(strtolower($page), 'papi-add-new-page,') !== false) { $exploded = explode(',', $page); if (empty($exploded[1])) { return ''; } return $exploded[1]; } // If only `post-new.php` without any querystrings // it would be the post post type. $req_uri = $_SERVER['REQUEST_URI']; $exploded = explode('/', $req_uri); $last = end($exploded); if ($last === 'post-new.php') { return 'post'; } return ''; }
/** * Append post type query string. * * @param string $url * @param string $post_type_arg * * @return string */ function papi_append_post_type_query($url, $post_type_arg = null) { if (strpos($url, 'post_type=') !== false) { return preg_replace('/&%.+/', '', $url); } $post_id = papi_get_post_id(); if ($post_id === 0) { $post_type = papi_get_or_post('post_type'); } else { $post_type = get_post_type($post_id); } if (!empty($post_type_arg) && empty($post_type)) { $post_type = $post_type_arg; } if (empty($post_type)) { $post_type = 'post'; } if (!empty($post_type)) { if (substr($url, -1, 1) !== '&') { $url .= '&'; } $url .= 'post_type=' . $post_type; } return $url; }
/** * Append post type query string. * * @param string $url * @param string $post_type_arg * * @return string */ function papi_append_post_type_query($url, $post_type_arg = null) { if (strpos($url, 'post_type=') !== false) { return preg_replace('/&%.+/', '', $url); } $post_type = ''; // Only change post type if post type arg isn't the same. if ($post_type_arg !== $post_type) { $post_type = $post_type_arg; } // Add post type if empty. if (empty($post_type)) { $post_id = papi_get_post_id(); if ($post_id === 0) { $post_type = papi_get_or_post('post_type'); } else { $post_type = get_post_type($post_id); } if (empty($post_type)) { $post_type = $post_type_arg; } if (empty($post_type)) { $post_type = 'post'; } } // Add right query string character. if (!empty($post_type)) { if (substr($url, -1, 1) !== '&') { $url .= '&'; } $url .= 'post_type=' . $post_type; } return $url; }
/** * Get meta post type. * * @return string */ private function get_post_type() { if ($post_id = papi_get_post_id()) { return get_post_type($post_id); } if ($post_type = papi_get_post_type()) { return $post_type; } return $this->box->id; }
/** * The constructor. * * Create a new instance of the class. * * @param int $post_id */ public function __construct($post_id = 0) { if ($post_id === 0) { $this->id = papi_get_post_id(); } else { $this->id = intval($post_id); } $this->post = get_post($this->id); $id = papi_get_page_type_id($this->id); $this->page_type = papi_get_page_type_by_id($id); }
/** * Get meta post type. * * @return string */ protected function get_post_type() { if (papi_get_meta_type() === 'post') { if ($post_id = papi_get_post_id()) { return get_post_type($post_id); } if ($post_type = papi_get_post_type()) { return $post_type; } } return $this->box->id; }
/** * Shortcode for `papi_get_field` function. * * [papi_field id=1 slug="property_slug" default="Default value"][/papi_field] * * @param array $atts * * @return mixed */ function papi_field_shortcode($atts) { $atts['id'] = isset($atts['id']) ? $atts['id'] : 0; $atts['id'] = papi_get_post_id($atts['id']); $default = isset($atts['default']) ? $atts['default'] : ''; if (empty($atts['id']) || empty($atts['slug'])) { $value = $default; } else { $value = papi_get_field($atts['id'], $atts['slug'], $default); } if (is_array($value)) { $value = implode(', ', $value); } return $value; }
/** * Set page type to a post. * * @param mixed $post_id * @param string $page_type * * @return bool */ function papi_set_page_type_id($post_id, $page_type) { return papi_page_type_exists($page_type) && update_post_meta(papi_get_post_id($post_id), papi_get_page_type_key(), $page_type); }
/** * Get import options. * * @param mixed $options * * @return array */ protected function get_import_options($options) { $default_options = ['meta_id' => 0, 'meta_type' => 'post', 'post_id' => 0, 'page_type' => '', 'update_arrays' => false]; if (!is_array($options)) { $options = array_merge($default_options, ['post_id' => papi_get_post_id($options)]); } return array_merge($default_options, $options); }
/** * Get value from the database. * * @return string */ public function get_value() { $value = $this->format_value(parent::get_value(), $this->get_slug(), papi_get_post_id()); return $this->get_setting('allow_html') ? $value : esc_html($value); }
/** * Populate post type. * * @param array|string $post_type * * @return string */ private function populate_post_type($post_type) { $post_id = papi_get_post_id(); if ($post_id !== 0) { return get_post_type($post_id); } // Get the post type that we currently are on if it exist in the array of post types. $post_type = array_filter(papi_to_array($post_type), function ($post_type) { // Support fake post types. if (strpos($post_type, '_papi') !== false) { return true; } return !empty($post_type) && strtolower($post_type) === strtolower(papi_get_or_post('post_type')); }); if (!empty($post_type)) { return $post_type[0]; } return 'page'; }
/** * Get value from the database. * * @return float|int */ public function get_value() { return $this->format_value(parent::get_value(), $this->get_slug(), papi_get_post_id()); }
/** * Render property html. */ public function html() { $post_id = papi_get_post_id(); $slug = $this->html_name(); $settings = $this->get_settings(); $settings_json = []; $sort_option = $this->get_sort_option($post_id); $sort_options = static::get_sort_options(); $values = papi_get_only_objects($this->get_value()); $items = $this->get_items($settings); if (papi_is_empty($settings->items)) { $values = array_map([$this, 'convert_post_to_item'], $values); } else { foreach (array_keys($sort_options) as $key) { if (strpos($key, 'Post') === 0) { unset($sort_options[$key]); } } } // Remove existing values if `only once` is active. if ($this->get_setting('only_once')) { $items = array_udiff($items, $values, function ($a, $b) { // Backwards compatibility with both `post_title` and `title`. return strcmp(strtolower(isset($a->post_title) ? $a->post_title : $a->title), strtolower(isset($b->post_title) ? $b->post_title : $b->title)); }); } // Convert all sneak case key to camel case. foreach ((array) $settings as $key => $val) { if (!is_string($key) || !in_array($key, ['only_once', 'limit'], true)) { continue; } if ($key = papi_camel_case($key)) { $settings_json[$key] = $val; } } ?> <div class="papi-property-relationship" data-settings='<?php echo esc_attr(papi_maybe_json_encode($settings_json)); ?> '> <input type="hidden" name="<?php echo esc_attr($slug); ?> []" data-papi-rule="<?php echo esc_attr($slug); ?> " /> <div class="relationship-inner"> <div class="relationship-top-left"> <label for="<?php echo esc_attr($this->html_id('search')); ?> "><?php esc_html_e('Search', 'papi'); ?> </label> <input id="<?php echo esc_attr($this->html_id('search')); ?> " type="search" /> </div> <div class="relationship-top-right"> <?php if ($settings->show_sort_by) { ?> <label for="<?php echo esc_attr($this->html_id('sort_option')); ?> "><?php esc_html_e('Sort by', 'papi'); ?> </label> <select id="<?php echo esc_attr($this->html_id('sort_option')); ?> " name="<?php echo esc_attr($this->html_id('sort_option')); ?> "> <?php foreach (array_keys($sort_options) as $key) { ?> <option value="<?php echo esc_attr($key); ?> " <?php echo $key === $sort_option ? 'selected="selected"' : ''; ?> ><?php echo esc_html($key); ?> </option> <?php } ?> </select> <?php } ?> </div> <div class="papi-clear"></div> </div> <div class="relationship-inner"> <div class="relationship-left"> <ul> <?php foreach ($items as $item) { if (!empty($item->title)) { ?> <li> <input type="hidden" data-name="<?php echo esc_attr($slug); ?> []" value="<?php echo esc_attr($item->id); ?> "/> <a href="#" title="<?php echo esc_attr($item->title); ?> "><?php echo esc_html($item->title); ?> </a> <span class="icon plus"></span> </li> <?php } } ?> </ul> </div> <div class="relationship-right"> <ul> <?php foreach ($values as $item) { ?> <li> <input type="hidden" name="<?php echo esc_attr($slug); ?> []" value="<?php echo esc_attr($item->id); ?> "/> <a href="#"><?php echo esc_attr($item->title); ?> </a> <span class="icon minus"></span> </li> <?php } ?> </ul> </div> <div class="papi-clear"></div> </div> </div> <?php }
/** * 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; }
/** * Render property html. */ public function html() { $post_id = papi_get_post_id(); $slug = $this->html_name(); $settings = $this->get_settings(); $settings_json = []; $sort_option = $this->get_sort_option($post_id); $sort_options = static::get_sort_options(); $values = papi_get_only_objects($this->get_value()); $items = $this->get_items($settings); if (papi_is_empty($settings->items)) { $values = array_map([$this, 'convert_post_to_item'], $values); } else { foreach ($sort_options as $key => $sort) { if (strpos($key, 'Post') === 0) { unset($sort_options[$key]); } } } // Convert all sneak case key to camel case. foreach ((array) $settings as $key => $val) { if (!is_string($key) || !in_array($key, ['only_once', 'limit'])) { continue; } $settings_json[papi_camel_case($key)] = $val; } ?> <div class="papi-property-relationship" data-settings='<?php echo json_encode($settings_json); ?> '> <input type="hidden" name="<?php echo $slug; ?> []" data-papi-rule="<?php echo $slug; ?> " /> <div class="relationship-inner"> <div class="relationship-top-left"> <label for="<?php echo $this->html_id('search'); ?> "><?php _e('Search', 'papi'); ?> </label> <input id="<?php echo $this->html_id('search'); ?> " type="search" /> </div> <div class="relationship-top-right"> <?php if ($settings->show_sort_by) { ?> <label for="<?php echo $this->html_id('sort_option'); ?> "><?php _e('Sort by', 'papi'); ?> </label> <select id="<?php echo $this->html_id('sort_option'); ?> " name="<?php echo $this->html_id('sort_option'); ?> "> <?php foreach ($sort_options as $key => $v) { ?> <option value="<?php echo $key; ?> " <?php echo $key === $sort_option ? 'selected="selected"' : ''; ?> ><?php echo $key; ?> </option> <?php } ?> </select> <?php } ?> </div> <div class="papi-clear"></div> </div> <div class="relationship-inner"> <div class="relationship-left"> <ul> <?php foreach ($items as $item) { if (!empty($item->title)) { ?> <li> <input type="hidden" data-name="<?php echo $slug; ?> []" value="<?php echo $item->id; ?> "/> <a href="#"><?php echo $item->title; ?> </a> <span class="icon plus"></span> </li> <?php } } ?> </ul> </div> <div class="relationship-right"> <ul> <?php foreach ($values as $item) { ?> <li> <input type="hidden" name="<?php echo $slug; ?> []" value="<?php echo $item->id; ?> "/> <a href="#"><?php echo $item->title; ?> </a> <span class="icon minus"></span> </li> <?php } ?> </ul> </div> <div class="papi-clear"></div> </div> </div> <?php }
/** * 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 }
/** * Set page type to a post. * * @param mixed $post_id * @param string $page_type * * @return bool */ function papi_set_page_type_id($post_id, $page_type) { if (papi_content_type_exists($page_type)) { return update_post_meta(papi_get_post_id($post_id), papi_get_page_type_key(), $page_type); } return false; }
/** * Get post id. * * @return int */ public function get_post_id() { if (!papi_is_empty($this->post_id)) { return $this->post_id; } if ($this->store instanceof Papi_Core_Meta_Store) { return $this->store->id; } return papi_get_post_id(); }
/** * Get page from factory. * * @param int $post_id * @param string $type * * @return mixed */ public static function factory($post_id, $type = 'page') { $type = $type === 'page' ? 'post' : $type; $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); if (!$page->valid()) { return; } return $page; }
/** * Update field with new value. The old value will be deleted. * * @param int $post_id * @param string $slug * @param mixed $value * @param string $type * * @return bool */ function papi_update_field($post_id = null, $slug = null, $value = null, $type = 'page') { if (!is_numeric($post_id) && is_string($post_id)) { $value = $slug; $slug = $post_id; $post_id = null; } if (!is_string($slug) || empty($slug)) { return false; } if (papi_is_empty($value)) { return papi_delete_field($post_id, $slug, $type); } $post_id = papi_get_post_id($post_id); if ($post_id === 0 && $type === Papi_Post_Page::TYPE) { return false; } $page = papi_get_page($post_id, $type); if (is_null($page)) { return false; } $property = $page->get_property($slug); if (!papi_is_property($property)) { return false; } papi_delete_field($post_id, $slug, $type); $value = $property->update_value($value, $slug, $post_id); $value = papi_filter_update_value($property->get_option('type'), $value, $slug, $post_id); return papi_update_property_meta_value(['type' => $type, 'post_id' => $post_id, 'slug' => $slug, '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); }