public function widget($arguments, $instance)
 {
     extract($arguments);
     $this->register_widgets();
     $instance = piklist::object_value($instance);
     $widget = $instance['widget'];
     if (!empty($widget)) {
         unset($instance['widget']);
         $this->widgets[$widget]['instance'] = $instance;
         piklist_widget::$current_widget = $this->widget_name;
         do_action('piklist_pre_render_widget', $this->widgets[$widget]);
         piklist::render($this->widgets[$widget]['path'], array('instance' => $instance, 'settings' => $instance, 'before_widget' => str_replace('class="', 'class="' . piklist::dashes($this->widgets[$widget]['add_on'] . ' ' . $this->widgets[$widget]['name']) . ' ' . $this->widgets[$widget]['data']['class'] . ' ', $before_widget), 'after_widget' => $after_widget, 'before_title' => $before_title, 'after_title' => $after_title));
         do_action('piklist_post_render_widget', $this->widgets[$widget]);
     }
 }
 /**
  * ajax
  * Render the widget form.
  *
  * @access public
  * @static
  * @since 1.0
  */
 public function ajax()
 {
     global $wp_widget_factory;
     $widget = isset($_REQUEST['widget']) ? esc_attr($_REQUEST['widget']) : null;
     if ($widget && current_user_can('edit_theme_options')) {
         $this->setup($this->widget_name);
         if (isset($_REQUEST['number'])) {
             $instances = get_option('widget_' . piklist::dashes($this->widget_name));
             piklist_widget::widget()->_set($_REQUEST['number']);
             if (isset($instances[$_REQUEST['number']])) {
                 piklist_widget::widget()->instance = $instances[$_REQUEST['number']];
             }
         }
         if (isset($this->widgets[$widget])) {
             ob_start();
             do_action('piklist_notices');
             foreach ($this->widgets[$widget]['render'] as $render) {
                 if (strstr($render, '-form.php')) {
                     piklist::render($render);
                 }
             }
             piklist_form::save_fields();
             $output = ob_get_contents();
             ob_end_clean();
             wp_send_json(array('form' => $output, 'widget' => $this->widgets[$widget]));
         }
     }
     wp_send_json_error();
 }
