/**
 * Filter input and return sanitized SQL LIKE output
 *
 * @param mixed $input The string, array, or object to sanitize
 *
 * @return array|mixed|object|string|void
 *
 * @since 2.3.9
 *
 * @see like_escape
 */
function pods_sanitize_like($input)
{
    if ('' === $input || is_int($input) || is_float($input) || empty($input)) {
        return $input;
    }
    $output = array();
    if (is_object($input)) {
        $input = get_object_vars($input);
        foreach ($input as $key => $val) {
            $output[$key] = pods_sanitize_like($val);
        }
        $output = (object) $output;
    } elseif (is_array($input)) {
        foreach ($input as $key => $val) {
            $output[$key] = pods_sanitize_like($val);
        }
    } else {
        global $wpdb;
        if (pods_version_check('wp', '4.0')) {
            $output = $wpdb->esc_like(pods_sanitize($input));
        } else {
            // like_escape is deprecated in WordPress 4.0
            $output = like_escape(pods_sanitize($input));
        }
    }
    return $output;
}
Esempio n. 2
0
/**
 * Filter input and return sanitized SQL LIKE output
 *
 * @param mixed $input The string, array, or object to sanitize
 *
 * @return array|mixed|object|string|void
 *
 * @since 2.3.9
 *
 * @see like_escape
 */
