public static function pass_shortcode($atts, $content, $shortcode_name) { $args = array('field' => '', 'date_format' => '', 'in' => '', 'fields' => '', 'array' => '', 'debug' => '', 'field_loop' => '', 'taxonomy_loop' => '', 'list' => '', 'acf_gallery' => '', 'current' => '', 'orderby' => '', 'order' => '', 'hide_empty' => 'false', 'pre_render' => 'false', 'post_render' => 'true', 'trim' => 'false', 'count' => '9999', 'user_field' => '', 'user_fields' => '', 'global' => '', 'sub' => '', 'random' => ''); extract(shortcode_atts($args, $atts, true)); if ($pre_render == 'true') { $content = do_local_shortcode('ccs', $content, true); } if (CCS_Loop::$state['is_loop']) { $post_id = do_shortcode('[field id]'); } else { $post_id = get_the_ID(); } // Support nested $prefix = ''; if (substr($shortcode_name, 0, 2) == '--') { $prefix = '--'; } elseif (substr($shortcode_name, 0, 1) == '-') { $prefix = '-'; } if (!empty($date_format)) { // Date format: allow escape via "//" because "\" disappears in shortcode parameters $date_format = str_replace("//", "\\", $date_format); } /*--------------------------------------------- * * Pass single field to {FIELD} * */ if (!empty($global) && empty($field) && $field != '0') { $field = 'this'; } if (!empty($array)) { $field = $array; } if (!empty($field) || $field == '0') { if ($field == 'gallery') { $field = '_custom_gallery'; } // Support CCS gallery field // Pass global variable if (!empty($global)) { $field_value = ''; if ($global == 'route') { // Parsed URL route $request = CCS_URL::get_route(); $requests = CCS_URL::get_routes(); if ($field == 'this') { $field_value = $request; // whole thing for ($i = 0; $i < 6; $i++) { $part = ''; if (isset($requests[$i])) { $part = $requests[$i]; } $tag = '{' . $prefix . 'FIELD_' . ($i + 1) . '}'; $content = str_replace($tag, $part, $content); } } else { if (isset($requests[intval($field)])) { $field_value = $requests[intval($field)]; } } } elseif ($global == 'query') { // Parsed query string $field_value = CCS_URL::get_query(); $query_array = CCS_URL::get_queries(); foreach ($query_array as $key => $value) { $tag = '{' . $prefix . strtoupper($key) . '}'; $content = str_replace($tag, $value, $content); } if (!empty($fields)) { // Remove what was not rendered $fields = CCS_Loop::explode_list($fields); foreach ($fields as $key) { $tag = '{' . $prefix . strtoupper($key) . '}'; $content = str_replace($tag, '', $content); } $fields = ''; } elseif (!empty($field)) { $tag = '{' . $prefix . 'FIELD' . '}'; $value = isset($query_array[$field]) ? $query_array[$field] : ''; $content = str_replace($tag, $value, $content); } } else { if ($field == 'this' && isset($GLOBALS[$global])) { $field_value = $GLOBALS[$global]; } elseif (!empty($sub) && isset($GLOBALS[$global][$field][$sub])) { $field_value = $GLOBALS[$global][$field][$sub]; } elseif (isset($GLOBALS[$global][$field])) { $field_value = $GLOBALS[$global][$field]; } } // end global // Repeater or flexible content field } elseif (class_exists('CCS_To_ACF') && CCS_To_ACF::$state['is_repeater_or_flex_loop']) { if ($field == 'index') { $field_value = CCS_To_ACF::$state['repeater_index']; } elseif (function_exists('get_sub_field')) { $field_value = get_sub_field($field); } else { $field_value = null; } // Predefined or custom field } else { $field_value = CCS_Content::get_prepared_field($field, $post_id); } if (is_array($field_value)) { if (!empty($array)) { if ($debug == 'true') { ob_start(); echo '<pre><code>'; print_r($field_value); echo '</code></pre>'; return ob_get_clean(); } // array parameter foreach ($field_value as $key => $value) { $content = str_replace('{' . strtoupper($key) . '}', $value, $content); } } else { // field parameter foreach ($field_value as $key => $value) { $content = str_replace('{' . $prefix . 'FIELD:' . strtoupper($key) . '}', $value, $content); } } //$field_value = '(Array)'; $field_value = implode(",", $field_value); } else { // Clean extra spaces if it's a list $field_value = CCS_Loop::clean_list($field_value); } if (!empty($date_format)) { // Date format for custom field if (!empty($in) && $in == 'timestamp' && is_numeric($field_value)) { $field_value = gmdate("Y-m-d H:i:s", $field_value); } if ($date_format == 'true') { $date_format = get_option('date_format'); } $field_value = mysql2date($date_format, $field_value); } // Replace it $content = str_replace('{' . $prefix . 'FIELD}', $field_value, $content); /*--------------------------------------------- * * Pass each item in a list stored in a field * */ } elseif (!empty($field_loop)) { if ($field_loop == 'gallery' && class_exists('CCS_Gallery_Field')) { // Support gallery field $field_values = CCS_Gallery_Field::get_image_ids(); } else { $field_values = get_post_meta($post_id, $field_loop, true); } if (!empty($field_values)) { if (!is_array($field_values)) { $field_values = CCS_Loop::explode_list($field_values); } // Get comma-separated list of values $contents = null; // Loop for the number of field values foreach ($field_values as $field_value) { $contents[] = str_replace('{' . $prefix . 'FIELD}', $field_value, $content); } $content = implode('', $contents); } /*--------------------------------------------- * * Pass image IDs from ACF gallery * */ } elseif (!empty($acf_gallery)) { if (function_exists('get_field') && function_exists('get_sub_field')) { $field = $acf_gallery; $images = get_field($acf_gallery, $post_id, false); if (empty($field_value)) { // Try sub field $images = get_sub_field($acf_gallery, $post_id, false); } if (!empty($images)) { $ids = array(); foreach ($images as $image) { $ids[] = $image['id']; } if (is_array($ids)) { $replace = implode(',', $ids); } else { $replace = $ids; } $content = str_replace('{' . $prefix . 'FIELD}', $replace, $content); } } /*--------------------------------------------- * * Pass each taxonomy term * */ } elseif (!empty($taxonomy_loop)) { if ($current == 'true') { if (empty($orderby) && empty($order)) { // Doesn't accept order/orderby parameters - but it's cached $terms = get_the_terms($post_id, $taxonomy_loop); } else { $terms = wp_get_object_terms($post_id, $taxonomy_loop, array('orderby' => empty($orderby) ? 'name' : $orderby, 'order' => empty($order) ? 'ASC' : strtoupper($order))); } } else { // Get all terms: not by post ID $terms = get_terms($taxonomy_loop, array('orderby' => empty($orderby) ? 'name' : $orderby, 'order' => empty($order) ? 'ASC' : strtoupper($order), 'hide_empty' => $hide_empty == 'true')); } $contents = ''; // Loop through each term if (!empty($terms)) { $i = 0; foreach ($terms as $term) { if ($i++ >= $count) { break; } $slug = $term->slug; $id = $term->term_id; $name = $term->name; $replaced_content = str_replace('{' . $prefix . 'TERM}', $slug, $content); $replaced_content = str_replace('{' . $prefix . 'TERM_ID}', $id, $replaced_content); $replaced_content = str_replace('{' . $prefix . 'TERM_NAME}', $name, $replaced_content); $contents .= $replaced_content; } } $content = $contents; /*--------------------------------------------- * * Pass an arbitrary list of items * */ } elseif (!empty($list)) { $items = CCS_Loop::explode_list($list); // Comma-separated list -> array // Create range $new_items = array(); foreach ($items as $item) { if (strpos($item, '~') !== false) { $pair = CCS_Loop::explode_list($item, '~'); $list = range($pair[0], $pair[1]); foreach ($list as $list_item) { $new_items[] = $list_item; } } else { $new_items[] = $item; } } $items = $new_items; $contents = ''; foreach ($items as $item) { $replaced_content = $content; // Multiple items per loop if (strpos($item, ':') !== false) { $parts = explode(':', $item); $count = count($parts); for ($i = 0; $i < $count; $i++) { $this_item = trim($parts[$i]); // Index starts at ITEM_1 $replaced_content = str_replace('{' . $prefix . 'ITEM_' . ($i + 1) . '}', $this_item, $replaced_content); // Would this be useful? // $replaced_content = str_replace('{Item_'.$i.'}', ucfirst($this_item), // $replaced_content); } } else { $replaced_content = str_replace('{' . $prefix . 'ITEM}', $item, $replaced_content); $replaced_content = str_replace('{' . $prefix . 'Item}', ucfirst($item), $replaced_content); } $contents .= $replaced_content; } $content = $contents; } /*--------------------------------------------- * * Pass user field(s) * */ if (!empty($user_field)) { $user_field_value = do_shortcode('[user ' . $user_field . ' out="slug"]'); // Replace it $content = str_replace('{' . $prefix . 'USER_FIELD}', $user_field_value, $content); } if (!empty($user_fields)) { $user_fields_array = CCS_Loop::explode_list($user_fields); foreach ($user_fields_array as $this_field) { $user_field_value = do_shortcode('[user ' . $this_field . ' out="slug"]'); // Replace {FIELD_NAME} $content = str_replace('{' . $prefix . strtoupper($this_field) . '}', $user_field_value, $content); } } if (!empty($random)) { $content = str_replace('{' . $prefix . 'RANDOM}', do_shortcode('[random ' . $random . ']'), $content); } if (!empty($fields)) { if (!empty($global)) { $fields = CCS_Loop::explode_list($fields); foreach ($fields as $this_field) { $tag = '{' . $prefix . strtoupper($this_field) . '}'; $value = ''; if (isset($GLOBALS[$global][$this_field])) { $value = $GLOBALS[$global][$this_field]; } $content = str_replace($tag, $value, $content); } } else { // Replace these fields (+default) $content = CCS_Loop::render_field_tags($content, array('fields' => $fields)); } } else { $content = CCS_Loop::render_default_field_tags($content); } if ($post_render == 'true') { $content = do_local_shortcode('ccs', $content, true); } // Trim trailing white space and comma if ($trim != 'false') { if ($trim == 'true') { $trim = null; } $content = rtrim($content, " \t\n\r\v," . $trim); } return $content; }
public static function attached_shortcode($atts, $content, $tag) { $args = array('orderby' => '', 'order' => '', 'category' => '', 'count' => '', 'offset' => '', 'trim' => '', 'id' => '', 'field' => '', 'columns' => '', 'pad' => '', 'between' => ''); extract(shortcode_atts($args, $atts, true)); /*--------------------------------------------- * * Get attachments * */ $attachment_ids = array(); if (!empty($id)) { $parent_id = 0; // Any post } elseif (!empty($field)) { // Specific attachment ID from field $id = do_shortcode('[field ' . $field . ']'); if (empty($id)) { return; } $parent_id = 0; // Any post } else { $parent_id = do_shortcode('[field id]'); // Attachments of current post if (empty($parent_id)) { return; } } if (isset($atts[0]) && $atts[0] == 'gallery') { // Get attachment IDs from gallery field $attachment_ids = CCS_Gallery_Field::get_image_ids($parent_id); // Support for orderby title if ($orderby == 'title') { usort($attachment_ids, array($this, 'sort_gallery_by_title')); } } else { $attach_args = array('post_parent' => $parent_id, 'post_type' => 'attachment', 'post_status' => 'any', 'posts_per_page' => '-1'); // default orderby $attach_args['orderby'] = empty($orderby) ? 'date' : $orderby; // default for titles if ($orderby == 'title') { $order = empty($order) ? 'ASC' : $order; } if (!empty($order)) { $attach_args['order'] = $order; } if (!empty($category)) { $attach_args['category'] = $category; } if (!empty($count)) { $attach_args['posts_per_page'] = $count; } if (!empty($offset)) { $attach_args['offset'] = $offset; } if (!empty($id)) { $attach_args['post__in'] = CCS_Loop::explode_list($id); $attach_args['orderby'] = empty($orderby) ? 'post__in' : $orderby; unset($attach_args['post_parent']); } // Get attachments for current post $posts = get_posts($attach_args); $index = 0; foreach ($posts as $post) { $attachment_ids[$index] = $post->ID; // Keep it in order $index++; } } // If no images in gallery field if (count($attachment_ids) == 0) { return null; } /*--------------------------------------------- * * Compile template * */ $out = array(); // if nested, save previous state if ($tag[0] == '-') { $prev_state = self::$state; } self::$state['is_attachment_loop'] = true; foreach ($attachment_ids as $index => $attachment_id) { self::$state['current_attachment_id'] = $attachment_id; $out[] = do_ccs_shortcode($content); } self::$state['is_attachment_loop'] = false; // if nested, restore previous state if ($tag[0] == '-') { self::$state = $prev_state; } /*--------------------------------------------- * * Post-process * * TODO: Combine this with loop and others * */ if (!empty($columns)) { $out = CCS_Loop::render_columns($out, $columns, $pad, $between); } else { $out = implode('', $out); if ($trim == 'true') { $out = trim($out, " \t\n\r\v,"); } } return $out; }
static function get_the_field($parameters, $id = null) { $result = ''; $field = $parameters['field']; if (!empty($parameters['id'])) { $id = $parameters['id']; } /*--------------------------------------------- * * Option field * */ if (!empty($parameters['option'])) { return self::get_option_field($field, $parameters['option']); } /*--------------------------------------------- * * Attachment field * */ if (!empty($parameters['type']) && $parameters['type'] == 'attachment' || CCS_Loop::$state['is_attachment_loop'] || CCS_Attached::$state['is_attachment_loop']) { return self::get_the_attachment_field($parameters); /*--------------------------------------------- * * Array field * */ } elseif (self::$state['is_array_field']) { $array = self::$state['current_field_value']; if (isset($array[$field])) { return $array[$field]; } elseif ($field == 'value') { if (is_array($array)) { $array = implode('', $array); } return $array; } // ACF gallery loop } elseif (class_exists('CCS_To_ACF') && CCS_To_ACF::$state['is_gallery_loop']) { return CCS_To_ACF::get_image_details_from_acf_gallery($parameters); /*--------------------------------------------- * * Repeater or flexible content loop * */ } elseif (empty($parameters['id']) && empty($parameters['name']) && class_exists('CCS_To_ACF') && CCS_To_ACF::$state['is_repeater_or_flex_loop']) { // If not inside relationship loop if (!CCS_To_ACF::$state['is_relationship_loop']) { // Get sub field if (function_exists('get_sub_field')) { return get_sub_field($field); } else { return null; } } /*--------------------------------------------- * * Menu loop * */ } elseif (CCS_Menu::$state['is_menu_loop']) { if (isset(CCS_Menu::$state['current_menu_object'][$field])) { return CCS_Menu::$state['current_menu_object'][$field]; } else { if (!isset(CCS_Menu::$state['current_menu_object']['id'])) { return; } // Get it from the post $id = CCS_Menu::$state['current_menu_object']['id']; CCS_Menu::$state['is_menu_loop'] = false; $result = do_shortcode('[field ' . $field . ' id=' . $id . ']'); CCS_Menu::$state['is_menu_loop'] = true; return $result; } } if (!empty($id)) { $post_id = $id; } else { // Default $post_id = get_the_ID(); /* global $post; if (!empty($post)) { $post_id = $post->ID; } */ } if (empty($post_id)) { return null; } // No post ID $post = get_post($post_id); if (empty($post)) { return null; } // No post /*--------------------------------------------- * * Prepare image attributes * */ $image_fields = array('image', 'image-full', 'image-link', 'image-link-self', 'thumbnail', 'thumbnail-link', 'thumbnail-link-self', 'gallery'); if ($field == 'thumbnail' && empty($parameters['size'])) { $parameters['size'] = 'thumbnail'; // Default thumbnail } $attr = array(); if (in_array($field, $image_fields)) { if (!empty($parameters['width']) || !empty($parameters['height'])) { $parameters['size'] = array((int) $parameters['width'], (int) $parameters['height']); } if (!empty($parameters['image_class'])) { $attr['class'] = $parameters['image_class']; } if (!empty($parameters['nopin'])) { $attr['nopin'] = $parameters['nopin']; } if (!empty($parameters['alt'])) { $attr['alt'] = $parameters['alt']; } // if (!empty($parameters['title'])) // $attr['title'] = $parameters['title']; } // Custom field only? if (!empty($parameters['custom']) && $parameters['custom'] == 'true') { $custom = $field; $field = 'custom'; // Skip predefined fields } /*--------------------------------------------- * * Pre-defined fields * */ switch ($field) { case 'id': $result = $post_id; break; case 'url': $result = get_permalink($post_id); break; case 'edit-url': $result = get_edit_post_link($post_id); break; case 'edit-link': $result = $post->post_title; break; case 'edit-link-self': $result = $post->post_title; break; case 'slug': $result = $post->post_name; break; case 'post-type': $result = $post->post_type; break; case 'post-type-name': $post_type = $post->post_type; $obj = get_post_type_object($post_type); $result = $obj->labels->singular_name; break; case 'post-type-plural': $post_type = $post->post_type; $obj = get_post_type_object($post_type); $result = $obj->labels->name; break; case 'post-status': $result = $post->post_status; if ($parameters['out'] !== 'slug') { $result = ucwords($result); } break; case 'post-format': if (function_exists('get_post_format')) { $result = get_post_format($post_id); } break; case 'post-format-name': if (function_exists('get_post_format')) { $result = get_post_format($post_id); $result = ucwords($result); } break; case 'post-format-link': if (function_exists('get_post_format')) { $result = get_post_format($post_id); $result = '<a href="' . get_post_format_link($result) . '">' . ucwords($result) . '</a>'; } break; case 'post-format-url': if (function_exists('get_post_format')) { $result = get_post_format_link(get_post_format($post_id)); } break; case 'parent-id': $parent_id = isset($post->post_parent) ? $post->post_parent : 0; if (!empty($parent_id)) { $result = $parent_id; } break; case 'parent-slug': $parent_id = isset($post->post_parent) ? $post->post_parent : 0; if (!empty($parent_id)) { $post_data = get_post($parent_id); if (!empty($post_data)) { $result = isset($post_data->post_name) ? $post_data->post_name : ''; } } break; case 'link': case 'title-link': case 'title-link-out': case 'title': $result = $post->post_title; break; case 'author': $author_id = $post->post_author; $user = get_user_by('id', $author_id); if (!empty($parameters['meta'])) { $result = get_the_author_meta($parameters['meta'], $author_id); } elseif (!empty($user)) { $result = $user->display_name; } break; case 'author-id': $result = $post->post_author; break; case 'author-url': $result = get_author_posts_url($post->post_author); break; case 'author-login': $author_id = $post->post_author; $result = do_shortcode('[users id=' . $author_id . '][user name][/users]'); break; case 'avatar': if (!empty($parameters['size'])) { $result = get_avatar($post->post_author, $parameters['size']); } else { $result = get_avatar($post->post_author); } break; case 'date': if (!empty($parameters['date_format'])) { if ($parameters['date_format'] == 'relative') { $result = CCS_Format::get_relative_date($post->post_date); } else { $result = mysql2date($parameters['date_format'], $post->post_date); } } else { // Default date format under Settings -> General $result = mysql2date(get_option('date_format'), $post->post_date); } break; case 'modified': if (!empty($parameters['date_format'])) { $result = get_post_modified_time($parameters['date_format'], $gmt = false, $post_id, $translate = true); } else { // Default date format under Settings -> General $result = get_post_modified_time(get_option('date_format'), $gmt = false, $post_id, $translate = true); } break; case 'image-full': $parameters['size'] = 'full'; case 'image': // image // image case 'image-link': // image with link to post // image with link to post case 'image-link-self': // image with link to attachment page $parameters['size'] = isset($parameters['size']) && !empty($parameters['size']) ? $parameters['size'] : 'full'; if (empty($attr['alt'])) { $attr['alt'] = self::wp_get_featured_image_field($post_id, 'alt'); } $result = get_the_post_thumbnail($post_id, $parameters['size'], $attr); break; case 'image-url': $parameters['size'] = isset($parameters['size']) && !empty($parameters['size']) ? $parameters['size'] : 'full'; $src = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), $parameters['size']); $result = $src['0']; // $result = wp_get_attachment_url(get_post_thumbnail_id($post_id)); break; case 'image-title': case 'image-caption': case 'image-alt': case 'image-description': $image_field_name = substr($field, 6); // Remove "image-" $result = self::wp_get_featured_image_field($post_id, $image_field_name); break; case 'thumbnail': // thumbnail // thumbnail case 'thumbnail-link': // thumbnail with link to post // thumbnail with link to post case 'thumbnail-link-self': // thumbnail with link to attachment page $parameters['size'] = isset($parameters['size']) && !empty($parameters['size']) ? $parameters['size'] : 'thumbnail'; if (empty($attr['alt'])) { $attr['alt'] = self::wp_get_featured_image_field($post_id, 'alt'); } $result = get_the_post_thumbnail($post_id, $parameters['size'], $attr); break; case 'thumbnail-url': $src = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), 'thumbnail'); $result = $src['0']; break; case 'tags': $result = implode(' ', wp_get_post_tags($post_id, array('fields' => 'names'))); break; case 'gallery': // Get specific image from gallery field if (class_exists('CCS_Gallery_Field')) { // Check if gallery field is enabled $attachment_ids = CCS_Gallery_Field::get_image_ids($post_id); if (empty($parameters['num'])) { $parameters['num'] = 1; } if (empty($parameters['size'])) { $parameters['size'] = 'full'; } $result = wp_get_attachment_image($attachment_ids[$parameters['num'] - 1], $parameters['size'], $icon = false, $attr); } break; case 'excerpt': // Get excerpt // $result = get_the_excerpt(); $result = $post->post_excerpt; if (empty($result)) { // If empty, get it from post content $result = $post->post_content; if (empty($parameters['words']) && empty($parameters['length'])) { self::$parameters['words'] = 25; } } break; case 'debug': ob_start(); echo '<pre>'; print_r(get_post_custom($post_id)); echo '</pre>'; if (function_exists('acf_get_fields_by_id')) { echo '<pre>'; print_r(acf_get_fields_by_id($post_id)); echo '</pre>'; } $result = ob_get_clean(); break; case 'loop-count': $result = CCS_Loop::$state['loop_count']; break; default: /*--------------------------------------------- * * Custom field * */ if (!empty($parameters['custom']) && $parameters['custom'] == 'true') { $field = $custom; } $result = get_post_meta($post_id, $field, true); if (is_numeric($result) && !empty($parameters['return'])) { if ($parameters['return'] == 'page-link') { // ACF page link: get URL from post ID $result = get_permalink($result); } else { // Get attachment field $parameters['id'] = $result; $parameters['field'] = $parameters['return']; $result = self::get_the_attachment_field($parameters); } } elseif (!empty($parameters['property']) && is_object($result)) { $result = self::get_object_property($result, $parameters['property']); } elseif (!empty($parameters['currency']) || !empty($parameters['decimals']) || !empty($parameters['point']) || !empty($parameters['thousands'])) { $currency = !empty($parameters['currency']) ? $parameters['currency'] : ''; $decimals = !empty($parameters['decimals']) ? $parameters['decimals'] : 2; $point = !empty($parameters['point']) ? $parameters['point'] : '.'; $thousands = !empty($parameters['thousands']) ? $parameters['thousands'] : ','; $result = CCS_Format::getCurrency($result, $currency, $decimals, $point, $thousands); } break; } return $result; }
public static function prepare_query($parameters) { $query = array(); /*--------------------------------------------- * * field="gallery" * */ if ($parameters['field'] == 'gallery' && class_exists('CCS_Gallery_Field')) { // Gallery field $parameters['type'] = 'attachment'; // $query['post_parent'] = get_the_ID(); self::$state['is_attachment_loop'] = true; $parameters['id'] = implode(',', CCS_Gallery_Field::get_image_ids(get_the_ID())); $parameters['field'] = ''; } /*--------------------------------------------- * * Post type * */ if (!empty($parameters['type'])) { $query['post_type'] = self::explode_list($parameters['type']); } else { $query['post_type'] = 'any'; } /*--------------------------------------------- * * Post ID, exclude ID, name * */ if (!empty($parameters['include'])) { if (!empty($parameters['id'])) { $parameters['id'] .= ','; } $parameters['id'] .= $parameters['include']; } if (!empty($parameters['id'])) { $id_array = self::explode_list($parameters['id']); foreach ($id_array as $key => $value) { // Include current post if ($value == 'this') { // ID of post that contains the loop $id_array[$key] = self::$state['original_post_id']; // Include child posts and descendants } elseif ($value == 'children') { // Query for top-level posts first if (empty($parameters['parent'])) { $query['post_parent'] = 0; } // Then manually get descendants after each post self::$state['include_descendants'] = true; unset($id_array[$key]); // Include by post slug } elseif (!is_numeric($value)) { $get_id = self::get_post_id(array('name' => $value, 'type' => $parameters['type'])); unset($id_array[$key]); if (!empty($get_id)) { $id_array[$key] = $get_id; } else { $id_array[$key] = 99999; // Prevent empty } } } if (count($id_array) > 0) { $query['post__in'] = $id_array; $query['orderby'] = 'post__in'; // Preserve ID order } } if (!empty($parameters['exclude'])) { $id_array = self::explode_list($parameters['exclude']); foreach ($id_array as $key => $value) { // Exclude current post if ($value == 'this') { // ID of post that contains the loop $id_array[$key] = self::$state['original_post_id']; // Top-level posts only } elseif ($value == 'children') { unset($id_array[$key]); $query['post_parent'] = 0; // Exclude by post slug } elseif (!is_numeric($value)) { $get_id = self::get_post_id(array('name' => $value, 'type' => $parameters['type'])); unset($id_array[$key]); if (!empty($get_id)) { $id_array[$key] = $get_id; } } } if (count($id_array)) { $query['post__not_in'] = $id_array; } } elseif (!empty($parameters['name'])) { $query['name'] = $parameters['name']; } /*--------------------------------------------- * * Parent * */ if (!empty($parameters['parent'])) { $parent = $parameters['parent']; /* if ( $parent=='this' ) { // Get children of current post $query['post_parent'] = get_the_ID(); if (!$query['post_parent']) $query['post_parent'] = '-1'; // If no current post } elseif ( $parent=='same' ) { // Get siblings of current post $query['post_parent'] = wp_get_post_parent_id( get_the_ID() ); if (!$query['post_parent']) $query['post_parent'] = '-1'; // If current post has no parent } elseif ( is_numeric($parent) ) { $query['post_parent'] = intval( $parent ); // Single parent ID } else { */ // Multiple IDs $parents = self::explode_list($parent); // Convert to array $parent_IDs = array(); foreach ($parents as $each_parent) { if ($each_parent == 'this') { // Get children of current post $parent_IDs[] = get_the_ID(); } elseif ($parent == 'same') { // Get siblings of current post, if it has parent $parent_ID = wp_get_post_parent_id(get_the_ID()); if ($parent_ID) { $parent_IDs[] = $parent_ID; } } elseif (is_numeric($each_parent)) { // by ID $parent_IDs[] = intval($each_parent); } else { // by slug $posts = get_posts(array('name' => $each_parent, 'post_type' => $query['post_type'], 'posts_per_page' => '1')); if ($posts) { $parent_IDs[] = $posts[0]->ID; } } } if (count($parent_IDs) == 0) { return null; } // No parent $query['post_parent__in'] = $parent_IDs; /* } // End single/multiple */ } // End if parent pameter /*--------------------------------------------- * * Sticky posts: ignore by default * */ if (empty($parameters['sticky'])) { $query['ignore_sticky_posts'] = true; } /*--------------------------------------------- * * Search keyword * */ if (!empty($parameters['search'])) { $query['s'] = $parameters['search']; } /*--------------------------------------------- * * User role * */ if (!empty($parameters['role'])) { if ($parameters['role'] == 'this') { $parameters['role'] = do_shortcode('[user role out="slug"]'); } $roles = self::explode_list($parameters['role']); foreach ($roles as $role) { // Make a list of authors in this user role $authors = do_shortcode('[users role="' . $role . '" trim="true"][user id],[/users]'); if (!empty($parameters['author'])) { $parameters['author'] .= ','; } $parameters['author'] .= $authors; } } /*--------------------------------------------- * * Post author * */ if (!empty($parameters['author'])) { $authors = self::explode_list($parameters['author']); foreach ($authors as $author) { if (is_numeric($author)) { // Author ID $query['author__in'][] = $author; } else { if ($author == 'this') { // Current user ID $query['author__in'][] = CCS_User::get_user_field('id'); } elseif ($author == 'same') { // Same author as current post $current_post = get_post(get_the_ID()); if ($current_post) { $query['author__in'][] = $current_post->post_author; } } else { // Get author ID from login name $author_data = get_user_by('login', $author); if ($author_data) { $query['author__in'][] = $author_data->ID; } else { // No author by that name: use an arbitrary ID $query['author__in'][] = 9999; } } } } } if (!empty($parameters['author_exclude'])) { $authors = self::explode_list($parameters['author_exclude']); foreach ($authors as $author) { if (is_numeric($author)) { // Author ID $query['author__not_in'][] = $author; } else { if ($author == 'this') { // Current user ID $query['author__not_in'][] = CCS_User::get_user_field('id'); } else { // Get author ID from login name $author_data = get_user_by('login', $author); if ($author_data) { $query['author__not_in'][] = $author_data->ID; } } } } } /*--------------------------------------------- * * Post status * */ if (!empty($parameters['status'])) { $query['post_status'] = self::explode_list($parameters['status']); } else { // Default if ($parameters['type'] == 'attachment') { $query['post_status'] = array('any'); } else { $query['post_status'] = array('publish'); } } /*--------------------------------------------- * * Post count, offset, paged * */ if (!empty($parameters['offset'])) { $query['offset'] = $parameters['offset']; } if (!empty($parameters['paged'])) { if (class_exists('CCS_Paged')) { if (!empty($parameters['maxpage'])) { self::$state['maxpage'] = $parameters['maxpage']; } if (is_numeric($parameters['paged'])) { $parameters['count'] = $parameters['paged']; } else { $parameters['count'] = 5; } $paged = 1; if (empty($parameters['query'])) { // Get from query string $query_var = CCS_Paged::$prefix; if (self::$state['paged_index'] == 0) { self::$state['paged_index'] = 1; } else { self::$state['paged_index']++; } if (self::$state['paged_index'] > 1) { $query_var .= self::$state['paged_index']; } $paged = isset($_GET[$query_var]) ? $_GET[$query_var] : 1; } else { // From permalink $paged = max(1, get_query_var(CCS_Paged::$prefix)); } $query['paged'] = $paged; } } if (!empty($parameters['page'])) { $query['paged'] = $parameters['page']; // Specific page } // Work around if using both offset and pagination // Reference: http://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination if (!empty($parameters['paged']) && $query['paged'] > 1 && !empty($query['offset'])) { $query['offset'] = $query['offset'] + ($query['paged'] - 1) * $parameters['count']; } if (!empty($parameters['count'])) { if ($parameters['orderby'] == 'rand') { $query['posts_per_page'] = '-1'; // For random, get all posts and count later } else { $query['posts_per_page'] = $parameters['count']; } } else { if (!empty($query['offset'])) { $query['posts_per_page'] = '99999'; // Show all posts (to make offset work) } else { $query['posts_per_page'] = '-1'; // Show all posts (normal method) } } /*--------------------------------------------- * * Category * */ if (!empty($parameters['category'])) { // Category can be slug, ID, multiple $category = $parameters['category']; $categories = self::explode_list($category, ',+'); $check_category = array_pop($categories); // Check one item if (!empty($parameters['compare']) && strtoupper($parameters['compare']) == 'AND') { $category = str_replace(',', '+', $category); } if (is_numeric($check_category)) { $query['cat'] = $category; // ID(s) } elseif ($category == 'this') { $category = do_shortcode('[taxonomy category field="id"]'); if (empty($category)) { return; } if (!empty($parameters['compare']) && strtoupper($parameters['compare']) == 'AND') { $category = str_replace(',', '+', $category); } $query['cat'] = $category; } else { $query['category_name'] = $category; // Slug(s) } } /*--------------------------------------------- * * Tag * */ if (!empty($parameters['tag'])) { // Remove extra space in a list $tags = self::clean_list($parameters['tag']); if ($tags == 'this') { $tags = do_shortcode('[taxonomy tag field="slug"]'); if (empty($tags)) { return; } $tags = str_replace(' ', ',', $tags); } if (!empty($parameters['compare']) && strtoupper($parameters['compare']) == 'AND') { $tags = str_replace(',', '+', $tags); } $query['tag'] = $tags; } /*--------------------------------------------- * * Taxonomy * */ // In a [for] loop, filter by each taxonomy term unless specified otherwise if (CCS_ForEach::$state['is_for_loop']) { $parameters['taxonomy'] = empty($parameters['taxonomy']) ? CCS_ForEach::$current_term[CCS_ForEach::$index]['taxonomy'] : $parameters['taxonomy']; $parameters['term'] = empty($parameters['term']) ? CCS_ForEach::$current_term[CCS_ForEach::$index]['slug'] : $parameters['term']; } if (!empty($parameters['taxonomy'])) { $query['tax_query'] = array(); // Support multiple taxonomy queries $max = 4; for ($index = 0; $index < $max; $index++) { if ($index == 0) { $suffix = ''; } else { $suffix = '_' . ($index + 1); // field_2, field_3, ... } if (!empty($parameters['taxonomy' . $suffix])) { if ($index == 1) { $relation = !empty($parameters['relation']) ? strtoupper($parameters['relation']) : 'AND'; $query['tax_query']['relation'] = $relation; } $taxonomy = $parameters['taxonomy' . $suffix]; $term = ''; if (!empty($parameters['term' . $suffix])) { $term = $parameters['term' . $suffix]; } elseif (!empty($parameters['value' . $suffix])) { $term = $parameters['value' . $suffix]; } // Alias, if field value is not used // Post Format if ($taxonomy == 'format') { $taxonomy = 'post_format'; $term = 'post-format-' . $term; } $args = array('taxonomy' => $taxonomy, 'field' => !empty($parameters['taxonomy_field']) ? $parameters['taxonomy_field'] : 'term_id', 'term' => $term, 'compare' => !empty($parameters['compare' . $suffix]) ? strtoupper($parameters['compare' . $suffix]) : 'IN'); $query['tax_query'][] = self::prepare_tax_query($args); } } // End each taxonomy query } // End taxonomy query /*--------------------------------------------- * * Order and orderby * */ if (!empty($parameters['comment_author']) && empty($parameters['orderby'])) { $parameters['orderby'] = 'comment-date'; } // Order by comment date if ($parameters['orderby'] == 'comment-date') { // WP_Query doesn't support it, so sort after getting all posts self::$state['parameters']['orderby_comment_date'] = 'true'; self::$state['parameters']['order_comment_date'] = !empty($parameters['order']) ? strtoupper($parameters['order']) : 'DESC'; $parameters['orderby'] = ''; } elseif (!empty($parameters['order'])) { $query['order'] = $parameters['order']; } if (!empty($parameters['orderby'])) { $orderby = $parameters['orderby']; $default_orderby = array('none', 'id', 'author', 'title', 'name', 'type', 'date', 'modified', 'parent', 'rand', 'comment_count', 'menu_order', 'meta_value', 'meta_value_num'); // Alias if ($orderby == 'id') { $orderby = 'ID'; } elseif ($orderby == 'field_num') { $orderby = 'meta_value_num'; } elseif ($orderby == 'field') { $orderby = 'meta_value'; } elseif ($orderby == 'menu') { $orderby = 'menu_order'; } elseif (!in_array(strtolower($orderby), $default_orderby)) { // If not recognized, assume it's a field name if (empty($parameters['field'])) { $parameters['key'] = $orderby; } $orderby = 'meta_value'; // or meta_value_num? } $query['orderby'] = $orderby; if (in_array($orderby, array('meta_value', 'meta_value_num'))) { if (!empty($parameters['key'])) { $key = $parameters['key']; } elseif (!empty($parameters['field'])) { // If no key is specified, order by field $key = $parameters['field']; } else { $key = ''; } // No orderby key $query['meta_key'] = $key; if (!empty($parameters['meta_type'])) { $query['meta_type'] = $parameters['meta_type']; } } if (empty($parameters['order'])) { // Default order if (in_array($orderby, array('meta_value', 'meta_value_num', 'menu_order', 'title', 'name', 'id'))) { $query['order'] = 'ASC'; } else { $query['order'] = 'DESC'; } } } /*--------------------------------------------- * * Multiple orderby * * Sort by multiple fields: orderby_2, orderby_3 * */ if (!empty($parameters['orderby_2'])) { $first_orderby = $query['orderby']; $to_num = $first_orderby == 'meta_value_num' ? '+0' : ''; // Start building orderby query for filter self::$state['multiple_orderby'] = 'mt1.meta_value' . $to_num . ' ' . $query['order']; // Include orderby fields in query $query['meta_query'] = array('relation' => 'AND', array('key' => $query['meta_key'], 'compare' => 'EXISTS')); // Up to five orderby fields for ($i = 2; $i <= 5; $i++) { if (empty($parameters['orderby_' . $i])) { break; } // Alias if ($parameters['orderby_' . $i] == 'field_num') { $parameters['orderby_' . $i] = 'meta_value_num'; } if ($parameters['orderby_' . $i] == 'meta_value_num') { $next_orderby = $parameters['orderby_' . $i]; $next_orderby_field = @$parameters['key_' . $i]; } else { $next_orderby = 'meta_value'; $next_orderby_field = $parameters['orderby_' . $i]; } // Include additional orderby field in query $query['meta_query'][] = array('key' => $next_orderby_field, 'compare' => 'EXISTS'); $next_order = !empty($parameters['order_' . $i]) ? strtoupper($parameters['order_' . $i]) : 'ASC'; $to_num = $next_orderby == 'meta_value_num' ? '+0' : ''; // Add orderby field to database query filter self::$state['multiple_orderby'] .= ', mt' . $i . '.meta_value' . $to_num . ' ' . $next_order; } if (!empty($next_orderby_field)) { add_filter('posts_orderby', array(__CLASS__, 'multiple_orderby_filter')); } } //debug_array($query); /*--------------------------------------------- * * Sort by series * */ if (!empty($parameters['series'])) { // TODO: Just use range() // Remove white space $series = str_replace(' ', '', $parameters['series']); // Expand range: 1-3 -> 1,2,3 /* PHP 5.3+ $series = preg_replace_callback('/(\d+)-(\d+)/', function($m) { return implode(',', range($m[1], $m[2])); }, $series); */ /* Compatible with PHP 5.2 and below */ $callback = create_function('$m', 'return implode(\',\', range($m[1], $m[2]));'); $series = preg_replace_callback('/(\\d+)-(\\d+)/', $callback, $series); // Store posts IDs and key self::$state['sort_posts'] = self::explode_list($series); self::$state['sort_key'] = $parameters['key']; // Get the posts to be sorted later $query['meta_query'] = array(array('key' => self::$state['sort_key'], 'value' => self::$state['sort_posts'], 'compare' => 'IN')); } /*--------------------------------------------- * * Published date * */ if (!empty($parameters['year']) || !empty($parameters['month']) || !empty($parameters['day'])) { $q = array(); $today = getdate(); if (!empty($parameters['year'])) { $year = $parameters['year']; if ($year == 'today' || $year == 'this') { $year = $today['year']; } $q['year'] = $year; } if (!empty($parameters['month'])) { $month = $parameters['month']; if (empty($parameters['year'])) { $q['year'] = $today['year']; } if ($month == 'today' || $month == 'this') { $month = $today['mon']; } $q['month'] = $month; } if (!empty($parameters['day'])) { $day = $parameters['day']; if (empty($parameters['year'])) { $q['year'] = $today['year']; } if (empty($parameters['month'])) { $q['month'] = $today['mon']; } if ($day == 'today' || $day == 'this') { $day = $today['mday']; } $q['day'] = $day; } $query['date_query'] = array($q); } /*--------------------------------------------- * * Date query: before and after * */ // Support multiple date field queries $max = 4; $now = current_time('timestamp'); for ($index = 0; $index < $max; $index++) { if ($index == 0) { $suffix = ''; } else { $suffix = '_' . ($index + 1); // field_2, field_3, ... } if (empty($parameters['field' . $suffix])) { if (!empty($parameters['before' . $suffix])) { if (!isset($query['date_query'])) { $query['date_query'] = array(); } $query['date_query'][] = array('before' => $parameters['before' . $suffix]); } if (!empty($parameters['after' . $suffix])) { if (!isset($query['date_query'])) { $query['date_query'] = array(); } $query['date_query'][] = array('after' => $parameters['after' . $suffix]); } } elseif ($parameters['field' . $suffix] == 'date' && !empty($parameters['value' . $suffix])) { $today = gmdate('Y-n-j', $now); if (!isset($query['date_query'])) { $query['date_query'] = array(); } if ($parameters['value' . $suffix] == 'today') { $query['date_query'][] = array('after' => $today . ' 00:00:00', 'before' => $today . ' 23:59:59'); } elseif ($parameters['value' . $suffix] == 'past') { $query['date_query'][] = array('before' => $today . ' 00:00:00'); } elseif ($parameters['value' . $suffix] == 'future') { $query['date_query'][] = array('after' => $today . ' 00:00:00'); } //debug_array($query['date_query']); echo '<hr>'; unset($parameters['field' . $suffix]); // Don't do field/value compare } else { // Other field if (!empty($parameters['value' . $suffix]) && $parameters['value' . $suffix] == 'today-between') { $today = gmdate('Y-n-j', $now); $parameters['after' . $suffix] = $today . ' 00:00:00'; $parameters['before' . $suffix] = $today . ' 23:59:59'; } if (!empty($parameters['before' . $suffix]) && !empty($parameters['after' . $suffix])) { // Between before and after $parameters['value' . $suffix] = strtotime($parameters['after' . $suffix] . ' +0000', $now); $parameters['value' . $suffix] .= ',' . strtotime($parameters['before' . $suffix] . ' +0000', $now); $parameters['compare' . $suffix] = 'BETWEEN'; } elseif (!empty($parameters['before' . $suffix])) { $parameters['value' . $suffix] = strtotime($parameters['before' . $suffix] . ' +0000', $now); $parameters['compare' . $suffix] = !empty($parameters['compare' . $suffix]) ? $parameters['compare' . $suffix] : 'OLD'; } elseif (!empty($parameters['after' . $suffix])) { $parameters['value' . $suffix] = strtotime($parameters['after' . $suffix] . ' +0000', $now); $parameters['compare' . $suffix] = !empty($parameters['compare' . $suffix]) ? $parameters['compare' . $suffix] : 'NEW'; //echo 'AFTER: '.date('r',$parameters['value'.$suffix]).' '.$parameters['value'.$suffix].'<br>'; } } } // End each date field query /*--------------------------------------------- * * Field value * */ if (!empty($parameters['field']) && !empty($parameters['value'])) { if (!isset($query['meta_query'])) { $query['meta_query'] = array(); } // Support multiple field value queries $max = 4; $suffix = ''; for ($index = 0; $index < $max; $index++) { if ($index > 0) { $suffix = '_' . ($index + 1); // field_2, field_3, ... if ($index == 1) { // Only one relation possible..? $relation = $parameters['relation']; $relation = !empty($relation) ? strtoupper($relation) : 'AND'; $query['meta_query']['relation'] = $relation; } } if (!empty($parameters['field' . $suffix]) && !empty($parameters['value' . $suffix])) { $args = array('field' => $parameters['field' . $suffix], 'compare' => strtoupper($parameters['compare' . $suffix])); if (!empty($parameters['in' . $suffix])) { $args['in'] = $parameters['in' . $suffix]; } if (!empty($parameters['date_format' . $suffix])) { $args['date_format'] = $parameters['date_format' . $suffix]; } else { // If any date format set, apply it by default if (!empty($parameters['date_format'])) { $args['date_format'] = $parameters['date_format']; } elseif (!empty($parameters['field']) && $parameters['field'] == 'last_viewed') { $args['date_format'] = 'Y-m-d H:i:s'; } } // Support value="1,2,3" if ($args['compare'] == 'BETWEEN') { $values = array($parameters['value' . $suffix]); } else { $values = CCS_Loop::explode_list($parameters['value' . $suffix]); } $j = 0; $_args = array(); if (count($values) > 1) { $_args['relation'] = 'OR'; } foreach ($values as $value) { $args['value'] = $value; $_args[] = self::prepare_meta_query($args); $j++; } if (count($values) > 1) { $query['meta_query'][] = $_args; } else { $query['meta_query'][] = $_args[0]; } } // if field and value exist } // For each field //debug_array($query['meta_query']); echo '<hr>'; } // End field value query /*--------------------------------------------- * * Sort by multiple custom fields * */ if (!empty($parameters['sort_field'])) { if (!isset($query['meta_query'])) { $query['meta_query'] = array(); } $query['meta_query'][] = self::prepare_meta_query($args); } //debug_array($query); return apply_filters('ccs_loop_query_filter', $query); }