/** * Save attachment post data. * * @param array $post * @param array $attachment * * @return array */ public function save_attachment($post, $attachment) { update_post_meta($post['ID'], papi_get_page_type_key(), $this->get_id()); $handler = new Papi_Admin_Post_Handler(); $handler->save_meta_boxes($post['ID'], $post); return $post; }
/** * Add form fields to edit tags page. */ public function add_form_fields() { $html_name = esc_attr(papi_get_page_type_key()); $taxonomy = papi_get_qs('taxonomy'); $taxonomy_object = get_taxonomy($taxonomy); // Get only the taxonomy types that has the taxonomy. $taxonomy_types = array_filter($this->taxonomy_types, function ($taxonomy_type) use($taxonomy) { return in_array($taxonomy, $taxonomy_type->taxonomy, true) && $taxonomy_type->display($taxonomy); }); $taxonomy_types = array_values($taxonomy_types); // Do not display empty select if no taxonomy types. if (empty($taxonomy_types)) { return; } // Prepare taxonomy types with standard taxonomy type. $taxonomy_types = $this->prepare_taxonomy_types($taxonomy_types); // Render a dropdown if more than one taxonomy types // exists on the taxonomy. if (count($taxonomy_types) > 1) { ?> <div class="form-field"> <label for="<?php echo esc_attr($html_name); ?> "> <?php echo esc_html(sprintf(__('%s type', 'papi'), $taxonomy_object->labels->singular_name)); ?> </label> <select name="<?php echo esc_attr($html_name); ?> " id="<?php echo esc_attr($html_name); ?> " data-papi-page-type-key="true"> <?php foreach ($taxonomy_types as $taxonomy_type) { papi_render_html_tag('option', ['data-redirect' => $taxonomy_type->redirect_after_create, 'value' => esc_attr($taxonomy_type->get_id()), esc_html($taxonomy_type->name)]); } ?> </select> </div> <?php } else { papi_render_html_tag('input', ['data-redirect' => $taxonomy_types[0]->redirect_after_create, 'data-papi-page-type-key' => true, 'name' => esc_attr($html_name), 'type' => 'hidden', 'value' => esc_attr($taxonomy_types[0]->get_id())]); } }
/** * Add page type class name as a css class on body. * * @param array $classes * * @return array */ function papi_body_class(array $classes) { global $post; // Check so we only change template on single and page posts. if (!is_single() && !is_page()) { return $classes; } $page_type = get_post_meta($post->ID, papi_get_page_type_key(), true); if (empty($page_type)) { return $classes; } $parts = explode('/', $page_type); if (empty($parts) || empty($parts[0])) { return $classes; } $classes[] = array_pop($parts); return $classes; }
/** * Get post data. * * @param string $pattern * * @return array */ protected function get_post_data($pattern = '/^papi\\_.*/') { $data = []; $keys = preg_grep($pattern, array_keys($_POST)); foreach ($keys as $key) { // Remove page type keys with suffix. This should not be saved. if (strpos($key, papi_get_page_type_key()) === 0 && strlen(papi_get_page_type_key()) !== strlen($key)) { continue; } // Fix for input fields that should be true on `on` value. if ($_POST[$key] === 'on') { $data[$key] = true; } else { $value = $this->decode_property($key, $_POST[$key]); $data[$key] = $this->prepare_post_data($value); $data[$key] = $this->santize_data($data[$key]); } } // Don't wont to save meta nonce field. if (isset($data['papi_meta_nonce'])) { unset($data['papi_meta_nonce']); } return $data; }
/** * Set taxonomy type to a term. * * @param mixed $term_id * @param string $taxonomy_type * * @return bool */ function papi_set_taxonomy_type_id($term_id, $taxonomy_type) { if (papi_entry_type_exists($taxonomy_type)) { return update_term_meta(papi_get_term_id($term_id), papi_get_page_type_key(), $taxonomy_type); } return false; }
/** * 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) { return papi_page_type_exists($page_type) && update_post_meta(papi_get_post_id($post_id), papi_get_page_type_key(), $page_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); $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; }
/** * 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); }
/** * Output Papi page type hidden field. * * This will only output on a post type page. */ public function edit_form_after_title() { wp_nonce_field('papi_save_data', 'papi_meta_nonce'); if ($value = esc_attr(papi_get_entry_type_id())) { papi_render_html_tag('input', ['data-papi-page-type-key' => true, 'name' => esc_attr(papi_get_page_type_key()), 'type' => 'hidden', 'value' => $value]); } }
/** * Get real query arguments without Papi Query specific arguments. * * @return array */ public function get_query_args() { $args = $this->args; if (empty($args['meta_query'])) { // Add new meta key/value if `meta_key` or `meta_value` is empty. if (empty($args['meta_key']) || empty($args['meta_value'])) { $args['meta_key'] = papi_get_page_type_key(); $args['meta_value'] = $args['entry_type']; } else { if (papi_entry_type_exists($args['entry_type'])) { $item = ['key' => $args['meta_key'], 'value' => $args['meta_value']]; // Add `meta_compare` if set. if (isset($args['meta_compare'])) { $item['compare'] = $args['meta_compare']; unset($args['meta_compare']); } // Add new meta query item. $args['meta_query'][] = $item; // Add Papi entry/page type meta query. $args['meta_query'][] = ['key' => papi_get_page_type_key(), 'value' => $args['entry_type']]; // Add meta query relation when two query items. if (isset($args['relation'])) { $args['meta_query']['relation'] = $args['relation']; } else { $args['meta_query']['relation'] = 'AND'; } unset($args['meta_key']); unset($args['meta_value']); } } } else { if (papi_entry_type_exists($args['entry_type'])) { // Add Papi entry/page type meta query. $args['meta_query'][] = ['key' => papi_get_page_type_key(), 'value' => $args['entry_type']]; // Add meta query relation if not set. if (!isset($args['meta_query']['relation'])) { $args['meta_query']['relation'] = 'AND'; } } } // Since the real query classes don't support // custom arguments the should be deleted. foreach (array_keys($this->default_args) as $key) { if (isset($args[$key])) { unset($args[$key]); } } return $args; }
/** * 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 entry type id. * * @param int $id * @param string $type * * @return string */ function papi_get_entry_type_id($id = 0, $type = null) { $type = papi_get_meta_type($type); $id = papi_get_meta_id($type, $id); if ($id > 0) { if ($meta_value = get_metadata($type, $id, papi_get_page_type_key(), true)) { return $meta_value; } } $entry_type_id = papi_get_qs('entry_type'); /** * Change entry type id. * * @param string $entry_type_id * @param string $type * * @return string */ return apply_filters('papi/entry_type_id', $entry_type_id, $type); }
/** * Filter posts on load if `page_type` query string is set. * * @param WP_Query $query * * @return WP_Query */ public function pre_get_posts($query) { global $pagenow; if ($pagenow === 'edit.php' && !is_null(papi_get_qs('page_type'))) { if (papi_get_qs('page_type') === 'papi-standard-page') { $query->set('meta_query', [['key' => papi_get_page_type_key(), 'compare' => 'NOT EXISTS']]); } else { $query->set('meta_key', papi_get_page_type_key()); $query->set('meta_value', papi_get_qs('page_type')); } } return $query; }
public function register() { $this->remove(['editor']); $this->box('Content', [papi_property(['title' => 'Pages', 'type' => 'repeater', 'sidebar' => false, 'settings' => ['items' => papi_property(['title' => 'Page', 'type' => 'post', 'settings' => ['text' => '', 'post_type' => 'page', 'query' => ['meta_query' => [['key' => papi_get_page_type_key(), 'compare' => 'NOT EXISTS']]]]])]])]); }