/** * Output hidden input field that cointains which property is used. */ protected function render_hidden_html() { $slug = $this->get_option('slug'); if (substr($slug, -1) === ']') { $slug = substr($slug, 0, -1); $slug = papi_get_property_type_key($slug); $slug .= ']'; } else { $slug = papi_get_property_type_key($slug); } $slug = papify($slug); $options = $this->get_options(); $property_json = base64_encode(papi_maybe_json_encode($options)); papi_render_html_tag('input', ['data-property' => strtolower($this->get_option('type')), 'name' => $slug, 'type' => 'hidden', 'value' => $property_json]); }
/** * Update value before it's saved to the database. * * @param mixed $value * @param string $slug * @param int $post_id * * @return mixed */ public function update_value($value, $slug, $post_id) { if (!($value = $this->prepare_value($value))) { return; } return papi_maybe_json_encode($value); }
/** * Update value before it's saved to the database. * * @param mixed $value * @param string $slug * @param int $post_id * * @return mixed */ public function update_value($value, $slug, $post_id) { return papi_maybe_json_encode($value); }
/** * Render layout JSON template. * * @param string $slug */ protected function render_json_template($slug) { $options = $this->get_options(); foreach ($options->settings->items as $key => $value) { if (!isset($value['items'])) { continue; } foreach ($value['items'] as $index => $property) { $property = $this->prepare_property_for_json(papi_property($property)); if ($property === false) { unset($options->settings->items[$key]['items'][$index]); continue; } $options->settings->items[$key]['items'][$index] = $property; } } papi_render_html_tag('script', ['data-papi-json' => esc_attr(sprintf('%s_repeater_json', $slug)), 'type' => 'application/json', papi_maybe_json_encode([$options])]); }
/** * 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 }
/** * Render Conditional rules as script tag with JSON. */ protected function render_rules_json() { $rules = $this->get_rules(); if (empty($rules)) { return; } $rules = $this->conditional->prepare_rules($rules, $this); papi_render_html_tag('script', ['data-papi-rule-source-slug' => $this->html_name(), 'data-papi-rules' => 'true', 'type' => 'application/json', papi_maybe_json_encode($rules)]); }
/** * 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 }
/** * 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 }
/** * 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; } switch ($key) { case 'checked': $value = $value === true ? 'checked' : $value; $value = is_bool($value) ? null : $value; break; case 'selected': $value = $value === true ? 'selected' : $value; $value = is_bool($value) ? null : $value; break; default: break; } if (is_array($value) || is_object($value)) { $value = papi_maybe_json_encode($value); } else { if (is_bool($value)) { $value = $value ? 'true' : 'false'; } else { if (is_string($value)) { $value = trim($value); } } } if (papi_is_empty($value)) { continue; } $attributes[] = sprintf('%s="%s"', $key, esc_attr($value)); } if (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); }