/** * Convert property value with the property type converter. * * @param string $slug * @param mixed $value * * @return mixed */ protected function convert($slug, $value) { $property = $this->get_property($slug); // If no property type is found, just return null. if (!papi_is_property($property)) { return; } if (papi_is_empty($value)) { if (!papi_is_empty($property->get_option('value'))) { return $property->get_option('value'); } return; } // A property need to know about the page. $property->set_page($this); // Run load value method right after the value has been loaded from the database. $value = $property->load_value($value, $slug, $this->id); $value = papi_filter_load_value($property->type, $value, $slug, $this->id); // Format the value from the property class. $value = $property->format_value($value, $slug, $this->id); // Only fired when not in admin. if (!is_admin()) { $value = papi_filter_format_value($property->type, $value, $slug, $this->id); } if (is_array($value)) { $value = array_filter($value); } return $value; }
/** * Handle Papi ajax. */ public function handle_papi_ajax() { global $wp_query; if (!is_object($wp_query)) { return; } if (defined('DOING_AJAX') && DOING_AJAX) { return; } if (!papi_is_empty(papi_get_qs('action'))) { $wp_query->set('papi_ajax_action', papi_get_qs('action')); } $ajax_action = $wp_query->get('papi_ajax_action'); if (is_user_logged_in() && has_action($this->action_prefix . $ajax_action) !== false) { if (!defined('DOING_AJAX')) { define('DOING_AJAX', true); } if (!defined('DOING_PAPI_AJAX')) { define('DOING_PAPI_AJAX', true); } status_header(200); do_action($this->action_prefix . $ajax_action); wp_die(); } }
/** * Render property html. */ public function html() { $settings = $this->get_settings(); $value = papi_cast_string_value($this->get_value()); $options_html = []; // Override selected setting with // database value if not empty. if (!papi_is_empty($value)) { $settings->selected = $value; } $classes = 'papi-fullwidth'; if ($settings->select2) { $classes .= ' papi-component-select2'; } if (!empty($settings->placeholder)) { $options_html[] = papi_html_tag('option', ['value' => '', $settings->placeholder]); } // Create option html tags for all items. foreach ($this->get_items() as $key => $value) { $key = is_numeric($key) ? $value : $key; if (papi_is_empty($key)) { continue; } $options_html[] = papi_html_tag('option', ['selected' => $value === $settings->selected ? 'selected' : null, 'value' => $value, papi_convert_to_string($key)]); } papi_render_html_tag('select', ['class' => $classes, 'data-allow-clear' => true, 'data-placeholder' => $settings->placeholder, 'data-width' => '100%', 'id' => $this->html_id(), 'name' => $this->html_name(), $options_html]); }
/** * Render property html. */ public function html() { $options = $this->get_options(); papi_render_html_tag('div', ['class' => 'papi-property-divider', 'data-papi-rule' => $this->html_name(), sprintf('<h3><span>%s</span></h3>', $options->title)]); if (!papi_is_empty($options->description)) { echo sprintf('<p>%s</p>', $options->description); } }
/** * Render property html. */ public function html() { $options = $this->get_options(); $text = ''; $options->description = $options->slug; if (!papi_is_empty($options->description)) { $text = sprintf('<p>%s</p>', $options->description); } papi_render_html_tag('div', ['class' => 'papi-property-divider', 'data-papi-rule' => esc_attr($this->html_name()), sprintf('<h3><span>%s</span></h3>%s', esc_html($options->title), $text)]); }
/** * Import value to the property. * * @param mixed $value * @param string $slug * @param int $post_id * * @return mixed */ public function import_value($value, $slug, $post_id) { if (is_string($value) && !papi_is_empty($value)) { return [$value]; } if (!is_array($value)) { return; } return $value; }
/** * Render property html. */ public function html() { $settings = $this->get_settings(); $value = papi_cast_string_value($this->get_value()); // Override selected setting with // database value if not null. if (!papi_is_empty($value)) { $settings->selected = $value; } foreach ($settings->items as $key => $value) { $key = is_numeric($key) ? $value : $key; papi_render_html_tag('label', ['class' => 'light', 'for' => $this->html_id($key), papi_render_html_tag('input', ['id' => $this->html_id($key), 'name' => $this->html_name(), 'type' => 'radio', 'checked' => $value === $settings->selected ? 'checked' : null, 'value' => $value]), papi_convert_to_string($key)]); papi_render_html_tag('br'); } }
/** * Render property html. */ public function html() { $settings = $this->get_settings(); $value = $this->get_value(); // If range type is used change the default values if empty. if ($settings->type === 'range') { $settings->max = papi_is_empty($settings->max) ? 100 : $settings->max; $settings->min = papi_is_empty($settings->min) ? 0 : $settings->min; $settings->step = papi_is_empty($settings->step) ? 1 : $settings->step; } if ($settings->min !== 0 && $value < $settings->min) { $value = $settings->min; } papi_render_html_tag('input', ['id' => $this->html_id(), 'max' => $settings->max, 'min' => $settings->min, 'name' => $this->html_name(), 'step' => $settings->step, 'type' => $settings->type, 'value' => $value]); }
/** * Render property html. */ public function html() { $settings = $this->get_settings(); $value = papi_cast_string_value($this->get_value()); // Override selected setting with // database value if not null. if (!papi_is_empty($value)) { $settings->selected = $value; } echo '<div class="papi-property-radio">'; foreach ($settings->items as $key => $value) { $key = is_numeric($key) ? $value : $key; papi_render_html_tag('p', [papi_html_tag('label', ['class' => 'light', 'for' => esc_attr($this->html_id($key)), papi_html_tag('input', ['id' => esc_attr($this->html_id($key)), 'name' => esc_attr($this->html_name()), 'type' => 'radio', 'checked' => $value === $settings->selected, 'value' => $value]), esc_html(papi_convert_to_string($key))])]); } echo '</div>'; }
/** * Format the value of the property before it's returned * to WordPress admin or the site. * * @param mixed $values * @param string $repeater_slug * @param int $post_id * * @return array */ public function format_value($values, $repeater_slug, $post_id) { if (!is_array($values)) { return []; } foreach ($values as $index => $layout) { foreach ($layout as $slug => $value) { if (is_string($value) && preg_match($this->layout_value_regex, $value)) { if (isset($values[$index][$this->layout_key])) { unset($values[$index][$slug]); continue; } $values[$index][$this->layout_key] = $value; unset($values[$index][$slug]); continue; } if (papi_is_property_type_key($slug)) { continue; } $property_type_slug = papi_get_property_type_key_f($slug); if (!isset($values[$index][$property_type_slug])) { continue; } $property_type_value = $values[$index][$property_type_slug]; $property_type = papi_get_property_type($property_type_value); if (!is_object($property_type)) { continue; } // Get property child slug. $child_slug = $this->get_child_slug($repeater_slug, $slug); // Create cache key. $cache_key = sprintf('%s_%d_%s', $repeater_slug, $index, $slug); // Get raw value from cache if enabled. if ($this->cache) { $raw_value = papi_cache_get($cache_key, $post_id, $this->get_meta_type()); } else { $raw_value = false; } // Load the value. if ($raw_value === null || $raw_value === false) { $values[$index][$slug] = $property_type->load_value($value, $child_slug, $post_id); $values[$index][$slug] = papi_filter_load_value($property_type->type, $values[$index][$slug], $child_slug, $post_id, papi_get_meta_type()); if (!papi_is_empty($values[$index][$slug]) && $this->cache) { papi_cache_set($cache_key, $post_id, $values[$index][$slug], $this->get_meta_type()); } } else { $values[$index][$slug] = $raw_value; } if (strtolower($property_type->type) === 'repeater') { $property_type->cache = false; } // Format the value from the property class. $values[$index][$slug] = $property_type->format_value($values[$index][$slug], $child_slug, $post_id); if (!is_admin()) { $values[$index][$slug] = papi_filter_format_value($property_type->type, $values[$index][$slug], $child_slug, $post_id, papi_get_meta_type()); } $values[$index][$property_type_slug] = $property_type_value; } } if (!is_admin()) { foreach ($values as $index => $row) { foreach ($row as $slug => $value) { if (is_string($value) && preg_match($this->layout_value_regex, $value)) { unset($values[$index][$slug]); $values[$index]['_layout'] = preg_replace($this->layout_value_regex, '', $value); } if (papi_is_property_type_key($slug)) { unset($values[$index][$slug]); } if (papi_is_empty($value)) { unset($values[$index][$slug]); } } } } return $values; }
/** * Render the final html that is displayed in the table. */ protected function render_row_html() { $display_class = $this->display() ? '' : ' papi-hide'; $rules_class = papi_is_empty($this->get_rules()) ? '' : ' papi-rules-exists'; $css_class = trim($display_class . $rules_class); if ($this->get_option('raw')) { echo sprintf('<div class="%s">', $css_class); $this->render_property_html(); echo '</div>'; } else { ?> <tr class="<?php echo $css_class; ?> "> <?php if ($this->get_option('sidebar')) { ?> <td> <?php $this->render_label_html(); $this->render_description_html(); ?> </td> <?php } ?> <td <?php echo $this->get_option('sidebar') ? '' : 'colspan="2"'; ?> > <?php $this->render_property_html(); ?> </td> </tr> <?php } }
/** * Render property html. */ public function html() { $settings = $this->get_settings(); $value = $this->get_value(); if (is_object($value)) { $value = $value->ID; } else { $value = 0; } $posts = $this->get_posts($settings); $render_label = count($posts) > 1; $classes = 'papi-fullwidth'; if ($settings->select2) { $classes = ' papi-component-select2'; } ?> <div class="papi-property-post"> <select id="<?php echo $this->html_id(); ?> " name="<?php echo $this->html_name(); ?> " class="<?php echo $classes; ?> " data-allow-clear="true" data-placeholder="<?php echo $settings->placeholder; ?> " data-width="100%"> <?php if (!empty($settings->placeholder)) { ?> <option value=""></option> <?php } ?> <?php foreach ($posts as $label => $items) { ?> <?php if ($render_label && is_string($label)) { ?> <optgroup label="<?php echo $label; ?> "> <?php } ?> <?php foreach ($items as $post) { if (papi_is_empty($post->post_title)) { continue; } papi_render_html_tag('option', ['value' => $post->ID, 'selected' => $value === $post->ID ? 'selected' : null, $post->post_title]); } ?> <?php if ($render_label) { ?> </optgroup> <?php } ?> <?php } ?> </select> </div> <?php }
/** * 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(['id' => 0, 'slug' => '', 'type' => 'post', 'value' => ''], $meta); $meta = (object) $meta; $meta->type = papi_get_meta_type($meta->type); $save_value = true; // Set the right update value function for the type. $update_value_fn = $meta->type === 'option' ? 'update_option' : 'update_metadata'; /** * Change update function. * * @param string $update_value_fn */ $update_value_fn = apply_filters('papi/core/update_value_fn', $update_value_fn); // Check so the function is callable before using it. if (!is_callable($update_value_fn)) { return; } // Check for string keys in the array if any. foreach (papi_to_array($meta->value) as $key => $value) { if (is_string($key)) { $save_value = false; break; } } // If main value shouldn't be saved it should be array. if (!$save_value && is_array($meta->value)) { $meta->value = [$meta->value]; } // Delete saved value if empty. if (papi_is_empty($meta->value)) { return papi_delete_property_meta_value($meta->id, $meta->slug, $meta->type); } $result = true; foreach (papi_to_array($meta->value) as $key => $value) { // Delete saved value if value is empty. if (papi_is_empty($value) || $value === '[]' || $value === '{}') { return papi_delete_property_meta_value($meta->id, $meta->slug, $meta->type); } // Delete main value cache. papi_cache_delete($meta->slug, $meta->id, $meta->type); // If not a array we can save the value. if (!is_array($value)) { if ($save_value) { $value = $meta->value; } if (papi_get_meta_type($meta->type) === 'option') { $out = call_user_func_array($update_value_fn, [unpapify($meta->slug), $value]); $result = $out ? $result : $out; } else { $out = call_user_func_array($update_value_fn, [$meta->type, $meta->id, unpapify($meta->slug), $value]); $result = $out ? $result : $out; } continue; } // Delete all child value caches. papi_update_property_meta_value_cache_delete($meta, $value); // Update metadata or option value for all child values. foreach ($value as $child_key => $child_value) { if (papi_is_empty($child_value)) { papi_delete_property_meta_value($meta->id, $child_key, $meta->type); } else { if (papi_get_meta_type($meta->type) === 'option') { call_user_func_array($update_value_fn, [unpapify($child_key), $child_value]); } else { call_user_func_array($update_value_fn, [$meta->type, $meta->id, unpapify($child_key), $child_value]); } } } } return $result; }
/** * Update field with new value. The old value will be deleted. * * @param int $id * @param string $slug * @param mixed $value * @param string $type * * @return bool */ function papi_update_field($id = null, $slug = null, $value = null, $type = 'post') { if (!is_numeric($id) && is_string($id)) { $type = empty($value) ? $value : $type; $value = $slug; $slug = $id; $id = null; } if (!is_string($slug) || empty($slug)) { return false; } if (papi_is_empty($value)) { return papi_delete_field($id, $slug, $type); } $id = papi_get_meta_id($type, $id); $store = papi_get_meta_store($id, $type); if (is_null($store)) { return false; } $property = $store->get_property($slug); if (!papi_is_property($property)) { return false; } papi_delete_field($id, $slug, $type); $value = $property->update_value($value, $slug, $id); $value = papi_filter_update_value($property->get_option('type'), $value, $slug, $id, $type); return papi_update_property_meta_value(['type' => $type, 'id' => $id, 'slug' => $slug, 'value' => $value]); }
/** * Render property html. */ public function html() { // Setup variables needed. $settings = $this->get_settings(); $value = $this->get_value(); $options_html = []; // Properties that extends dropdown property // maybe don't have this setting. if (!isset($settings->multiple)) { $settings->multiple = false; } // Override selected setting with // database value if not empty. if (!papi_is_empty($value)) { $settings->selected = $value; } $classes = 'papi-fullwidth'; if ($settings->select2) { $classes .= ' papi-component-select2'; } // Add placeholder if any. if (!empty($settings->placeholder)) { $options_html[] = papi_html_tag('option', ['value' => '', $settings->placeholder]); } // Create option html tags for all items. foreach ($this->get_items() as $key => $value) { $key = is_numeric($key) ? $value : $key; if (papi_is_empty($key)) { continue; } // When a multiple we need to check with // `in_array` since the value is a array. if ($settings->multiple) { $selected = in_array($value, $settings->selected, true); } else { $selected = $value === $settings->selected; } $options_html[] = papi_html_tag('option', ['data-edit-url' => get_edit_post_link($value), 'selected' => $selected ? 'selected' : null, 'value' => $value, esc_html(papi_convert_to_string($key))]); } // When selectize is empty this will be used. papi_render_html_tag('input', ['name' => $this->html_name() . ($settings->multiple ? '[]' : ''), 'type' => 'hidden']); papi_render_html_tag('select', ['class' => $classes, 'data-allow-clear' => !empty($settings->placeholder), 'data-placeholder' => $settings->placeholder, 'data-width' => '100%', 'id' => $this->html_id() . ($settings->multiple ? '[]' : ''), 'multiple' => $settings->multiple ? 'multiple' : null, 'name' => $this->html_name() . ($settings->multiple ? '[]' : ''), $options_html]); }
/** * Setup properties. */ protected function setup_properties() { $this->conditional = new Papi_Core_Conditional(); if ($this->default_options['sort_order'] === -1) { $this->default_options['sort_order'] = papi_filter_settings_sort_order(); } if (papi_is_empty($this->default_options['post_type'])) { $this->default_options['post_type'] = papi_get_post_type(); } }
/** * Get html tag from tag name and array of attributes. * * @param string $tag * @param array $attr * * @return string */ function papi_html_tag($tag, $attr = []) { $attributes = []; $content = []; if (!is_array($attr)) { $attr = [$attr]; } foreach ($attr as $key => $value) { if (is_numeric($key)) { if (is_array($value)) { $content[] = implode(' ', $value); } else { $content[] = $value; } continue; } if (is_array($value) || is_object($value)) { $value = json_encode($value); } else { if (is_bool($value)) { $value = $value ? 'true' : 'false'; } else { if (is_string($value)) { $value = trim($value); } } } if (is_null($value)) { continue; } $attributes[] = sprintf('%s="%s"', $key, esc_attr($value)); } if (papi_is_empty($content)) { $end = '>'; } else { $end = sprintf('>%s</%s>', implode(' ', $content), $tag); } if (!empty($attributes)) { $attributes = ' ' . implode(' ', $attributes); } else { $attributes = ''; } return sprintf('<%s%s%s', $tag, $attributes, $end); }
/** * 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; }
/** * 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; }
/** * 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 }
/** * Restore to post revision. * * @param int $post_id * @param int $revision_id */ public function restore_post_revision($post_id, $revision_id) { $slugs = papi_get_slugs($revision_id, true); foreach ($slugs as $slug) { $value = papi_get_field($revision_id, $slug); if (papi_is_empty($value)) { papi_delete_field($post_id, $slug); } else { papi_update_field($post_id, $slug, $value); } } }
/** * Render the final html that is displayed in the table. */ protected function render_row_html() { $display_class = $this->display() ? '' : ' papi-hide'; $rules_class = papi_is_empty($this->get_rules()) ? '' : ' papi-rules-exists'; $css_class = trim($display_class . $rules_class); $css_class .= sprintf(' papi-layout-%s', $this->get_option('layout')); if ($this->get_option('raw')) { echo sprintf('<div class="%s">', esc_attr($css_class)); $this->render_property_html(); echo '</div>'; } else { ?> <tr class="<?php echo esc_attr($css_class); ?> "> <?php if ($this->get_option('sidebar')) { ?> <td class="papi-table-sidebar"> <?php $this->render_label_html(); $this->render_description_html(); ?> </td> <?php } ?> <td <?php echo $this->get_option('sidebar') ? '' : 'colspan="2"'; ?> > <?php // Render vertical layout where title and description is // above the property. if ($this->get_option('layout') === 'vertical') { $this->render_label_html(); $this->render_description_html(); } $this->render_property_html(); ?> </td> </tr> <?php } }
/** * Setup default options values. * All default values can't be set in the `$default_options` array. */ private function setup_default_options() { if ($this->default_options['sort_order'] === -1) { $this->default_options['sort_order'] = papi_filter_settings_sort_order(); } if (empty($this->default_options['post_type'])) { $this->default_options['post_type'] = papi_get_post_type(); } if (papi_is_empty($this->default_options['default'])) { $this->default_options['default'] = $this->default_value; } }
/** * 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; }
/** * Render property html. */ public function html() { $settings = $this->get_settings(); $layout = $settings->layout; $labels = $this->get_labels(); $taxonomies = $this->get_taxonomies(); $render_label = count($taxonomies) > 1; $advanced = $render_label && $layout === 'advanced'; $single = $render_label && $layout !== 'advanced'; $classes = count($taxonomies) > 1 ? '' : 'papi-fullwidth'; $value = $this->get_value(); $value = is_object($value) ? $value->term_id : 0; $selected_label = reset($labels); $selected_term = $this->get_term($value) ?: ''; $selected_term = is_wp_error($selected_term) || empty($selected_term) ? '' : $selected_term; $selected_taxonomy = empty($selected_term) ? reset($taxonomies) : $selected_term->taxonomy; if ($settings->select2) { $classes = ' papi-component-select2'; } ?> <div class="papi-property-term <?php echo $advanced ? 'advanced' : ''; ?> "> <?php if ($advanced) { ?> <table class="papi-table"> <tr> <td> <label for="<?php echo esc_attr($this->html_id()); ?> _taxonomy"> <?php echo esc_html($settings->labels['select_taxonomy']); ?> </label> </td> <td> <select id="<?php echo esc_attr($this->html_id()); ?> _taxonomy" class="<?php echo esc_attr($classes); ?> papi-property-term-left" data-select-item="<?php echo esc_attr($settings->labels['select_item']); ?> " data-term-query='<?php echo esc_attr(papi_maybe_json_encode($settings->query)); ?> ' data-width="100%" > <?php foreach ($labels as $taxonomy => $label) { papi_render_html_tag('option', ['value' => $taxonomy, 'selected' => $taxonomy === $selected_taxonomy, $label]); if ($selected) { $selected_label = $label; } } ?> </select> </td> </tr> <tr> <td> <label for="<?php echo esc_attr($this->html_id()); ?> _terms"> <?php echo esc_html(sprintf($settings->labels['select_item'], $selected_label)); ?> </label> </td> <td> <?php } ?> <select class="<?php echo esc_attr($classes); ?> papi-property-term-right" id="<?php echo esc_attr($this->html_id()); ?> _terms" name="<?php echo esc_attr($this->html_name()); ?> " class="<?php echo esc_attr($classes); ?> " data-allow-clear="<?php echo empty($settings->placeholder) ? 'false' : 'true'; ?> " data-placeholder="<?php echo esc_attr(isset($settings->placeholder) ? $settings->placeholder : ''); ?> " data-width="100%"> <?php if (!empty($settings->placeholder)) { ?> <option value=""></option> <?php } ?> <?php foreach ($taxonomies as $taxonomy) { ?> <?php if ($advanced && $taxonomy !== $selected_taxonomy) { continue; } $terms = $this->get_terms($taxonomy); if (empty($terms)) { continue; } ?> <?php if ($single) { ?> <optgroup label="<?php echo esc_attr($labels[$taxonomy]); ?> "> <?php } ?> <?php foreach ($terms as $term_id => $term_name) { if (papi_is_empty($term_name)) { continue; } papi_render_html_tag('option', ['value' => $term_id, 'selected' => $value === $term_id, esc_html($term_name)]); } ?> <?php if ($single) { ?> </optgroup> <?php } ?> <?php } ?> </select> <?php if ($advanced) { ?> </td> </tr> </table> <?php } ?> </div> <?php }
/** * Format the value of the property before it's returned * to WordPress admin or the site. * * @param mixed $values * @param string $repeater_slug * @param int $post_id * * @return array */ public function format_value($values, $repeater_slug, $post_id) { if (!is_array($values)) { return []; } $values = papi_to_property_array_slugs($values, $repeater_slug); foreach ($values as $slug => $value) { if (papi_is_property_type_key($slug)) { continue; } $property_type_slug = papi_get_property_type_key_f($slug); if (!isset($values[$property_type_slug])) { continue; } // Get property type $property_type_value = $values[$property_type_slug]; $property_type = papi_get_property_type($property_type_value); if (!is_object($property_type)) { continue; } // Get property child slug. $child_slug = $this->get_child_slug($repeater_slug, $slug); // Get raw value from cache if enabled. if ($this->cache) { $raw_value = papi_cache_get($slug, $post_id, $this->get_meta_type()); } else { $raw_value = false; } // Load the value. if ($raw_value === null || $raw_value === false) { $values[$slug] = $property_type->load_value($value, $child_slug, $post_id); $values[$slug] = papi_filter_load_value($property_type->type, $values[$slug], $child_slug, $post_id, papi_get_meta_type()); if (!papi_is_empty($values[$slug]) && $this->cache) { papi_cache_set($slug, $post_id, $values[$slug], $this->get_meta_type()); } } else { $values[$slug] = $raw_value; } // Format the value from the property class. $values[$slug] = $property_type->format_value($values[$slug], $child_slug, $post_id); if (!is_admin()) { $values[$slug] = papi_filter_format_value($property_type->type, $values[$slug], $child_slug, $post_id, papi_get_meta_type()); } $values[$property_type_slug] = $property_type_value; } if (!is_admin()) { foreach ($values as $slug => $value) { if (papi_is_property_type_key($slug)) { unset($values[$slug]); } } } return papi_from_property_array_slugs($values, $repeater_slug); }
/** * Update property with new value. The old value will be deleted. * * @param int $term_id * @param string $slug * @param mixed $value * * @return bool */ function papi_update_term_field($term_id = null, $slug = null, $value = null) { if (!is_numeric($term_id) && is_string($term_id)) { $value = $slug; $slug = $term_id; $term_id = null; } if (!is_string($slug) || empty($slug)) { return false; } if (papi_is_empty($value)) { return papi_delete_term_field($term_id, $slug); } return papi_update_field(papi_get_term_id($term_id), $slug, $value, 'term'); }
/** * Render property html. */ public function html() { $layout = $this->get_setting('layout'); $labels = $this->get_labels(); $post_types = $this->get_post_types(); $render_label = count($post_types) > 1; $advanced = $render_label && $layout === 'advanced'; $single = $render_label && $layout !== 'advanced'; $classes = count($post_types) > 1 ? '' : 'papi-fullwidth'; $settings = $this->get_settings(); $value = $this->get_value(); $value = is_object($value) ? $value->ID : 0; $selected_label = array_shift($labels); $selected_post_type = get_post_type($value) ?: ''; $posts = $this->get_posts($selected_post_type); if ($settings->select2) { $classes .= ' papi-component-select2'; } ?> <div class="papi-property-post <?php echo $advanced ? 'advanced' : ''; ?> "> <?php if ($advanced) { ?> <table class="papi-table"> <tr> <td> <label for="<?php echo esc_attr($this->html_id()); ?> _post_type"> <?php echo esc_html($settings->labels['select_post_type']); ?> </label> </td> <td> <select id="<?php echo esc_attr($this->html_id()); ?> _post_type" class="<?php echo esc_attr($classes); ?> papi-property-post-left" data-select-item="<?php echo esc_attr($settings->labels['select_item']); ?> " data-post-query='<?php echo esc_attr(papi_maybe_json_encode($settings->query)); ?> ' data-width="100%" > <?php foreach ($labels as $post_type => $label) { papi_render_html_tag('option', ['value' => $post_type, 'selected' => $post_type === $selected_post_type, $label]); if ($selected) { $selected_label = $label; } } ?> </select> </td> </tr> <tr> <td> <label for="<?php echo esc_attr($this->html_id()); ?> _posts"> <?php echo esc_html(sprintf($settings->labels['select_item'], $selected_label)); ?> </label> </td> <td> <?php } ?> <select class="<?php echo esc_attr($classes); ?> papi-property-post-right" id="<?php echo esc_attr($this->html_id()); ?> _posts" name="<?php echo esc_attr($this->html_name()); ?> " data-allow-clear="<?php echo empty($settings->placeholder) ? 'false' : 'true'; ?> " data-placeholder="<?php echo esc_attr($settings->placeholder); ?> " data-width="100%" > <?php if (!empty($settings->placeholder)) { ?> <option value=""></option> <?php } ?> <?php foreach ($posts as $label => $items) { ?> <?php if ($single) { ?> <optgroup label="<?php echo esc_attr($label); ?> "> <?php } ?> <?php foreach ($items as $post) { if (papi_is_empty($post->post_title)) { continue; } papi_render_html_tag('option', ['data-edit-url' => get_edit_post_link($value), 'selected' => $value === $post->ID, 'value' => $post->ID, $post->post_title]); } ?> <?php if ($single) { ?> </optgroup> <?php } ?> <?php } ?> </select> <?php if ($advanced) { ?> </td> </tr> </table> <?php } ?> </div> <?php }
/** * 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]); }
/** * Empty conditional rule. * * @param Papi_Core_Conditional_Rule $rule * * @return bool */ public function rule_not_empty(Papi_Core_Conditional_Rule $rule) { return !papi_is_empty($this->get_value($rule)); }