/** * Get Papi cache key. * * @param string $key * @param mixed $suffix * * @return string */ function papi_cache_key($key, $suffix) { if (!is_string($key)) { return ''; } $key = papify($key); $suffix = papi_convert_to_string($suffix); $suffix = papi_html_name($suffix); $suffix = papi_remove_papi($suffix); return sprintf('%s_%s', $key, $suffix); }
/** * Setup meta box. */ public function setup_meta_box() { $this->options->title = papi_remove_papi($this->options->title); if ($this->options->_required) { $this->options->title .= papi_required_html($this->properties[0], true); } add_meta_box($this->options->_id, $this->options->title, [$this, 'render_meta_box'], $this->options->_post_type, $this->options->context, $this->options->priority, $this->properties); }
/** * Get html name for property with or without sub property and row number. * * @param array|object $sub_property * @param int $row * * @return string */ public function html_name($sub_property = null, $row = null) { $base_slug = $this->get_option('slug'); if (is_null($sub_property)) { return $base_slug; } if (is_numeric($row)) { $base_slug = sprintf('%s[%d]', $base_slug, intval($row)); } if (!papi_is_property($sub_property)) { if (is_array($sub_property) || is_object($sub_property)) { $sub_property = self::factory($sub_property); } else { return $base_slug; } } return sprintf('%s[%s]', $base_slug, papi_remove_papi($sub_property->get_slug())); }
/** * Prepare properties data for saving. * * @param array $data * @param int $post_id * * @return array */ protected function prepare_properties_data(array $data = [], $post_id = 0) { // Since we are storing witch property it is in the $data array // we need to remove that and set the property type to the property // and make a array of the property type and the value. foreach ($data as $key => $value) { $property_type_key = papi_get_property_type_key(); if (strpos($key, $property_type_key) === false) { continue; } $property_key = str_replace($property_type_key, '', $key); // Check if value exists. if (isset($data[$property_key])) { $data[$property_key] = ['type' => $value, 'value' => $data[$property_key]]; } unset($data[$key]); } foreach ($data as $key => $item) { $property = papi_get_property_type($item['type']); unset($data[$key]); if (papi_is_property($property)) { // Run `update_value` method on the property class. $data[$key] = $property->update_value($item['value'], papi_remove_papi($key), $post_id); // Apply `update_value` filter so this can be changed from the theme for specified property type. $data[$key] = papi_filter_update_value($item['type']->type, $data[$key], papi_remove_papi($key), $post_id); if ($item['type']->overwrite) { $slug = papi_remove_papi($key); $this->overwrite[$slug] = $data[$key]; } } } return $data; }
/** * 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; }
/** * 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 repeater row. */ protected function render_repeater_row() { $layouts = $this->get_settings_layouts(); $values = $this->get_value(); // Fetch all slugs in all layouts. $slugs = []; foreach ($layouts as $index => $layout) { foreach ($layout['items'] as $item) { $slugs[] = papi_remove_papi($item->slug); } } // Remove values that don't exists in the slugs array. foreach ($values as $index => $row) { $keys = array_keys($row); foreach ($row as $slug => $value) { if (in_array($slug, $keys) || papi_is_property_type_key($slug) || $this->is_layout_key($slug)) { continue; } unset($values[$index][$slug]); } } $values = array_filter($values); $closed_rows = $this->get_setting('closed_rows', true); foreach ($values as $index => $row) { ?> <tr <?php echo $closed_rows ? 'class="closed"' : ''; ?> > <td class="handle"> <span class="toggle"></span> <span class="count"><?php echo $this->counter + 1; ?> </span> </td> <?php foreach ($layouts as $layout) { // Don't render layouts that don't have a valid value in the database. if (!isset($row[$this->layout_key]) || $layout['slug'] !== $row[$this->layout_key]) { continue; } // Render all properties in the layout $this->render_properties($layout['items'], $row); } $this->counter++; ?> <td class="last"> <span> <a title="<?php _e('Remove', 'papi'); ?> " href="#" class="repeater-remove-item">x</a> </span> </td> </tr> <?php } }
/** * Change value after it's loaded from the database * and populate every property in the repeater with the right property type. * * @param int $value * @param string $repeater_slug * @param int $post_id * * @return array */ public function load_value($value, $repeater_slug, $post_id) { if (is_array($value)) { return $value; } list($results, $trash) = $this->get_results($value, $repeater_slug, $post_id); // Will not need this array. unset($trash); $page = $this->get_page(); $types = []; $results = papi_from_property_array_slugs($results, papi_remove_papi($repeater_slug)); if (empty($page) || empty($results)) { return $this->default_value; } foreach ($results[0] as $slug => $value) { if ($property = $page->get_property($repeater_slug, $slug)) { $types[$slug] = $property; } } foreach ($results as $index => $row) { foreach ($row as $slug => $value) { if (!isset($types[$slug])) { continue; } $type_key = papi_get_property_type_key_f($slug); $results[$index][$type_key] = $types[$slug]; } } return $results; }
/** * Get value from property. * * @param string $slug * * @return mixed */ public function get_value($slug) { $slug = papi_remove_papi($slug); $value = papi_get_property_meta_value($this->id, $slug, $this->type); return $this->convert($slug, $value); }