Beispiel #3
0
 /**
  * get_field_value
  * Get the field value.
  *
  * @param string $scope The field scope.
  * @param string $field The field field attribute.
  * @param string $type The field type.
  * @param int $id The field object id
  * @param bool $unique Whether to get a unique value or not.
  *
  * @return mixed The field value.
  *
  * @access public
  * @static
  * @since 1.0
  */
 public static function get_field_value($scope, $field, $type, $id = false, $unique = false)
 {
     global $wpdb;
     if (piklist_admin::is_widget()) {
         return isset(piklist_widget::widget()->instance[$field['field']]) ? maybe_unserialize(piklist_widget::widget()->instance[$field['field']]) : $field['value'];
     }
     $key = $field['field'] ? $field['field'] : null;
     $prefix = !in_array($scope, array(piklist::$prefix, false)) ? piklist::$prefix : null;
     if ($id || $type == 'option') {
         switch ($type) {
             case 'post':
             case 'user':
             case 'comment':
                 switch ($type) {
                     case 'user':
                         $method = 'get_userdata';
                         break;
                     case 'comment':
                         $method = 'get_comment';
                         break;
                     default:
                         $method = 'get_post';
                         break;
                 }
                 $return = array();
                 $ids = is_array($id) ? $id : array($id);
                 foreach ($ids as $id) {
                     $object = $method($id);
                     $allowed = true;
                     if ($type == 'post' && $object->post_status == 'auto-draft') {
                         $allowed = false;
                     }
                     if ($type == 'user' && $field['field'] == 'user_pass') {
                         $allowed = false;
                     }
                     if (!is_wp_error($object) && is_object($object) && $allowed) {
                         array_push($return, $object->{$field}['field'] ? $object->{$field}['field'] : $field['value']);
                     }
                 }
                 return !empty($return) ? count($return) == 1 ? current($return) : $return : $field['value'];
                 break;
             case 'option':
                 if ($type != $scope) {
                     $options = get_option($scope);
                     if (stristr($key, ':')) {
                         $value = piklist::array_path_get($options, explode(':', $key));
                     } else {
                         $value = isset($options[$key]) ? $options[$key] : $field['value'];
                     }
                     return $value;
                 } else {
                     if ($field['field'] && !stristr($field['field'], ':')) {
                         if (!isset($field['options']['type'])) {
                             return get_option($field['field'], $field['value']);
                         } elseif ($field['options']['type'] == 'blog' && $field['object_id']) {
                             return get_blog_option($field['object_id'], $field['field'], $field['value']);
                         } elseif ($field['options']['type'] == 'user' && $field['object_id']) {
                             return get_user_option($field['field'], $field['object_id']);
                         } elseif ($field['options']['type'] == 'site') {
                             $use_cache = isset($field['options']['use_cache']) ? $field['options']['use_cache'] : true;
                             return get_site_option($field['field'], $field['value'], $use_cache);
                         }
                     }
                 }
                 break;
             case 'taxonomy':
                 $key = $field['save_as'] ? $field['save_as'] : $key;
                 /**
                  * piklist_taxonomy_value_key
                  *
                  * @since 1.0
                  */
                 $terms = piklist(wp_get_object_terms($id, $key), apply_filters('piklist_taxonomy_value_key', 'term_id', $key));
                 /**
                  * piklist_taxonomy_value
                  *
                  * @since 1.0
                  */
                 $terms = apply_filters('piklist_taxonomy_value', $terms, $id, $key, $field);
                 return !empty($terms) ? $terms : false;
                 break;
             case 'post_meta':
             case 'term_meta':
             case 'user_meta':
             case 'comment_meta':
                 $meta_type = substr($type, 0, strpos($type, '_'));
                 if ($key) {
                     $meta_key = strstr($key, ':') ? substr($key, 0, strpos($key, ':')) : $key;
                     $meta_key = $field['save_as'] ? $field['save_as'] : $meta_key;
                 } else {
                     $meta_key = $scope;
                 }
                 if ($field['multiple']) {
                     switch ($type) {
                         case 'post_meta':
                             $meta_table = $wpdb->postmeta;
                             $meta_id_field = 'meta_id';
                             $meta_id = 'post_id';
                             break;
                         case 'term_meta':
                             $meta_table = $wpdb->termmeta;
                             $meta_id_field = 'meta_id';
                             $meta_id = 'term_id';
                             break;
                         case 'comment_meta':
                             $meta_table = $wpdb->commentmeta;
                             $meta_id_field = 'umeta_id';
                             $meta_id = 'comment_id';
                             break;
                         case 'user_meta':
                             $meta_table = $wpdb->usermeta;
                             $meta_id_field = 'umeta_id';
                             $meta_id = 'user_id';
                             break;
                     }
                     $keys = $wpdb->get_results($wpdb->prepare("SELECT {$meta_id_field} FROM {$meta_table} WHERE meta_key = %s AND {$meta_id} = %d", $meta_key, $id));
                     $unique = count($keys) == 1 ? true : $unique;
                 } elseif ($field['type'] == 'group' && $field['field'] && !is_numeric($field['index_force'])) {
                     $unique = true;
                 }
                 $meta = get_metadata($meta_type, $id, $meta_key, $unique);
                 if ($field['type'] == 'group' && $field['field'] && is_numeric($field['index_force'])) {
                     $meta = array_key_exists($field['index_force'], $meta) ? $meta[$field['index_force']] : null;
                 }
                 if (strstr($key, ':')) {
                     $meta = isset($meta[$meta_key]) ? $meta[$meta_key] : null;
                 }
                 if ($meta != 0) {
                     if (metadata_exists($meta_type, $id, $meta_key) && !$meta) {
                         $meta = array();
                     } elseif (!metadata_exists($meta_type, $id, $meta_key)) {
                         if ($field['value']) {
                             $meta = $field['value'];
                         } else {
                             $meta = null;
                         }
                     }
                 }
                 return $meta;
                 break;
         }
     } elseif (!$id && $key) {
         if (isset($_REQUEST[piklist::$prefix . $key])) {
             $request_value = $_REQUEST[piklist::$prefix . $key];
         } elseif (isset($_REQUEST[piklist::$prefix . $scope][$key])) {
             $request_value = $_REQUEST[piklist::$prefix . $scope][$key];
         } elseif (isset($_REQUEST[$scope][$key])) {
             $request_value = $_REQUEST[$scope][$key];
         }
         if (isset($request_value)) {
             if (is_array($request_value)) {
                 array_walk_recursive($request_value, function (&$value, $key) {
                     $value = urldecode($value);
                 });
             } else {
                 $request_value = urldecode($request_value);
             }
             return $request_value;
         }
     }
     return isset($field['value']) ? $field['value'] : null;
 }
 public static function render_field($field, $return = false)
 {
     $current_user = wp_get_current_user();
     $field = wp_parse_args($field, array('field' => false, 'type' => 'text', 'label' => false, 'description' => false, 'prefix' => true, 'scope' => null, 'value' => null, 'capability' => false, 'role' => false, 'logged_in' => false, 'add_more' => false, 'sortable' => false, 'choices' => false, 'list' => true, 'position' => false, 'template' => self::get_template(), 'wrapper' => false, 'columns' => null, 'embed' => false, 'editable' => true, 'child_field' => false, 'label_position' => 'before', 'conditions' => false, 'options' => false, 'on_post_status' => false, 'on_comment_status' => false, 'display' => false, 'group_field' => false, 'required' => false, 'index' => null, 'multiple' => false, 'errors' => false, 'attributes' => array('class' => array(), 'title' => false, 'alt' => false, 'tabindex' => false, 'columns' => null, 'value' => false), 'tax_query' => array('include_children' => true, 'field' => 'term_id', 'operator' => 'IN'), 'meta_query' => array('compare' => '=', 'type' => 'CHAR')));
     // Should this field be rendered?
     if ($field['embed'] && !$return || $field['capability'] && !current_user_can($field['capability']) || !isset($field['field']) || $field['logged_in'] && !is_user_logged_in() || $field['role'] && !piklist_user::current_user_role($field['role'])) {
         return false;
     }
     // Set default scopes based on enviroment
     if (is_null($field['scope'])) {
         if (self::is_post()) {
             $field['scope'] = 'post_meta';
         } elseif (self::is_media()) {
             $field['scope'] = 'post_meta';
         } elseif (self::is_term()) {
             $field['scope'] = 'term_meta';
         } elseif (self::is_user()) {
             $field['scope'] = 'user_meta';
         }
     }
     // Set Defaults
     array_push(self::$fields_defaults, $field);
     // Determine if its a multiple type field
     if (in_array($field['type'], self::$field_list_types['multiple_fields']) || is_array($field['attributes']) && in_array('multiple', $field['attributes'])) {
         $field['multiple'] = true;
     }
     if ($field['type'] == 'html' && !isset($field['field'])) {
         $field['field'] = piklist::unique_id();
     }
     // Manage Classes
     if (isset($field['attributes']['class'])) {
         $field['attributes']['class'] = !is_array($field['attributes']['class']) ? explode(' ', $field['attributes']['class']) : $field['attributes']['class'];
     } else {
         $field['attributes']['class'] = array();
     }
     array_push($field['attributes']['class'], piklist_form::get_field_id($field['field'], $field['scope'], false, $field['prefix']));
     // Set Wrapper
     $wrapper = array('id' => $field['type'] == 'map' ? null : self::get_field_wrapper_id($field), 'class' => array());
     // Set Columns
     if (is_numeric($field['columns']) && !$field['child_field']) {
         array_push($wrapper['class'], 'piklist-field-type-group piklist-field-column-' . $field['columns']);
     }
     if (isset($field['attributes']['columns']) && is_numeric($field['attributes']['columns'])) {
         array_push($field['attributes']['class'], 'piklist-field-column-' . $field['attributes']['columns']);
         unset($field['attributes']['columns']);
     }
     if (isset($field['attributes']['wrapper_class'])) {
         array_push($wrapper['class'], $field['attributes']['wrapper_class']);
     }
     // Check Statuses
     $status_types = apply_filters('piklist_status_types', array('post'));
     foreach ($status_types as $type) {
         $status = $field['on_' . $type . '_status'];
         if (!empty($status)) {
             $object = !empty(self::$save_ids) ? get_post(self::$save_ids[$type], ARRAY_A) : (array) $GLOBALS[$type];
             if ((is_admin() && isset($GLOBALS[$type]) || !empty(self::$save_ids)) && isset($object['post_type'])) {
                 $status_list = piklist_cpt::get_post_statuses($object['post_type']);
                 foreach (array('field', 'value', 'hide') as $status_display) {
                     if (isset($status[$status_display])) {
                         $status[$status_display] = is_array($status[$status_display]) ? $status[$status_display] : array($status[$status_display]);
                         foreach ($status[$status_display] as $_status) {
                             if (strstr($_status, '--')) {
                                 $status_range = explode('--', $_status);
                                 $status_range_start = array_search($status_range[0], $status_list);
                                 $status_range_end = array_search($status_range[1], $status_list);
                                 if (is_numeric($status_range_start) && is_numeric($status_range_end)) {
                                     $status_slice = array();
                                     for ($i = $status_range_start; $i <= $status_range_end; $i++) {
                                         array_push($status_slice, $status_list[$i]);
                                     }
                                     array_splice($status[$status_display], array_search($_status, $status[$status_display]), 1, $status_slice);
                                 }
                             }
                         }
                     }
                 }
             }
             if (isset($status['hide']) && isset($object[$type . '_status']) && piklist::check_in($status['hide'], $object[$type . '_status'] ? $object[$type . '_status'] : array('draft'))) {
                 return false;
             } elseif (isset($status['value']) && isset($object[$type . '_status']) && piklist::check_in($status['value'], $object[$type . '_status'] ? $object[$type . '_status'] : array('draft'))) {
                 $field['display'] = true;
             }
         }
     }
     // Get errors
     $field['errors'] = piklist_validate::get_errors($field['field'], $field['scope']);
     // Get field value
     if (!$field['group_field']) {
         if (piklist_validate::errors()) {
             $stored_value = piklist_validate::get_request_value($field['field'], $field['scope']);
         }
         if (!isset($stored_value)) {
             if (self::is_widget()) {
                 $stored_value = isset(piklist_widget::widget()->instance[$field['field']]) ? maybe_unserialize(piklist_widget::widget()->instance[$field['field']]) : $field['value'];
             } else {
                 $stored_value = self::get_field_value($field['scope'], $field, $field['scope'], piklist_form::get_field_object_id($field), false);
             }
         }
         if (!isset($stored_value) && !isset($field['attributes']['placeholder']) && !$field['multiple']) {
             $field['attributes']['placeholder'] = htmlspecialchars($field['value']);
         } elseif (isset($stored_value) || is_array($stored_value) && empty($stored_value)) {
             $field['value'] = $stored_value;
         }
     }
     // Check for nested fields
     if ($field['description']) {
         $field['description'] = self::render_nested_field($field, $field['description']);
     }
     if (is_array($field['choices']) && !in_array($field['type'], array('select', 'multiselect'))) {
         foreach ($field['choices'] as &$choice) {
             $choice = self::render_nested_field($field, $choice);
         }
     }
     if ($field['conditions']) {
         if ($field['display'] && empty($field['value']) && $field['type'] != 'group') {
             return false;
         }
         foreach ($field['conditions'] as &$condition) {
             if (is_array($condition)) {
                 $condition['scope'] = isset($condition['scope']) ? $condition['scope'] : $field['scope'];
                 $condition['id'] = piklist_form::get_field_id($condition['field'], $condition['scope'], false, $field['prefix']);
                 $condition['name'] = piklist_form::get_field_name($condition['field'], $condition['scope'], false, $field['prefix']);
                 $condition['reset'] = isset($condition['reset']) ? $condition['reset'] : true;
                 if (!isset($condition['type']) || empty($condition['type'])) {
                     $condition['type'] = 'toggle';
                 }
                 if (!in_array('piklist-field-condition', $field['attributes']['class'])) {
                     if (!in_array('piklist-field-condition', $wrapper['class'])) {
                         array_push($wrapper['class'], 'piklist-field-condition');
                     }
                     if (!in_array('piklist-field-condition-' . $condition['type'], $wrapper['class'])) {
                         array_push($wrapper['class'], 'piklist-field-condition-' . $condition['type']);
                     }
                 }
             }
         }
     }
     // Set the field template
     if ($field['group_field'] && self::get_template() == $field['template'] && (strstr(self::$templates[$field['template']]['template'], '</tr>') || $field['template'] == 'default')) {
         $field['child_field'] = true;
         $field['template'] = 'field';
     } elseif ($field['type'] == 'hidden' || $field['embed']) {
         $field['template'] = 'field';
     }
     $field['wrapper'] = preg_replace(array('/ {2,}/', '/<!--.*?-->|\\t|(?:\\r?\\n[ \\t]*)+/s'), array(' ', ''), sprintf(self::$templates[$field['template']]['template'], $wrapper['id'], implode(' ', $wrapper['class'])));
     $field = apply_filters('piklist_pre_render_field', $field);
     self::$field_rendering = $field;
     self::$fields_rendered[$field['scope']][$field['field']] = $field;
     $field_to_render = self::template_tag_fetch('field_wrapper', $field['wrapper']);
     $rendered_field = do_shortcode($field_to_render);
     switch ($field['position']) {
         case 'start':
             $rendered_field = piklist_form::template_tag_fetch('field_wrapper', $field['wrapper'], 'start') . $rendered_field;
             break;
         case 'end':
             $rendered_field .= piklist_form::template_tag_fetch('field_wrapper', $field['wrapper'], 'end');
             break;
         case 'wrap':
             $rendered_field = piklist_form::template_tag_fetch('field_wrapper', $field['wrapper'], 'start') . $rendered_field . piklist_form::template_tag_fetch('field_wrapper', $field['wrapper'], 'end');
             break;
     }
     $rendered_field = apply_filters('piklist_post_render_field', $rendered_field, $field);
     self::$field_rendering = null;
     // Return the field as requested
     if ($return) {
         return $rendered_field;
     } else {
         echo $rendered_field;
     }
 }
