/** * Add new meta box with properties. * * @param mixed $file_or_options * @param array $properties */ protected function box($file_or_options = [], $properties = []) { if (!is_string($file_or_options) && !is_array($file_or_options) && !is_object($file_or_options)) { return; } list($options, $properties) = papi_get_options_and_properties($file_or_options, $properties, true); $post_type = $this->get_post_type(); // Check so we have a post the to add the box to. // @codeCoverageIgnoreStart if (!$this->load_boxes && (empty($post_type) || !$this->has_post_type($post_type))) { return; } // @codeCoverageIgnoreEnd // Add post type to the options array. $options['_post_type'] = $post_type; if (isset($options['sort_order'])) { $sort_order = intval($options['sort_order']); } else { $sort_order = papi_filter_settings_sort_order(); } if (is_callable($properties)) { $properties = call_user_func($properties); } // Check and convert all non properties objects to properties objects. $properties = $this->convert_properties($properties); $options['title'] = papi_esc_html(isset($options['title']) ? $options['title'] : ''); array_push($this->boxes, [$options, $properties, 'sort_order' => $sort_order, 'title' => $options['title']]); }
/** * Get tabs options. * * @param array|object $options * * @return stdClass */ function papi_get_tab_options($options) { if (!is_array($options) && !is_object($options) || empty($options)) { return; } if (is_object($options)) { $options = (array) $options; } $defaults = ['capabilities' => [], 'icon' => '', 'sort_order' => papi_filter_settings_sort_order(), '_name' => '']; $options = array_merge($defaults, $options); return (object) $options; }
/** * 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(); } }
/** * Setup page type variables. */ private function setup_page_type() { if (is_null($this->sort_order)) { $this->sort_order = papi_filter_settings_sort_order(); } }
/** * 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; } }
/** * Setup meta data. */ protected function setup_meta_data() { foreach ($this->get_meta() as $key => $value) { if (substr($key, 0, 1) === '_') { continue; } $this->{$key} = papi_esc_html($value); } if ($this->sort_order === 1000) { $this->sort_order = papi_filter_settings_sort_order(); } }
/** * Sort array based on given key and numeric value. * * @param array $array * @param string $key * * @return array */ function papi_sort_order($array, $key = 'sort_order') { if (empty($array) || !is_array($array) && !is_object($array)) { return []; } if (is_object($array)) { $array = papi_to_array($array); } $sorter = []; foreach ($array as $k => $value) { if (is_object($value)) { if (isset($value->{$key})) { $sorter[$k] = $value->{$key}; } else { if (isset($value->options->{$key})) { $sorter[$k] = $value->options->{$key}; } } } else { if (is_array($value) && isset($value[$key])) { $sorter[$k] = $value[$key]; } } } $i = 0; $default_sort = papi_filter_settings_sort_order(); foreach ($sorter as $k => $v) { if ($default_sort === $v) { $sorter[$k] = $v - $i; $i++; } } asort($sorter, SORT_NUMERIC); $result = []; $rest = []; foreach ($sorter as $k => $v) { $value = $array[$k]; if (is_object($value) && (!isset($value->options) && !isset($value->options->{$key}) || !isset($value->{$key})) || is_array($value) && !isset($value[$key])) { $rest[] = $value; } else { $result[$k] = $array[$k]; } } $result = array_values($result); foreach ($rest as $key => $value) { $result[] = $value; } return $result; }
/** * Sort array based on given key and numeric value. * * @param array $array * @param string $key * * @return array */ function papi_sort_order($array, $key = 'sort_order') { if (empty($array) || !is_array($array) && !is_object($array)) { return []; } if (is_object($array)) { $array = papi_to_array($array); } $sorter = []; foreach ($array as $k => $value) { $value = is_array($value) ? (object) $value : $value; if (is_object($value) && isset($value->{$key})) { $sorter[$k] = $value->{$key}; continue; } } $i = 0; $default_sort = papi_filter_settings_sort_order(); foreach ($sorter as $k => $v) { if ($default_sort === $v) { $sorter[$k] = $v - $i; $i++; } } asort($sorter, SORT_NUMERIC); $result = []; foreach ($sorter as $k => $v) { $value = $array[$k]; $value = is_array($value) ? (object) $value : $value; if (is_object($value) && isset($value->{$key})) { $result[$k] = $array[$k]; } } return array_values($result); }