/** * Check to see if Pod Page exists and return data * * $uri not required, if NULL then returns REQUEST_URI matching Pod Page * * @param string $uri The Pod Page URI to check if exists * * @return array|bool */ public static function exists($uri = null) { if (null === $uri) { $uri = parse_url(pods_current_url()); $uri = $uri['path']; } else { $uri = explode('?', $uri); $uri = explode('#', $uri[0]); $uri = $uri[0]; } $home = parse_url(get_home_url()); if (!empty($home) && isset($home['path']) && '/' != $home['path']) { $uri = substr($uri, strlen($home['path'])); } $uri = trim($uri, '/'); $uri_depth = count(array_filter(explode('/', $uri))) - 1; $pods_page_exclusions = array('wp-admin', 'wp-content', 'wp-includes', 'index.php', 'wp-login.php', 'wp-signup.php'); $pods_page_exclusions = apply_filters('pods_page_exclusions', $pods_page_exclusions); if (is_admin() || empty($uri)) { return false; } foreach ($pods_page_exclusions as $exclusion) { if (0 === strpos($uri, $exclusion)) { return false; } } $object = false; if (false === strpos($uri, '*') && !apply_filters('pods_page_regex_matching', false)) { $object = pods_by_title($uri, ARRAY_A, '_pods_page', 'publish'); } $wildcard = false; if (empty($object)) { if (false === strpos($uri, '*')) { $object = pods_cache_get($uri, 'pods_object_page_wildcard'); if (!empty($object)) { return $object; } } $pod_page_rewrites = pods_transient_get('pods_object_page_rewrites'); if (empty($pod_page_rewrites)) { $pod_page_rewrites = self::flush_rewrites(); } else { $pod_page_rewrites = array_flip($pod_page_rewrites); } $found_rewrite_page_id = 0; if (!empty($pod_page_rewrites)) { foreach ($pod_page_rewrites as $pod_page => $pod_page_id) { if (!apply_filters('pods_page_regex_matching', false)) { if (false === strpos($pod_page, '*')) { continue; } $depth_check = strlen($pod_page) - strlen(str_replace('/', '', $pod_page)); $pod_page = preg_quote($pod_page, '/'); $pod_page = str_replace('\\*', '(.*)', $pod_page); if ($uri_depth == $depth_check && preg_match('/^' . $pod_page . '$/', $uri)) { $found_rewrite_page_id = $pod_page_id; break; } } elseif (preg_match('/^' . str_replace('/', '\\/', $pod_page) . '$/', $uri)) { $found_rewrite_page_id = $pod_page_id; break; } } if (!empty($found_rewrite_page_id)) { $object = get_post($found_rewrite_page_id, ARRAY_A); if (empty($object) || '_pods_page' != $object['post_type']) { $object = false; } } } $wildcard = true; } if (!empty($object)) { $object = array('id' => $object['ID'], 'uri' => $object['post_title'], 'code' => $object['post_content'], 'phpcode' => $object['post_content'], 'precode' => get_post_meta($object['ID'], 'precode', true), 'page_template' => get_post_meta($object['ID'], 'page_template', true), 'title' => get_post_meta($object['ID'], 'page_title', true), 'options' => array('admin_only' => (bool) get_post_meta($object['ID'], 'admin_only', true), 'restrict_role' => (bool) get_post_meta($object['ID'], 'restrict_role', true), 'restrict_capability' => (bool) get_post_meta($object['ID'], 'restrict_capability', true), 'roles_allowed' => get_post_meta($object['ID'], 'roles_allowed', true), 'capability_allowed' => get_post_meta($object['ID'], 'capability_allowed', true), 'restrict_redirect' => (bool) get_post_meta($object['ID'], 'restrict_redirect', true), 'restrict_redirect_login' => (bool) get_post_meta($object['ID'], 'restrict_redirect_login', true), 'restrict_redirect_url' => get_post_meta($object['ID'], 'restrict_redirect_url', true), 'pod' => get_post_meta($object['ID'], 'pod', true), 'pod_slug' => get_post_meta($object['ID'], 'pod_slug', true))); if ($wildcard) { pods_cache_set($uri, $object, 'pods_object_page_wildcard', 3600); } return $object; } return false; }
/** * Load the pod item object * * $params['pod'] string The datatype name * $params['id'] int (optional) The item's ID * * @param array $params An associative array of parameters * * @return bool|\Pods * * @uses pods() * * @since 2.0 */ public function load_pod_item($params) { $params = (object) pods_sanitize($params); if (!isset($params->pod) || empty($params->pod)) { return pods_error(__('Pod name required', 'pods'), $this); } if (!isset($params->id) || empty($params->id)) { return pods_error(__('Item ID required', 'pods'), $this); } $pod = false; if (pods_api_cache()) { $pod = pods_cache_get($params->id, 'pods_item_object_' . $params->pod); } if (false !== $pod) { return $pod; } $pod = pods($params->pod, $params->id); if (pods_api_cache()) { pods_cache_set($params->id, $pod, 'pods_item_object_' . $params->pod); } return $pod; }
/** * Fetch a new row for the current pod_data * * @param int $row Row number to fetch * @param bool $explicit_set Whether to set explicitly (use false when in loop) * * @return mixed * * @since 2.0 */ public function fetch($row = null, $explicit_set = true) { global $wpdb; if (null === $row) { $explicit_set = false; } $id = $row; $tableless_field_types = PodsForm::tableless_field_types(); if (null === $row) { $this->row_number++; $this->row = false; if (isset($this->data[$this->row_number])) { $this->row = get_object_vars($this->data[$this->row_number]); $current_row_id = false; if (in_array($this->pod_data['type'], array('post_type', 'media'))) { $current_row_id = pods_var_raw('ID', $this->row); } elseif ('taxonomy' == $this->pod_data['type']) { $current_row_id = pods_var_raw('term_id', $this->row); } elseif ('user' == $this->pod_data['type']) { $current_row_id = pods_var_raw('ID', $this->row); } elseif ('comment' == $this->pod_data['type']) { $current_row_id = pods_var_raw('comment_ID', $this->row); } elseif ('settings' == $this->pod_data['type']) { $current_row_id = $this->pod_data['id']; } if (0 < $current_row_id) { $row = $current_row_id; } } } if (null !== $row || 'settings' == $this->pod_data['type']) { if ($explicit_set) { $this->row_number = -1; } $mode = 'id'; $id = pods_absint($row); if (!is_numeric($row) || 0 === strpos($row, '0') || $row != preg_replace('/[^0-9]/', '', $row)) { $mode = 'slug'; $id = $row; } $row = false; // @todo Figure out why taking out this in_array() causes cached data issues in User edit screen if (!empty($this->pod) && in_array($this->pod_data['type'], array('pod', 'table'))) { $row = pods_cache_get($id, 'pods_items_' . $this->pod); } $current_row_id = false; $get_table_data = false; $old_row = $this->row; if (false !== $row && is_array($row)) { $this->row = $row; } elseif (in_array($this->pod_data['type'], array('post_type', 'media'))) { if ('post_type' == $this->pod_data['type']) { if (empty($this->pod_data['object'])) { $post_type = $this->pod_data['name']; } else { $post_type = $this->pod_data['object']; } } else { $post_type = 'attachment'; } if ('id' == $mode) { $this->row = get_post($id, ARRAY_A); if (is_array($this->row) && $this->row['post_type'] != $post_type) { $this->row = false; } } else { $args = array('post_type' => $post_type, 'name' => $id, 'numberposts' => 5); $find = get_posts($args); if (!empty($find)) { $this->row = get_object_vars($find[0]); } } if (is_wp_error($this->row) || empty($this->row)) { $this->row = false; } else { $current_row_id = $this->row['ID']; } $get_table_data = true; } elseif ('taxonomy' == $this->pod_data['type']) { $taxonomy = $this->pod_data['object']; if (empty($taxonomy)) { $taxonomy = $this->pod_data['name']; } // Taxonomies are registered during init, so they aren't available before then if (!did_action('init')) { // hackaround :( if ('id' == $mode) { $term_where = 't.term_id = %d'; } else { $term_where = 't.slug = %s'; } $filter = 'raw'; $term = $id; if ('id' != $mode || !($_term = wp_cache_get($term, $taxonomy))) { $_term = $wpdb->get_row($wpdb->prepare("SELECT t.*, tt.* FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND {$term_where} LIMIT 1", $taxonomy, $term)); if ($_term) { wp_cache_add($term, $_term, $taxonomy); } } $_term = apply_filters('get_term', $_term, $taxonomy); $_term = apply_filters("get_{$taxonomy}", $_term, $taxonomy); $_term = sanitize_term($_term, $taxonomy, $filter); if (is_object($_term)) { $this->row = get_object_vars($_term); } } elseif ('id' == $mode) { $this->row = get_term($id, $taxonomy, ARRAY_A); } else { $this->row = get_term_by('slug', $id, $taxonomy, ARRAY_A); } if (is_wp_error($this->row) || empty($this->row)) { $this->row = false; } else { $current_row_id = $this->row['term_id']; } $get_table_data = true; } elseif ('user' == $this->pod_data['type']) { if ('id' == $mode) { $this->row = get_userdata($id); } else { $this->row = get_user_by('slug', $id); } if (is_wp_error($this->row) || empty($this->row)) { $this->row = false; } else { // Get other vars $roles = $this->row->roles; $caps = $this->row->caps; $allcaps = $this->row->allcaps; $this->row = get_object_vars($this->row->data); // Set other vars $this->row['roles'] = $roles; $this->row['caps'] = $caps; $this->row['allcaps'] = $allcaps; unset($this->row['user_pass']); $current_row_id = $this->row['ID']; } $get_table_data = true; } elseif ('comment' == $this->pod_data['type']) { $this->row = get_comment($id, ARRAY_A); // No slug handling here if (is_wp_error($this->row) || empty($this->row)) { $this->row = false; } else { $current_row_id = $this->row['comment_ID']; } $get_table_data = true; } elseif ('settings' == $this->pod_data['type']) { $this->row = array(); if (empty($this->fields)) { $this->row = false; } else { foreach ($this->fields as $field) { if (!in_array($field['type'], $tableless_field_types)) { $this->row[$field['name']] = get_option($this->pod_data['name'] . '_' . $field['name'], null); } } // Force ID $this->id = $this->pod_data['id']; $this->row['option_id'] = $this->id; } } else { $params = array('table' => $this->table, 'where' => "`t`.`{$this->field_id}` = " . (int) $id, 'orderby' => "`t`.`{$this->field_id}` DESC", 'page' => 1, 'limit' => 1, 'search' => false); if ('slug' == $mode && !empty($this->field_slug)) { $id = pods_sanitize($id); $params['where'] = "`t`.`{$this->field_slug}` = '{$id}'"; } $this->row = pods_data()->select($params); if (empty($this->row)) { $this->row = false; } else { $current_row = (array) $this->row; $this->row = get_object_vars((object) @current($current_row)); } } if (!$explicit_set && is_array($this->row) && !empty($this->row) && !empty($old_row)) { $this->row = array_merge($old_row, $this->row); } if ('table' == $this->pod_data['storage'] && false !== $get_table_data && is_numeric($current_row_id)) { $params = array('table' => $wpdb->prefix . "pods_", 'where' => "`t`.`id` = {$current_row_id}", 'orderby' => "`t`.`id` DESC", 'page' => 1, 'limit' => 1, 'search' => false, 'strict' => true); if (empty($this->pod_data['object'])) { $params['table'] .= $this->pod_data['name']; } else { $params['table'] .= $this->pod_data['object']; } $row = pods_data()->select($params); if (!empty($row)) { $current_row = (array) $row; $row = get_object_vars((object) @current($current_row)); if (is_array($this->row) && !empty($this->row)) { $this->row = array_merge($row, $this->row); } else { $this->row = $row; } } } // @todo Figure out why taking out this in_array() causes cached data issues in User edit screen if (!empty($this->pod) && in_array($this->pod_data['type'], array('pod', 'table'))) { pods_cache_set($id, $this->row, 'pods_items_' . $this->pod, 0); } } $this->row = $this->do_hook('fetch', $this->row, $id, $this->row_number); return $this->row; }
/** * Take over the avatar served from WordPress * * @param string $avatar Default Avatar Image output from WordPress * @param int|string|object $id_or_email A user ID, email address, or comment object * @param int $size Size of the avatar image * @param string $default URL to a default image to use if no avatar is available * @param string $alt Alternate text to use in image tag. Defaults to blank * @return string <img> tag for the user's avatar */ public function get_avatar($avatar, $id_or_email, $size, $default = '', $alt = '') { $_user_ID = 0; if (is_numeric($id_or_email) && 0 < $id_or_email) { $_user_ID = (int) $id_or_email; } elseif (is_object($id_or_email) && isset($id_or_email->user_id) && 0 < $id_or_email->user_id) { $_user_ID = (int) $id_or_email->user_id; } elseif (is_object($id_or_email) && isset($id_or_email->ID) && isset($id_or_email->user_login) && 0 < $id_or_email->ID) { $_user_ID = (int) $id_or_email->ID; } elseif (!is_object($id_or_email) && false !== strpos($id_or_email, '@')) { $_user = get_user_by('email', $id_or_email); if (!empty($_user)) { $_user_ID = (int) $_user->ID; } } if (0 < $_user_ID && !empty(PodsMeta::$user)) { $avatar_cached = pods_cache_get($_user_ID . '-' . $size, 'pods_avatars'); if (!empty($avatar_cached)) { $avatar = $avatar_cached; } else { $avatar_field = pods_transient_get('pods_avatar_field'); $user = current(PodsMeta::$user); if (empty($avatar_field)) { foreach ($user['fields'] as $field) { if ('avatar' == $field['type']) { $avatar_field = $field['name']; pods_transient_set('pods_avatar_field', $avatar_field); break; } } } elseif (!isset($user['fields'][$avatar_field])) { $avatar_field = false; } if (!empty($avatar_field)) { $user_avatar = get_user_meta($_user_ID, $avatar_field . '.ID', true); if (!empty($user_avatar)) { $attributes = array('alt' => '', 'class' => 'avatar avatar-' . $size . ' photo'); if (!empty($alt)) { $attributes['alt'] = $alt; } $user_avatar = pods_image($user_avatar, array($size, $size), 0, $attributes); if (!empty($user_avatar)) { $avatar = $user_avatar; pods_cache_set($_user_ID . '-' . $size, $avatar, 'pods_avatars'); } } } } } return $avatar; }
/** * Fetch a new row for the current pod_data * * @param int $row Row number to fetch * @param bool $explicit_set Whether to set explicitly (use false when in loop) * * @return mixed * * @since 2.0 */ public function fetch($row = null, $explicit_set = true) { global $wpdb; if (null === $row) { $explicit_set = false; } $id = $row; $tableless_field_types = PodsForm::tableless_field_types(); if (null === $row) { $this->row_number++; $this->row = false; if (isset($this->data[$this->row_number])) { $this->row = get_object_vars($this->data[$this->row_number]); $current_row_id = false; if (in_array($this->pod_data['type'], array('post_type', 'media'))) { $current_row_id = pods_var_raw('ID', $this->row); } elseif ('taxonomy' == $this->pod_data['type']) { $current_row_id = pods_var_raw('term_id', $this->row); } elseif ('user' == $this->pod_data['type']) { $current_row_id = pods_var_raw('ID', $this->row); } elseif ('comment' == $this->pod_data['type']) { $current_row_id = pods_var_raw('comment_ID', $this->row); } elseif ('settings' == $this->pod_data['type']) { $current_row_id = $this->pod_data['id']; } if (0 < $current_row_id) { $row = $current_row_id; } } } if (null !== $row || 'settings' == $this->pod_data['type']) { if ($explicit_set) { $this->row_number = -1; } $mode = 'id'; $id = pods_absint($row); if (!is_numeric($row) || 0 === strpos($row, '0') || $row != preg_replace('/[^0-9]/', '', $row)) { $mode = 'slug'; $id = $row; } $row = false; if (!empty($this->pod)) { $row = pods_cache_get($id, 'pods_items_' . $this->pod); } $current_row_id = false; $get_table_data = false; if (false !== $row && is_array($row)) { $this->row = $row; } elseif (in_array($this->pod_data['type'], array('post_type', 'media'))) { if ('post_type' == $this->pod_data['type']) { $post_type = $this->pod_data['object']; if (empty($post_type)) { $post_type = $this->pod_data['name']; } } else { $post_type = 'attachment'; } if ('id' == $mode) { $this->row = get_post($id, ARRAY_A); if (is_array($this->row) && $this->row['post_type'] != $post_type) { $this->row = false; } } else { $args = array('post_type' => $post_type, 'name' => $id, 'numberposts' => 5); $find = get_posts($args); if (!empty($find)) { $this->row = get_object_vars($find[0]); } } if (empty($this->row)) { $this->row = false; } $current_row_id = $this->row['ID']; $get_table_data = true; } elseif ('taxonomy' == $this->pod_data['type']) { $taxonomy = $this->pod_data['object']; if (empty($taxonomy)) { $taxonomy = $this->pod_data['name']; } if ('id' == $mode) { $this->row = get_term($id, $taxonomy, ARRAY_A); } else { $this->row = get_term_by('slug', $id, $taxonomy, ARRAY_A); } if (empty($this->row)) { $this->row = false; } $current_row_id = $this->row['term_id']; $get_table_data = true; } elseif ('user' == $this->pod_data['type']) { if ('id' == $mode) { $this->row = get_userdata($id); } else { $this->row = get_user_by('slug', $id); } if (empty($this->row)) { $this->row = false; } else { $this->row = get_object_vars($this->row); } $current_row_id = $this->row['ID']; $get_table_data = true; } elseif ('comment' == $this->pod_data['type']) { $this->row = get_comment($id, ARRAY_A); // No slug handling here if (empty($this->row)) { $this->row = false; } $current_row_id = $this->row['comment_ID']; $get_table_data = true; } elseif ('settings' == $this->pod_data['type']) { $this->row = array(); if (empty($this->fields)) { $this->row = false; } else { foreach ($this->fields as $field) { if (!in_array($field['type'], $tableless_field_types)) { $this->row[$field['name']] = get_option($this->pod_data['name'] . '_' . $field['name'], null); } } // Force ID $this->id = $this->pod_data['id']; $this->row['option_id'] = $this->id; } } else { $params = array('table' => $this->table, 'where' => "`t`.`{$this->field_id}` = " . (int) $id, 'orderby' => "`t`.`{$this->field_id}` DESC", 'page' => 1, 'limit' => 1, 'search' => false); if ('slug' == $mode && !empty($this->field_slug)) { $id = esc_sql($id); $params['where'] = "`t`.`{$this->field_slug}` = '{$id}'"; } $this->row = pods_data()->select($params); if (empty($this->row)) { $this->row = false; } else { $current_row = (array) $this->row; $this->row = get_object_vars((object) @current($current_row)); } } if ('table' == $this->pod_data['storage'] && false !== $get_table_data && is_numeric($current_row_id)) { $params = array('table' => $wpdb->prefix . "pods_", 'where' => "`t`.`id` = {$current_row_id}", 'orderby' => "`t`.`id` DESC", 'page' => 1, 'limit' => 1, 'search' => false, 'strict' => true); if (empty($this->pod_data['object'])) { $params['table'] .= $this->pod_data['name']; } else { $params['table'] .= $this->pod_data['object']; } $row = pods_data()->select($params); if (!empty($row)) { $current_row = (array) $row; $row = get_object_vars((object) @current($current_row)); if (is_array($this->row) && !empty($this->row)) { $this->row = array_merge($row, $this->row); } else { $this->row = $row; } } } if (!empty($this->pod)) { pods_cache_set($id, $this->row, 'pods_items_' . $this->pod, 0); } } $this->row = $this->do_hook('fetch', $this->row, $id, $this->row_number); return $this->row; }
/** * Set cache value * * @param string $key Cache key * @param mixed $value Cache value * @param string $group Cache group * @param int|null $expires Cache expiration */ public function cache_set($key, $value, $group, $expires) { if (function_exists('pods_cache_set')) { pods_cache_set($key, $value, $group, $expires); } else { wp_cache_set($key, $value, $group, $expires); } }
/** * Take over the avatar served from WordPress * * @param string $avatar Default Avatar Image output from WordPress * @param int|string|object $id_or_email A user ID, email address, or comment object * @param int $size Size of the avatar image * @param string $default URL to a default image to use if no avatar is available * @param string $alt Alternate text to use in image tag. Defaults to blank * @return string <img> tag for the user's avatar */ public function get_avatar($avatar, $id_or_email, $size, $default = '', $alt = '') { // Don't replace for the Avatars section of the Discussion settings page if (is_admin()) { $current_screen = get_current_screen(); if (!is_null($current_screen) && 'options-discussion' == $current_screen->id && 32 == $size) { return $avatar; } } $_user_ID = 0; if (is_numeric($id_or_email) && 0 < $id_or_email) { $_user_ID = (int) $id_or_email; } elseif (is_object($id_or_email) && isset($id_or_email->user_id) && 0 < $id_or_email->user_id) { $_user_ID = (int) $id_or_email->user_id; } elseif (is_object($id_or_email) && isset($id_or_email->ID) && isset($id_or_email->user_login) && 0 < $id_or_email->ID) { $_user_ID = (int) $id_or_email->ID; } elseif (!is_object($id_or_email) && false !== strpos($id_or_email, '@')) { $_user = get_user_by('email', $id_or_email); if (!empty($_user)) { $_user_ID = (int) $_user->ID; } } // Include PodsMeta if not already included pods_meta(); if (0 < $_user_ID && !empty(PodsMeta::$user)) { $avatar_cached = pods_cache_get($_user_ID . '-' . $size, 'pods_avatars'); if (!empty($avatar_cached)) { $avatar = $avatar_cached; } else { $avatar_field = pods_transient_get('pods_avatar_field'); $user = current(PodsMeta::$user); if (empty($avatar_field)) { foreach ($user['fields'] as $field) { if ('avatar' == $field['type']) { $avatar_field = $field['name']; pods_transient_set('pods_avatar_field', $avatar_field); break; } } } elseif (!isset($user['fields'][$avatar_field])) { $avatar_field = false; } if (!empty($avatar_field)) { $user_avatar = get_user_meta($_user_ID, $avatar_field . '.ID', true); if (!empty($user_avatar)) { $attributes = array('alt' => '', 'class' => 'avatar avatar-' . $size . ' photo'); if (!empty($alt)) { $attributes['alt'] = $alt; } $user_avatar = pods_image($user_avatar, array($size, $size), 0, $attributes); if (!empty($user_avatar)) { $avatar = $user_avatar; pods_cache_set($_user_ID . '-' . $size, $avatar, 'pods_avatars'); } } } } } return $avatar; }