Beispiel #5
0
     $column['field'] = (isset($field) ? '' : $field . ':' . ($index ? $index : '0') . ':') . $column['field'];
     $column['scope'] = $scope;
 }
 if (piklist_validate::errors()) {
     $_values = piklist_validate::get_request_value($column['field'], $column['scope']);
 }
 if (!$_values) {
     if (isset($column['save_as']) && is_array($value) && isset($value[$column['save_as']])) {
         $_values = $value[$column['save_as']];
     } elseif (is_array($value) && isset($value[$field_name])) {
         $_values = $value[$field_name];
     }
 }
 if (!$_values) {
     if (piklist_form::is_widget()) {
         $_values = isset(piklist_widget::widget()->instance[$column['field']]) ? maybe_unserialize(piklist_widget::widget()->instance[$column['field']]) : null;
     } else {
         $_values = piklist_form::get_field_value($column['scope'], $column, $column['scope'], piklist_form::get_field_object_id($column));
     }
 }
 if (!$_values && $value && is_array($value) && is_numeric(key($value))) {
     $_values = $value;
 }
 if (!is_array($_values)) {
     $_values = array($_values);
 }
 if (isset($column['multiple']) && $column['multiple']) {
     foreach ($_values as $_index => $_value) {
         $column['index'] = $_index;
         $column['value'] = $_value;
         $column['group_field'] = true;