function pods_sanitize_like($input)
{
    $output = array();
    if (is_object($input)) {
        $input = get_object_vars($input);
        foreach ($input as $key => $val) {
            $output[$key] = pods_sanitize_like($val);
        }
        $output = (object) $output;
    } elseif (is_array($input)) {
        foreach ($input as $key => $val) {
            $output[$key] = pods_sanitize_like($val);
        }
    } else {
        $output = like_escape(pods_sanitize($input));
    }
    return $output;
}
Esempio n. 3
0
 /**
  * Rename a WP object's type
  *
  * @param string $object_type Object type: post|taxonomy|comment|setting
  * @param string $old_name The old name
  * @param string $new_name The new name
  *
  * @return bool
  *
  * @since 2.0
  */
 public function rename_wp_object_type($object_type, $old_name, $new_name)
 {
     /**
      * @var $wpdb wpdb
      */
     global $wpdb;
     if ('post_type' == $object_type) {
         $object_type = 'post';
     }
     if ('post' == $object_type) {
         pods_query("UPDATE `{$wpdb->posts}` SET `post_type` = %s WHERE `post_type` = %s", array($new_name, $old_name));
     } elseif ('taxonomy' == $object_type) {
         pods_query("UPDATE `{$wpdb->term_taxonomy}` SET `taxonomy` = %s WHERE `taxonomy` = %s", array($new_name, $old_name));
     } elseif ('comment' == $object_type) {
         pods_query("UPDATE `{$wpdb->comments}` SET `comment_type` = %s WHERE `comment_type` = %s", array($new_name, $old_name));
     } elseif ('settings' == $object_type) {
         pods_query("UPDATE `{$wpdb->options}` SET `option_name` = REPLACE( `option_name`, %s, %s ) WHERE `option_name` LIKE '" . pods_sanitize_like($old_name) . "_%'", array($new_name . '_', $old_name . '_'));
     }
     return true;
 }
 /**
  * Get data from relationship objects
  *
  * @param array $object_params Object data parameters
  *
  * @return array|bool Object data
  */
 public function get_object_data($object_params = null)
 {
     global $wpdb, $polylang, $sitepress, $icl_adjust_id_url_filter_off;
     $current_language = false;
     // WPML support
     if (is_object($sitepress) && !$icl_adjust_id_url_filter_off) {
         $current_language = pods_sanitize(ICL_LANGUAGE_CODE);
     } elseif (function_exists('pll_current_language')) {
         $current_language = pll_current_language('slug');
     }
     $object_params = array_merge(array('name' => '', 'value' => '', 'options' => array(), 'pod' => '', 'id' => '', 'context' => '', 'data_params' => array('query' => ''), 'page' => 1, 'limit' => 0), $object_params);
     $name = $object_params['name'];
     $value = $object_params['value'];
     $options = $object_params['options'] = (array) $object_params['options'];
     $pod = $object_params['pod'];
     $id = $object_params['id'];
     $context = $object_params['context'];
     $data_params = $object_params['data_params'] = (array) $object_params['data_params'];
     $page = min(1, (int) $object_params['page']);
     $limit = (int) $object_params['limit'];
     if (isset($options['options'])) {
         $options = array_merge($options, $options['options']);
         unset($options['options']);
     }
     $data = apply_filters('pods_field_pick_object_data', null, $name, $value, $options, $pod, $id, $object_params);
     $items = array();
     if (!isset($options[self::$type . '_object'])) {
         $data = pods_var_raw('data', $options, array(), null, true);
     }
     $simple = false;
     if (null === $data) {
         $data = array();
         if ('custom-simple' == $options[self::$type . '_object']) {
             $custom = pods_var_raw(self::$type . '_custom', $options, '');
             $custom = apply_filters('pods_form_ui_field_pick_custom_values', $custom, $name, $value, $options, $pod, $id, $object_params);
             if (!empty($custom)) {
                 if (!is_array($custom)) {
                     $data = array();
                     $custom = explode("\n", trim($custom));
                     foreach ($custom as $custom_value) {
                         $custom_label = explode('|', $custom_value);
                         if (empty($custom_label)) {
                             continue;
                         }
                         if (1 == count($custom_label)) {
                             $custom_label = $custom_value;
                         } else {
                             $custom_value = $custom_label[0];
                             $custom_label = $custom_label[1];
                         }
                         $custom_value = trim((string) $custom_value);
                         $custom_label = trim((string) $custom_label);
                         $data[$custom_value] = $custom_label;
                     }
                 } else {
                     $data = $custom;
                 }
                 $simple = true;
             }
         } elseif (isset(self::$related_objects[$options[self::$type . '_object']]) && isset(self::$related_objects[$options[self::$type . '_object']]['data']) && !empty(self::$related_objects[$options[self::$type . '_object']]['data'])) {
             $data = self::$related_objects[$options[self::$type . '_object']]['data'];
             $simple = true;
         } elseif (isset(self::$related_objects[$options[self::$type . '_object']]) && isset(self::$related_objects[$options[self::$type . '_object']]['data_callback']) && is_callable(self::$related_objects[$options[self::$type . '_object']]['data_callback'])) {
             $data = call_user_func_array(self::$related_objects[$options[self::$type . '_object']]['data_callback'], array($name, $value, $options, $pod, $id));
             $simple = true;
             // Cache data from callback
             if (!empty($data)) {
                 self::$related_objects[$options[self::$type . '_object']]['data'] = $data;
             }
         } elseif ('simple_value' != $context) {
             $pick_val = pods_var(self::$type . '_val', $options);
             if ('table' == pods_var(self::$type . '_object', $options)) {
                 $pick_val = pods_var(self::$type . '_table', $options, $pick_val, null, true);
             }
             if ('__current__' == $pick_val) {
                 if (is_object($pod)) {
                     $pick_val = $pod->pod;
                 } elseif (is_array($pod)) {
                     $pick_val = $pod['name'];
                 } elseif (0 < strlen($pod)) {
                     $pick_val = $pod;
                 }
             }
             $options['table_info'] = pods_api()->get_table_info(pods_var(self::$type . '_object', $options), $pick_val, null, null, $options);
             $search_data = pods_data();
             $search_data->table($options['table_info']);
             if (isset($options['table_info']['pod']) && !empty($options['table_info']['pod']) && isset($options['table_info']['pod']['name'])) {
                 $search_data->pod = $options['table_info']['pod']['name'];
                 $search_data->fields = $options['table_info']['pod']['fields'];
             }
             $params = array('select' => "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`", 'table' => $search_data->table, 'where' => pods_var_raw(self::$type . '_where', $options, (array) $options['table_info']['where_default'], null, true), 'orderby' => pods_var_raw(self::$type . '_orderby', $options, null, null, true), 'groupby' => pods_var_raw(self::$type . '_groupby', $options, null, null, true), 'pagination' => false, 'search' => false);
             if (in_array($options[self::$type . '_object'], array('site', 'network'))) {
                 $params['select'] .= ', `t`.`path`';
             }
             if (!empty($params['where']) && (array) $options['table_info']['where_default'] != $params['where']) {
                 $params['where'] = pods_evaluate_tags($params['where'], true);
             }
             if (empty($params['where']) || !is_array($params['where']) && strlen(trim($params['where'])) < 1) {
                 $params['where'] = array();
             } elseif (!is_array($params['where'])) {
                 $params['where'] = (array) $params['where'];
             }
             if ('value_to_label' == $context) {
                 $params['where'][] = "`t`.`{$search_data->field_id}` = " . number_format($value, 0, '', '');
             }
             /* not needed yet
                             if ( !empty( $params[ 'orderby' ] ) )
                                 $params[ 'orderby' ] = pods_evaluate_tags( $params[ 'orderby' ], true );
             
                             if ( !empty( $params[ 'groupby' ] ) )
                                 $params[ 'groupby' ] = pods_evaluate_tags( $params[ 'groupby' ], true );*/
             $display = trim(pods_var(self::$type . '_display', $options), ' {@}');
             if (0 < strlen($display)) {
                 if (isset($options['table_info']['pod']) && !empty($options['table_info']['pod'])) {
                     if (isset($options['table_info']['pod']['object_fields']) && isset($options['table_info']['pod']['object_fields'][$display])) {
                         $search_data->field_index = $display;
                         $params['select'] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
                     } elseif (isset($options['table_info']['pod']['fields'][$display])) {
                         $search_data->field_index = $display;
                         if ('table' == $options['table_info']['pod']['storage'] && !in_array($options['table_info']['pod']['type'], array('pod', 'table'))) {
                             $params['select'] = "`t`.`{$search_data->field_id}`, `d`.`{$search_data->field_index}`";
                         } elseif ('meta' == $options['table_info']['pod']['storage']) {
                             $params['select'] = "`t`.`{$search_data->field_id}`, `{$search_data->field_index}`.`meta_value` AS {$search_data->field_index}";
                         } else {
                             $params['select'] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
                         }
                     }
                 } elseif (isset($options['table_info']['object_fields']) && isset($options['table_info']['object_fields'][$display])) {
                     $search_data->field_index = $display;
                     $params['select'] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
                 }
             }
             $autocomplete = false;
             if ('single' == pods_var(self::$type . '_format_type', $options, 'single') && 'autocomplete' == pods_var(self::$type . '_format_single', $options, 'dropdown')) {
                 $autocomplete = true;
             } elseif ('multi' == pods_var(self::$type . '_format_type', $options, 'single') && 'autocomplete' == pods_var(self::$type . '_format_multi', $options, 'checkbox')) {
                 $autocomplete = true;
             }
             $hierarchy = false;
             if ('data' == $context && !$autocomplete) {
                 if ('single' == pods_var(self::$type . '_format_type', $options, 'single') && in_array(pods_var(self::$type . '_format_single', $options, 'dropdown'), array('dropdown', 'radio'))) {
                     $hierarchy = true;
                 } elseif ('multi' == pods_var(self::$type . '_format_type', $options, 'single') && in_array(pods_var(self::$type . '_format_multi', $options, 'checkbox'), array('multiselect', 'checkbox'))) {
                     $hierarchy = true;
                 }
             }
             if ($hierarchy && $options['table_info']['object_hierarchical'] && !empty($options['table_info']['field_parent'])) {
                 $params['select'] .= ', ' . $options['table_info']['field_parent_select'];
             }
             if ($autocomplete) {
                 if (0 == $limit) {
                     $limit = 30;
                 }
                 $params['limit'] = apply_filters('pods_form_ui_field_pick_autocomplete_limit', $limit, $name, $value, $options, $pod, $id, $object_params);
                 if (is_array($value) && $params['limit'] < count($value)) {
                     $params['limit'] = count($value);
                 }
                 $params['page'] = $page;
                 if ('admin_ajax_relationship' == $context) {
                     $lookup_where = array($search_data->field_index => "`t`.`{$search_data->field_index}` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'");
                     // @todo Hook into WPML for each table
                     if ($wpdb->users == $search_data->table) {
                         $lookup_where['display_name'] = "`t`.`display_name` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                         $lookup_where['user_login'] = "******" . pods_sanitize_like($data_params['query']) . "%'";
                         $lookup_where['user_email'] = "`t`.`user_email` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                     } elseif ($wpdb->posts == $search_data->table) {
                         $lookup_where['post_title'] = "`t`.`post_title` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                         $lookup_where['post_name'] = "`t`.`post_name` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                         $lookup_where['post_content'] = "`t`.`post_content` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                         $lookup_where['post_excerpt'] = "`t`.`post_excerpt` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                     } elseif ($wpdb->terms == $search_data->table) {
                         $lookup_where['name'] = "`t`.`name` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                         $lookup_where['slug'] = "`t`.`slug` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                     } elseif ($wpdb->comments == $search_data->table) {
                         $lookup_where['comment_content'] = "`t`.`comment_content` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                         $lookup_where['comment_author'] = "`t`.`comment_author` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                         $lookup_where['comment_author_email'] = "`t`.`comment_author_email` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                     }
                     $lookup_where = apply_filters('pods_form_ui_field_pick_autocomplete_lookup', $lookup_where, $data_params['query'], $name, $value, $options, $pod, $id, $object_params, $search_data);
                     if (!empty($lookup_where)) {
                         $params['where'][] = implode(' OR ', $lookup_where);
                     }
                     $orderby = array();
                     $orderby[] = "(`t`.`{$search_data->field_index}` LIKE '%" . pods_sanitize_like($data_params['query']) . "%' ) DESC";
                     $pick_orderby = pods_var_raw(self::$type . '_orderby', $options, null, null, true);
                     if (0 < strlen($pick_orderby)) {
                         $orderby[] = $pick_orderby;
                     }
                     $orderby[] = "`t`.`{$search_data->field_index}`";
                     $orderby[] = "`t`.`{$search_data->field_id}`";
                     $params['orderby'] = $orderby;
                 }
             } elseif (0 < $limit) {
                 $params['limit'] = $limit;
                 $params['page'] = $page;
             }
             $extra = '';
             if ($wpdb->posts == $search_data->table) {
                 $extra = ', `t`.`post_type`';
             } elseif ($wpdb->terms == $search_data->table) {
                 $extra = ', `tt`.`taxonomy`';
             } elseif ($wpdb->comments == $search_data->table) {
                 $extra = ', `t`.`comment_type`';
             }
             $params['select'] .= $extra;
             if ('user' == pods_var(self::$type . '_object', $options)) {
                 $roles = pods_var(self::$type . '_user_role', $options);
                 if (!empty($roles)) {
                     $where = array();
                     foreach ((array) $roles as $role) {
                         if (empty($role) || pods_clean_name($role) != $role && sanitize_title($role) != $role) {
                             continue;
                         }
                         $where[] = $wpdb->base_prefix . (is_multisite() && !is_main_site() ? get_current_blog_id() . '_' : '') . 'capabilities.meta_value LIKE "%\\"' . pods_sanitize_like($role) . '\\"%"';
                     }
                     if (!empty($where)) {
                         $params['where'][] = implode(' OR ', $where);
                     }
                 }
             }
             $results = $search_data->select($params);
             if ($autocomplete && $params['limit'] < $search_data->total_found()) {
                 if (!empty($value)) {
                     $ids = $value;
                     if (is_array($ids) && isset($ids[0]) && is_array($ids[0])) {
                         $ids = wp_list_pluck($ids, $search_data->field_id);
                     }
                     if (is_array($ids)) {
                         $ids = implode(', ', $ids);
                     }
                     if (is_array($params['where'])) {
                         $params['where'] = implode(' AND ', $params['where']);
                     }
                     if (!empty($params['where'])) {
                         $params['where'] .= ' AND ';
                     }
                     $params['where'] .= "`t`.`{$search_data->field_id}` IN ( " . $ids . " )";
                     $results = $search_data->select($params);
                 }
             } else {
                 $autocomplete = false;
             }
             if ('data' == $context) {
                 self::$field_data = array('field' => $name, 'id' => $options['id'], 'autocomplete' => $autocomplete);
             }
             if ($hierarchy && !$autocomplete && !empty($results) && $options['table_info']['object_hierarchical'] && !empty($options['table_info']['field_parent'])) {
                 $args = array('id' => $options['table_info']['field_id'], 'index' => $options['table_info']['field_index'], 'parent' => $options['table_info']['field_parent']);
                 $results = pods_hierarchical_select($results, $args);
             }
             $ids = array();
             if (!empty($results)) {
                 $display_filter = pods_var('display_filter', pods_var_raw('options', pods_var_raw($search_data->field_index, $search_data->pod_data['object_fields'])));
                 foreach ($results as $result) {
                     $result = get_object_vars($result);
                     if (!isset($result[$search_data->field_id]) || !isset($result[$search_data->field_index])) {
                         continue;
                     }
                     $result[$search_data->field_index] = trim($result[$search_data->field_index]);
                     $object = $object_type = '';
                     if ($wpdb->posts == $search_data->table && isset($result['post_type'])) {
                         $object = $result['post_type'];
                         $object_type = 'post_type';
                     } elseif ($wpdb->terms == $search_data->table && isset($result['taxonomy'])) {
                         $object = $result['taxonomy'];
                         $object_type = 'taxonomy';
                     }
                     // WPML integration for Post Types and Taxonomies
                     if (is_object($sitepress) && in_array($object_type, array('post_type', 'taxonomy'))) {
                         $translated = false;
                         if ('post_type' == $object_type && $sitepress->is_translated_post_type($object)) {
                             $translated = true;
                         } elseif ('taxonomy' == $object_type && $sitepress->is_translated_taxonomy($object)) {
                             $translated = true;
                         }
                         if ($translated) {
                             $object_id = icl_object_id($result[$search_data->field_id], $object, false, $current_language);
                             if (0 < $object_id && !in_array($object_id, $ids)) {
                                 $text = $result[$search_data->field_index];
                                 if ($result[$search_data->field_id] != $object_id) {
                                     if ($wpdb->posts == $search_data->table) {
                                         $text = trim(get_the_title($object_id));
                                     } elseif ($wpdb->terms == $search_data->table) {
                                         $text = trim(get_term($object_id, $object)->name);
                                     }
                                 }
                                 $result[$search_data->field_id] = $object_id;
                                 $result[$search_data->field_index] = $text;
                             } else {
                                 continue;
                             }
                         }
                     } elseif (is_object($polylang) && in_array($object_type, array('post_type', 'taxonomy')) && method_exists($polylang, 'get_translation')) {
                         $translated = false;
                         if ('post_type' == $object_type && pll_is_translated_post_type($object)) {
                             $translated = true;
                         } elseif ('taxonomy' == $object_type && pll_is_translated_taxonomy($object)) {
                             $translated = true;
                         }
                         if ($translated) {
                             $object_id = $polylang->get_translation($object, $result[$search_data->field_id], $current_language);
                             if (0 < $object_id && !in_array($object_id, $ids)) {
                                 $text = $result[$search_data->field_index];
                                 if ($result[$search_data->field_id] != $object_id) {
                                     if ($wpdb->posts == $search_data->table) {
                                         $text = trim(get_the_title($object_id));
                                     } elseif ($wpdb->terms == $search_data->table) {
                                         $text = trim(get_term($object_id, $object)->name);
                                     }
                                 }
                                 $result[$search_data->field_id] = $object_id;
                                 $result[$search_data->field_index] = $text;
                             } else {
                                 continue;
                             }
                         }
                     }
                     if (0 < strlen($display_filter)) {
                         $display_filter_args = pods_var('display_filter_args', pods_var_raw('options', pods_var_raw($search_data->field_index, $search_data->pod_data['object_fields'])));
                         $args = array($display_filter, $result[$search_data->field_index]);
                         if (!empty($display_filter_args)) {
                             foreach ((array) $display_filter_args as $display_filter_arg) {
                                 if (isset($result[$display_filter_arg])) {
                                     $args[] = $result[$display_filter_arg];
                                 }
                             }
                         }
                         $result[$search_data->field_index] = call_user_func_array('apply_filters', $args);
                     }
                     if (in_array($options[self::$type . '_object'], array('site', 'network'))) {
                         $result[$search_data->field_index] = $result[$search_data->field_index] . $result['path'];
                     } elseif (strlen($result[$search_data->field_index]) < 1) {
                         $result[$search_data->field_index] = '(No Title)';
                     }
                     if ('admin_ajax_relationship' == $context) {
                         $items[] = array('id' => $result[$search_data->field_id], 'text' => $result[$search_data->field_index], 'image' => '');
                     } else {
                         $data[$result[$search_data->field_id]] = $result[$search_data->field_index];
                     }
                     $ids[] = $result[$search_data->field_id];
                 }
             }
         }
         if ($simple && 'admin_ajax_relationship' == $context) {
             $found_data = array();
             foreach ($data as $k => $v) {
                 if (false !== stripos($v, $data_params['query']) || false !== stripos($k, $data_params['query'])) {
                     $found_data[$k] = $v;
                 }
             }
             $data = $found_data;
         }
     }
     if ('admin_ajax_relationship' == $context) {
         if (empty($items) && !empty($data)) {
             foreach ($data as $k => $v) {
                 $items[] = array('id' => $k, 'text' => $v, 'image' => '');
             }
         }
         return $items;
     }
     return $data;
 }
Esempio n. 5
0
 /**
  * @static
  *
  * Clear a cached value
  *
  * @param string|bool $key        Key for the cache
  * @param string      $cache_mode (optional) Decides the caching method to use for the view.
  * @param string      $group      (optional) Set the group.
  *
  * @return bool
  *
  * @since 2.0
  */
 public static function clear($key = true, $cache_mode = null, $group = '')
 {
     $object_cache = false;
     if (isset($GLOBALS['wp_object_cache']) && is_object($GLOBALS['wp_object_cache'])) {
         $object_cache = true;
     }
     global $wpdb;
     if (!in_array($cache_mode, self::$cache_modes)) {
         $cache_mode = 'cache';
     }
     $group_key = 'pods_';
     if (!empty($group)) {
         $group_key = $group . '_';
     }
     $full_key = $original_key = $key;
     if (true !== $key) {
         // Get proper cache key
         $key = self::get_key($key, $group_key);
         $full_key = $group_key . $key;
     }
     if (apply_filters('pods_view_cache_alt_set', false, $cache_mode, $full_key, $original_key, '', 0, $group)) {
         return true;
     } elseif ('transient' == $cache_mode) {
         if (true === $key) {
             $group_key = pods_sanitize_like($group_key);
             $wpdb->query("DELETE FROM `{$wpdb->options}` WHERE option_name LIKE '_transient_{$group_key}%'");
             if ($object_cache) {
                 wp_cache_flush();
             }
         } else {
             delete_transient($group_key . $key);
         }
     } elseif ('site-transient' == $cache_mode) {
         if (true === $key) {
             $group_key = pods_sanitize_like($group_key);
             $wpdb->query("DELETE FROM `{$wpdb->options}` WHERE option_name LIKE '_site_transient_{$group_key}%'");
             if ($object_cache) {
                 wp_cache_flush();
             }
         } else {
             delete_site_transient($group_key . $key);
         }
     } elseif ('cache' == $cache_mode && $object_cache) {
         if (true === $key) {
             wp_cache_flush();
         } else {
             wp_cache_delete(empty($key) ? 'pods_view' : $key, empty($group) ? 'pods_view' : $group);
         }
     } elseif ('option-cache' == $cache_mode) {
         global $_wp_using_ext_object_cache;
         do_action('delete_transient_' . $key, $key);
         if ($_wp_using_ext_object_cache) {
             $result = wp_cache_delete($key, empty($group) ? 'pods_option_cache' : $group);
             wp_cache_delete('_timeout_' . $key, empty($group) ? 'pods_option_cache' : $group);
         } else {
             $option_timeout = '_pods_option_timeout_' . $key;
             $option = '_pods_option_' . $key;
             $result = delete_option($option);
             if ($result) {
                 delete_option($option_timeout);
             }
         }
         if ($result) {
             do_action('deleted_transient', $key);
         }
     }
     do_action('pods_view_clear_' . $cache_mode, $original_key, $group);
     return true;
 }
Esempio n. 6
0
 /**
  * Recursively join tables based on fields
  *
  * @param array $traverse_recurse Array of traversal options
  *
  * @return array Array of table joins
  *
  * @since 2.0
  */
 function traverse_recurse($traverse_recurse)
 {
     global $wpdb;
     $defaults = array('pod' => null, 'fields' => array(), 'joined' => 't', 'depth' => 0, 'joined_id' => 'id', 'joined_index' => 'id', 'params' => new stdClass(), 'last_table_info' => array());
     $traverse_recurse = array_merge($defaults, $traverse_recurse);
     $joins = array();
     if (0 == $traverse_recurse['depth'] && !empty($traverse_recurse['pod']) && !empty($traverse_recurse['last_table_info']) && isset($traverse_recurse['last_table_info']['id'])) {
         $pod_data = $traverse_recurse['last_table_info'];
     } elseif (empty($traverse_recurse['pod'])) {
         if (!empty($traverse_recurse['params']) && !empty($traverse_recurse['params']->table) && 0 === strpos($traverse_recurse['params']->table, $wpdb->prefix)) {
             if ($wpdb->posts == $traverse_recurse['params']->table) {
                 $traverse_recurse['pod'] = 'post_type';
             } elseif ($wpdb->terms == $traverse_recurse['params']->table) {
                 $traverse_recurse['pod'] = 'taxonomy';
             } elseif ($wpdb->users == $traverse_recurse['params']->table) {
                 $traverse_recurse['pod'] = 'user';
             } elseif ($wpdb->comments == $traverse_recurse['params']->table) {
                 $traverse_recurse['pod'] = 'comment';
             } else {
                 return $joins;
             }
             $pod_data = array();
             if (in_array($traverse_recurse['pod'], array('user', 'comment'))) {
                 $pod = $this->api->load_pod(array('name' => $traverse_recurse['pod'], 'table_info' => true));
                 if (!empty($pod) && $pod['type'] == $pod) {
                     $pod_data = $pod;
                 }
             }
             if (empty($pod_data)) {
                 $pod_data = array('id' => 0, 'name' => '_table_' . $traverse_recurse['pod'], 'type' => $traverse_recurse['pod'], 'storage' => 'taxonomy' == $traverse_recurse['pod'] ? 'none' : 'meta', 'fields' => array(), 'object_fields' => $this->api->get_wp_object_fields($traverse_recurse['pod']));
                 $pod_data = array_merge($this->api->get_table_info($traverse_recurse['pod'], ''), $pod_data);
             }
             $traverse_recurse['pod'] = $pod_data['name'];
         } else {
             return $joins;
         }
     } else {
         $pod_data = $this->api->load_pod(array('name' => $traverse_recurse['pod'], 'table_info' => true), false);
         if (empty($pod_data)) {
             return $joins;
         }
     }
     if (isset($pod_data['object_fields'])) {
         $pod_data['fields'] = array_merge($pod_data['fields'], $pod_data['object_fields']);
     }
     $tableless_field_types = PodsForm::tableless_field_types();
     $simple_tableless_objects = PodsForm::field_method('pick', 'simple_objects');
     $file_field_types = PodsForm::file_field_types();
     if (!isset($this->traversal[$traverse_recurse['pod']])) {
         $this->traversal[$traverse_recurse['pod']] = array();
     }
     if ((empty($pod_data['meta_table']) || $pod_data['meta_table'] == $pod_data['table']) && (empty($traverse_recurse['fields']) || !isset($traverse_recurse['fields'][$traverse_recurse['depth']]) || empty($traverse_recurse['fields'][$traverse_recurse['depth']]))) {
         return $joins;
     }
     $field = $traverse_recurse['fields'][$traverse_recurse['depth']];
     $ignore_aliases = array('wpml_languages', 'polylang_languages');
     $ignore_aliases = $this->do_hook('traverse_recurse_ignore_aliases', $ignore_aliases, $field, $traverse_recurse);
     if (in_array($field, $ignore_aliases)) {
         return $joins;
     }
     $meta_data_table = false;
     if (!isset($pod_data['fields'][$field]) && 'd' == $field && isset($traverse_recurse['fields'][$traverse_recurse['depth'] - 1])) {
         $field = $traverse_recurse['fields'][$traverse_recurse['depth'] - 1];
         $field_type = 'pick';
         if (isset($traverse_recurse['last_table_info']['pod']['fields'][$field])) {
             $field_type = $traverse_recurse['last_table_info']['pod']['fields'][$field]['type'];
         } elseif (isset($traverse_recurse['last_table_info']['pod']['object_fields'][$field])) {
             $field_type = $traverse_recurse['last_table_info']['pod']['object_fields'][$field]['type'];
         }
         $pod_data['fields'][$field] = array('id' => 0, 'name' => $field, 'type' => $field_type, 'pick_object' => $traverse_recurse['last_table_info']['pod']['type'], 'pick_val' => $traverse_recurse['last_table_info']['pod']['name']);
         $meta_data_table = true;
     }
     // Fallback to meta table if the pod type supports it
     if (!isset($pod_data['fields'][$field])) {
         $last = end($traverse_recurse['fields']);
         if ('post_type' == $pod_data['type'] && !isset($pod_data['object_fields'])) {
             $pod_data['object_fields'] = $this->api->get_wp_object_fields('post_type', $pod_data);
         }
         if ('post_type' == $pod_data['type'] && isset($pod_data['object_fields'][$field]) && in_array($pod_data['object_fields'][$field]['type'], $tableless_field_types)) {
             $pod_data['fields'][$field] = $pod_data['object_fields'][$field];
         } elseif (in_array($pod_data['type'], array('post_type', 'media', 'user', 'comment')) && 'meta_value' == $last) {
             $pod_data['fields'][$field] = PodsForm::field_setup(array('name' => $field));
         } else {
             if ('post_type' == $pod_data['type']) {
                 $pod_data['object_fields'] = $this->api->get_wp_object_fields('post_type', $pod_data, true);
                 if ('post_type' == $pod_data['type'] && isset($pod_data['object_fields'][$field]) && in_array($pod_data['object_fields'][$field]['type'], $tableless_field_types)) {
                     $pod_data['fields'][$field] = $pod_data['object_fields'][$field];
                 } else {
                     return $joins;
                 }
             } else {
                 return $joins;
             }
         }
     }
     $traverse = $pod_data['fields'][$field];
     if ('taxonomy' == $traverse['type']) {
         $traverse['table_info'] = $this->api->get_table_info($traverse['type'], $traverse['name']);
     } elseif (in_array($traverse['type'], $file_field_types)) {
         $traverse['table_info'] = $this->api->get_table_info('post_type', 'attachment');
     } elseif (!in_array($traverse['type'], $tableless_field_types)) {
         $traverse['table_info'] = $this->api->get_table_info($pod_data['type'], $pod_data['name'], $pod_data['name'], $pod_data);
     } elseif (empty($traverse['table_info']) || in_array($traverse['pick_object'], $simple_tableless_objects) && !empty($traverse_recurse['last_table_info'])) {
         if (in_array($traverse['pick_object'], $simple_tableless_objects) && !empty($traverse_recurse['last_table_info'])) {
             $traverse['table_info'] = $traverse_recurse['last_table_info'];
             if (!empty($traverse['table_info']['meta_table'])) {
                 $meta_data_table = true;
             }
         } elseif (!in_array($traverse['type'], $tableless_field_types) && isset($traverse_recurse['last_table_info']) && !empty($traverse_recurse['last_table_info']) && 0 == $traverse_recurse['depth']) {
             $traverse['table_info'] = $traverse_recurse['last_table_info'];
         } else {
             $traverse['table_info'] = $this->api->get_table_info($traverse['pick_object'], $traverse['pick_val'], null, $traverse['pod'], $traverse);
         }
     }
     if (isset($this->traversal[$traverse_recurse['pod']][$traverse['name']])) {
         $traverse = array_merge($traverse, (array) $this->traversal[$traverse_recurse['pod']][$traverse['name']]);
     }
     $traverse = $this->do_hook('traverse', $traverse, compact('pod', 'fields', 'joined', 'depth', 'joined_id', 'params'));
     if (empty($traverse)) {
         return $joins;
     }
     $traverse = pods_sanitize($traverse);
     $traverse['id'] = (int) $traverse['id'];
     if (empty($traverse['id'])) {
         $traverse['id'] = $field;
     }
     $table_info = $traverse['table_info'];
     $this->traversal[$traverse_recurse['pod']][$field] = $traverse;
     $field_joined = $field;
     if (0 < $traverse_recurse['depth'] && 't' != $traverse_recurse['joined']) {
         if ($meta_data_table && ('pick' != $traverse['type'] || !in_array(pods_var('pick_object', $traverse), $simple_tableless_objects))) {
             $field_joined = $traverse_recurse['joined'] . '_d';
         } else {
             $field_joined = $traverse_recurse['joined'] . '_' . $field;
         }
     }
     $rel_alias = 'rel_' . $field_joined;
     if (pods_var('search', $traverse_recurse['params'], false) && empty($traverse_recurse['params']->filters)) {
         if (0 < strlen(pods_var('filter_' . $field_joined, 'get'))) {
             $val = absint(pods_var('filter_' . $field_joined, 'get'));
             $search = "`{$field_joined}`.`{$table_info['field_id']}` = {$val}";
             if ('text' == $this->search_mode) {
                 $val = pods_var('filter_' . $field_joined, 'get');
                 $search = "`{$field_joined}`.`{$traverse['name']}` = '{$val}'";
             } elseif ('text_like' == $this->search_mode) {
                 $val = pods_sanitize(pods_sanitize_like(pods_var_raw('filter_' . $field_joined)));
                 $search = "`{$field_joined}`.`{$traverse['name']}` LIKE '%{$val}%'";
             }
             $this->search_where[] = " {$search} ";
         }
     }
     $the_join = null;
     $joined_id = $table_info['field_id'];
     $joined_index = $table_info['field_index'];
     if ('taxonomy' == $traverse['type']) {
         $rel_tt_alias = 'rel_tt_' . $field_joined;
         if ($meta_data_table) {
             $the_join = "\n                    LEFT JOIN `{$table_info['pod_table']}` AS `{$field_joined}` ON\n                        `{$field_joined}`.`{$table_info['pod_field_id']}` = `{$traverse_recurse['rel_alias']}`.`{$traverse_recurse['joined_id']}`\n                ";
         } else {
             $the_join = "\n                    LEFT JOIN `{$wpdb->term_relationships}` AS `{$rel_alias}` ON\n                        `{$rel_alias}`.`object_id` = `{$traverse_recurse['joined']}`.`ID`\n\n                    LEFT JOIN `{$wpdb->term_taxonomy}` AS `{$rel_tt_alias}` ON\n                        `{$rel_tt_alias}`.`taxonomy` = '{$traverse['name']}'\n                        AND `{$rel_tt_alias}`.`term_taxonomy_id` = `{$rel_alias}`.`term_taxonomy_id`\n\n                    LEFT JOIN `{$table_info['table']}` AS `{$field_joined}` ON\n                        `{$field_joined}`.`{$table_info['field_id']}` = `{$rel_tt_alias}`.`{$table_info['field_id']}`\n                ";
             // Override $rel_alias
             $rel_alias = $field_joined;
             $joined_id = $table_info['field_id'];
             $joined_index = $table_info['field_index'];
         }
     } elseif (in_array($traverse['type'], $tableless_field_types) && ('pick' != $traverse['type'] || !in_array(pods_var('pick_object', $traverse), $simple_tableless_objects))) {
         if (pods_tableless()) {
             $the_join = "\n                    LEFT JOIN `{$table_info['meta_table']}` AS `{$rel_alias}` ON\n                        `{$rel_alias}`.`{$table_info['meta_field_index']}` = '{$traverse['name']}'\n                        AND `{$rel_alias}`.`{$table_info['meta_field_id']}` = `{$traverse_recurse['joined']}`.`{$traverse_recurse['joined_id']}`\n\n                    LEFT JOIN `{$table_info['meta_table']}` AS `{$field_joined}` ON\n                        `{$field_joined}`.`{$table_info['meta_field_index']}` = '{$traverse['name']}'\n                        AND `{$field_joined}`.`{$table_info['meta_field_id']}` = CONVERT( `{$rel_alias}`.`{$table_info['meta_field_value']}`, SIGNED )\n                ";
             $joined_id = $table_info['meta_field_id'];
             $joined_index = $table_info['meta_field_index'];
         } elseif ($meta_data_table) {
             $the_join = "\n                    LEFT JOIN `{$table_info['pod_table']}` AS `{$field_joined}` ON\n                        `{$field_joined}`.`{$table_info['pod_field_id']}` = `{$traverse_recurse['rel_alias']}`.`{$traverse_recurse['joined_id']}`\n                ";
         } else {
             $the_join = "\n                    LEFT JOIN `@wp_podsrel` AS `{$rel_alias}` ON\n                        `{$rel_alias}`.`field_id` = {$traverse['id']}\n                        AND `{$rel_alias}`.`item_id` = `{$traverse_recurse['joined']}`.`{$traverse_recurse['joined_id']}`\n\n                    LEFT JOIN `{$table_info['table']}` AS `{$field_joined}` ON\n                        `{$field_joined}`.`{$table_info['field_id']}` = `{$rel_alias}`.`related_item_id`\n                ";
         }
     } elseif ('meta' == $pod_data['storage']) {
         if ($traverse_recurse['depth'] + 2 == count($traverse_recurse['fields']) && ('pick' != $traverse['type'] || !in_array(pods_var('pick_object', $traverse), $simple_tableless_objects)) && $table_info['meta_field_value'] == $traverse_recurse['fields'][$traverse_recurse['depth'] + 1]) {
             $the_join = "\n                    LEFT JOIN `{$table_info['meta_table']}` AS `{$field_joined}` ON\n                        `{$field_joined}`.`{$table_info['meta_field_index']}` = '{$traverse['name']}'\n                        AND `{$field_joined}`.`{$table_info['meta_field_id']}` = `{$traverse_recurse['joined']}`.`{$traverse_recurse['joined_id']}`\n                ";
             $table_info['recurse'] = false;
         } else {
             $the_join = "\n                    LEFT JOIN `{$table_info['meta_table']}` AS `{$field_joined}` ON\n                        `{$field_joined}`.`{$table_info['meta_field_index']}` = '{$traverse['name']}'\n                        AND `{$field_joined}`.`{$table_info['meta_field_id']}` = `{$traverse_recurse['joined']}`.`{$traverse_recurse['joined_id']}`\n                ";
             $joined_id = $table_info['meta_field_id'];
             $joined_index = $table_info['meta_field_index'];
         }
     }
     $traverse_recursive = array('pod' => pods_var_raw('name', pods_var_raw('pod', $table_info)), 'fields' => $traverse_recurse['fields'], 'joined' => $field_joined, 'depth' => $traverse_recurse['depth'] + 1, 'joined_id' => $joined_id, 'joined_index' => $joined_index, 'params' => $traverse_recurse['params'], 'rel_alias' => $rel_alias, 'last_table_info' => $table_info);
     $the_join = $this->do_hook('traverse_the_join', $the_join, $traverse_recurse, $traverse_recursive);
     if (empty($the_join)) {
         return $joins;
     }
     $joins[$traverse_recurse['pod'] . '_' . $traverse_recurse['depth'] . '_' . $traverse['id']] = $the_join;
     if ($traverse_recurse['depth'] + 1 < count($traverse_recurse['fields']) && !empty($traverse_recurse['pod']) && false !== $table_info['recurse']) {
         $joins = array_merge($joins, $this->traverse_recurse($traverse_recursive));
     }
     return $joins;
 }
 /**
  * Called for all fields related to the target taxonomy that are in a user Pod
  *
  * @param string $pod_name
  * @param string $field_name
  */
 private function update_setting_meta($pod_name, $field_name)
 {
     /** @global wpdb $wpdb */
     global $wpdb;
     $option_name = "{$pod_name}_{$field_name}";
     // Fix up the unserialized data
     $task = "update_setting_meta_{$pod_name}_{$field_name}_unserialized";
     if (!$this->have_done($task)) {
         // UPDATE {$wpdb->options} SET option_value = '{$new_term_id}' WHERE option_name = '{$pod_name}_{$field_name}' AND option_value = '{$term_id}'
         $table = $wpdb->options;
         $data = array('option_value' => $this->new_term_id);
         $where = array('option_name' => $option_name, 'option_value' => $this->term_id);
         $format = '%s';
         $where_format = array('%s', '%s');
         $wpdb->update($table, $data, $where, $format, $where_format);
         $this->append_progress($task);
     }
     // Fix up the serialized data
     $task = "update_setting_meta_{$pod_name}_{$field_name}_serialized";
     if (!$this->have_done($task)) {
         $target_serialized = sprintf(';i:%s;', $this->term_id);
         $replace_serialized = sprintf(';i:%s;', $this->new_term_id);
         $wpdb->query($wpdb->prepare("\n\t\t\t\tUPDATE\n\t\t\t\t\t{$wpdb->options}\n\t\t\t\tSET\n\t\t\t\t\toption_value = REPLACE( option_value, %s, %s )\n\t\t\t\tWHERE\n\t\t\t\t\toption_name = %s\n\t\t\t\t\tAND option_value LIKE '%%%s%%'\n\t\t\t\t", $target_serialized, $replace_serialized, $option_name, pods_sanitize_like($target_serialized)));
         $this->append_progress($task);
     }
 }