/** * @param string $optionId * @param array $options * @return mixed False if option not set */ public function find($optionId, $options = array()) { $options = array_merge(array('booleanize' => false), $options); $optionId = $this->gummOptionId($optionId); if (strpos($optionId, '.') !== false) { $optionPath = explode('.', $optionId); $rootOptionId = array_shift($optionPath); $rootValue = get_option($rootOptionId); if ($rootValue) { $optionXPath = implode('/', $optionPath); $extractedValue = Set::extract('/Option/' . $optionXPath, array('Option' => $rootValue)); $optionLastId = end($optionPath); $parsedValue = array(); if ($extractedValue) { foreach ($extractedValue as $val) { if (is_array($val)) { if (isset($val[$optionLastId])) { $val = $val[$optionLastId]; } $parsedValue = array_merge($parsedValue, $val); } else { // We are working with ids, so extract will match only one id, and if not array - we need the string returned $parsedValue = $val; break; } } if ($options['booleanize']) { $parsedValue = Set::booleanize($parsedValue); } if (is_array($parsedValue)) { $parsedValue = Set::applyNative(Set::filter($parsedValue), 'stripslashes'); } elseif ($parsedValue !== false) { $parsedValue = stripslashes($parsedValue); } return $parsedValue; } } } // if (strpos($optionId, '[') !== false) { // if (preg_match_all("'(.*)(\[(.*)\])+'msU", $optionId, $out)) { // $baseOptionId = $out[1][0]; // $baseValue = get_option($baseOptionId); // if (is_array($baseValue)) { // $innerVal = Set::extract('/Option/' . implode('/', $out[3]), array('Option' => $baseValue)); // if ($innerVal) return reset($innerVal); // } // } // } $value = get_option($optionId); if ($value === false) { $configOption = $this->getConfigOption($optionId); if (isset($configOption['default'])) { $value = $configOption['default']; if (isset($optionPath) && is_array($optionPath)) { $optionLastId = end($optionPath); if (isset($optionLastId) && $value && is_array($value) && isset($value[$optionLastId])) { $value = $value[$optionLastId]; } } } elseif (isset($configOption['options'])) { $value = array(); foreach ($configOption['options'] as $configSubOption) { if (preg_match_all("'(.*)(\\[(.*)\\])+'msU", $configSubOption['id'], $out)) { $valueKey = end($out[3]); $value[$valueKey] = $this->find($configSubOption['id']); } } } } if (is_array($value)) { $value = Set::applyNative(Set::filter($value), 'stripslashes'); } elseif ($value !== false) { $value = stripslashes($value); } if ($optionId === GUMM_THEME_PREFIX . '_email' && !$value) { $value = get_option('admin_email'); } if ($options['booleanize']) { $value = Set::booleanize($value); } return $value; }
/** * @param array $options * @param array $attributes * @param array $settings * @return string */ public function postTypePicker(array $option, array $attributes, array $settings) { $postTypes = array_merge(array('post' => 'post'), get_post_types(array('capability_type' => 'post', '_builtin' => false, 'public' => true))); $postTypes = Set::applyNative($postTypes, 'ucwords'); // if (isset($attributes['name'])) $option['id'] = $attributes['name']; $originalValue = $this->value; $value = null; $val = 'post'; if ($originalValue && is_array($originalValue) && isset($originalValue['post_type'])) { $val = $value = $originalValue['post_type']; } $outputHtml = ''; // $outputHtml = '<div class="layout-element-post-type-select">'; $theInputField = array('id' => $option['id'] . '.post_type', 'name' => $option['name'], 'inputOptions' => $postTypes, 'type' => 'tabbed-input', 'tabs' => array()); foreach ($postTypes as $postType => $postTypeName) { $inputOptions = array(); $termName = $postType == 'post' ? 'category' : $postType . '_category'; if ($terms = get_terms($termName)) { if (!isset($terms->errors)) { foreach ($terms as $term) { $inputOptions[$term->term_id] = $term->name; } } } $hiddenClass = $postType == $val ? '' : ' hidden'; $value = null; if ($originalValue && is_array($originalValue) && isset($originalValue[$postType . '-category'])) { $value = $originalValue[$postType . '-category']; } $theCategoriesContent = '<em>' . __('There are no available categories for this post type.', 'gummfw') . '</em>'; if ($inputOptions) { $theCategoriesContent = $this->input($this->model, array('name' => ucwords($postType) . ' ' . __('Category', 'gummfw'), 'id' => $option['id'] . '.' . $postType . '-category', 'type' => 'checkboxes', 'inputOptions' => $inputOptions), array('value' => $value)); } $theInputField['tabs'][] = $theCategoriesContent; } $outputHtml .= $this->input($this->model, $theInputField, array('value' => $val)); $value = null; if ($originalValue && is_array($originalValue) && isset($originalValue['posts_number'])) { $value = $originalValue['posts_number']; } $outputHtml .= $this->input($this->model, array('name' => __('Number of posts', 'gummfw'), 'id' => $option['id'] . '.posts_number', 'type' => 'number', 'default' => 4), array('value' => $value), array('slider' => array('min' => 1, 'max' => 50, 'numberType' => ''))); // $outputHtml .= '</div>'; return $outputHtml; }
protected function _postTypeFields() { if (isset($this->params['postType'])) { $val = $this->params['postType']; } elseif (isset($this->supports['postType']) && isset($this->supports['postType']['value'])) { $val = $this->supports['postType']['value']; } elseif (isset($this->supports['postType'])) { $val = $this->supports['postType']; } else { $val = 'post'; } $postTypes = array_merge(array('post' => 'post'), get_post_types(array('capability_type' => 'post', '_builtin' => false, 'public' => true))); $postTypes = Set::applyNative($postTypes, 'ucwords'); $fields = array('postType' => array('name' => __('Data Source', 'gummfw'), 'type' => 'tabbed-input', 'inputOptions' => $postTypes, 'value' => $val, 'inputAttributes' => array('class' => 'layout-element-post-type-select-input gumm-input'), 'tabs' => array()), 'postOrder' => array('name' => __('Order', 'gummfw'), 'type' => 'tabbed-input', 'inputOptions' => array('default' => __('Default', 'gummfw'), 'custom' => __('Custom', 'gummfw')), 'value' => 'default', 'tabs' => array(array('tabText' => __('Default WordPress ordering will be used.', 'gummfw')), array('postOrderBy' => array('name' => __('Order by:', 'gummfw'), 'type' => 'select', 'inputOptions' => array('title' => __('Title', 'gummfw'), 'name' => __('Post name (post slug)', 'gummfw'), 'date' => __('Date'), 'modified' => __('Last modified date', 'gummfw'), 'menu_order' => __('Page/post order field', 'gummfw'), 'rand' => __('Random', 'gummfw')), 'value' => 'date'), 'postOrderDirection' => array('name' => __('Order direction:', 'gummfw'), 'type' => 'select', 'inputOptions' => array('DESC' => __('Descending', 'gummfw'), 'ASC' => __('Ascending', 'gummfw')), 'value' => 'DESC'))))); foreach ($postTypes as $postType => $postTypeName) { $categoryField = $this->_categoriesFields($postType); $tabContentInput = $categoryField[$postType . '-category']; if (count($categoryField[$postType . '-category']['inputOptions']) === 0) { $tabContentInput = array('tabText' => __('There are no available categories for this post type.', 'gummfw')); } $fields['postType']['tabs'][][$postType . '-category'] = $tabContentInput; } if (isset($this->supports['postType']) && isset($this->supports['postType']['flickr']) && $this->supports['postType']['flickr']) { $fields['postType']['inputOptions']['flickr'] = __('Flickr', 'gummfw'); $fields['postType']['tabs'][] = array('flickrUser' => array('name' => __('Flickr username', 'gummfw'), 'type' => 'text')); } $fields['postType']['inputOptions']['default'] = __('Default', 'gummfw'); $fields['postType']['tabs'][] = array('tabText' => __('Use default WordPress query.', 'gummfw', 'gummfw')); return $fields; }
/** * Allows the application of a native php callback function to elements of an array * * @param array $data An array of data to extract from & then process with the $callback. * @param mixed $callback Callback method to be applied to extracted data. * See http://ca2.php.net/manual/en/language.pseudo-types.php#language.types.callback for examples * of callback formats. * @param string $to Can be 'values', 'keys', 'both' * @return mixed Result of the callback when applied to extracted data * @access public * @static */ public static function applyNative($data, $callback, $to = 'values') { $parsed = array(); if ($data) { foreach ($data as $key => $value) { if (in_array($to, array('keys', 'both'))) { $key = $callback($key); } if (in_array($to, array('values', 'both'))) { if (is_array($value)) { $value = Set::applyNative($value, $callback, $to); } else { $value = $callback($value); } $parsed[$key] = $value; } } } return $parsed; }