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; }
function merge_with_defaults($parameters) { self::$original_parameters = $parameters; $defaults = array('type' => 'any', 'status' => 'publish', 'name' => '', 'id' => '', 'field' => '', 'page' => '', 'link_text' => '', 'custom' => '', 'glue' => ', ', 'taxonomy' => '', 'term' => '', 'term_name' => '', 'out' => '', 'image' => '', 'size' => 'full', 'in' => '', 'return' => '', 'alt' => '', 'title' => '', 'height' => '', 'width' => '', 'image_class' => '', 'nopin' => '', 'url' => '', 'cropped' => '', 'meta' => '', 'checkbox' => '', 'area' => '', 'sidebar' => '', 'menu' => '', 'ul' => '', 'cb' => '', 'menu_slug' => '', 'gallery' => 'false', 'group' => '', 'orderby' => '', 'order' => '', 'columns' => '', 'include' => '', 'exclude' => '', 'row' => '', 'sub' => '', 'acf_gallery' => '', 'num' => '', 'acf_date' => '', 'option' => '', 'site' => '', 'more' => '', 'link' => '', 'dots' => 'false', 'between' => 'false', 'property' => '', 'format' => '', 'slugify' => '', 'shortcode' => '', 'escape' => '', 'unescape' => '', 'filter' => '', 'texturize' => '', 'import' => '', 'embed' => '', 'http' => '', 'nl' => '', 'align' => '', 'class' => '', 'height' => '', 'date_format' => '', 'timestamp' => '', 'new' => '', 'words' => '', 'len' => '', 'length' => '', 'sentence' => '', 'html' => '', 'word' => '', 'currency' => '', 'decimals' => '', 'point' => '', 'thousands' => ''); /*--------------------------------------------- * * Pre-process parameters * */ if (isset($parameters['type']) && $parameters['type'] == 'attachment') { if (!isset($parameters['status'])) { $parameters['status'] = 'any'; // Default for attachment } } // Default size for featured image thumbnail $image_fields = array('thumbnail', 'thumbnail-link'); if (isset($parameters['field']) && in_array($parameters['field'], $image_fields)) { $parameters['size'] = isset($parameters['size']) ? $parameters['size'] : 'thumbnail'; } if (!empty($parameters['acf_date'])) { $parameters['field'] = $parameters['acf_date']; } // Merge with defaults $parameters = shortcode_atts($defaults, $parameters); /*--------------------------------------------- * * Post-process parameters * */ // Get page by name if (!empty($parameters['page'])) { $parameters['type'] = 'page'; $parameters['name'] = $parameters['page']; } // Post status if (!empty($parameters['status'])) { $parameters['status'] = CCS_Loop::explode_list($parameters['status']); // multiple values } // ACF page link if (!empty($parameters['link']) && empty($parameters['more'])) { $parameters['field'] = $parameters['link']; $parameters['return'] = 'page-link'; } // Image field if (!empty($parameters['image'])) { $parameters['field'] = $parameters['image']; } // Image size alias if ($parameters['size'] == 'middle') { $parameters['size'] = 'medium'; } // ACF cropped image field if (!empty($parameters['cropped'])) { $parameters['field'] = $parameters['cropped']; } // Checkbox if (!empty($parameters['checkbox'])) { $parameters['field'] = $parameters['checkbox']; } if (!empty($parameters['id'])) { // Manually set post ID } elseif (CCS_Attached::$state['is_attachment_loop']) { $parameters['id'] = CCS_Attached::$state['current_attachment_id']; } elseif (CCS_Related::$state['is_related_posts_loop']) { // Inside [related] $parameters['id'] = CCS_Related::$state['current_related_post_id']; } elseif (class_exists('CCS_To_ACF') && CCS_To_ACF::$state['is_relationship_loop']) { // Inside ACF Relationship field $parameters['id'] = CCS_To_ACF::$state['relationship_id']; } elseif (class_exists('CCS_To_WCK') && CCS_To_WCK::$state['is_wck_post_field']) { // Inside WCK post field $parameters['id'] = CCS_To_WCK::$state['current_wck_post_id']; } // HTML escape if ($parameters['escape'] == 'true' && empty($parameters['shortcode'])) { $parameters['shortcode'] = 'false'; } // Date format: allow escape via "//" because "\" disappears in shortcode parameters if (!empty($parameters['date_format'])) { $parameters['date_format'] = str_replace("//", "\\", $parameters['date_format']); } // Class - support multiple if (!empty($parameters['class'])) { $parameters['class'] = str_replace(',', ' ', $parameters['class']); } // Site option field if (!empty($parameters['site'])) { $parameters['field'] = $parameters['site']; $parameters['option'] = 'site'; } return $parameters; }
function loop_related_posts($atts, $content) { global $post; $outputs = array(); $current_count = 0; if (CCS_Loop::$state['is_loop']) { $post_id = CCS_Loop::$state['current_post_id']; $post_type = do_ccs_shortcode('[field post-type]', false); } elseif (!empty($post)) { $post_id = $post->ID; $post_type = $post->post_type; } else { $post_id = 0; $post_type = 'any'; } extract(shortcode_atts(array('type' => '', 'taxonomy' => 'category', 'field' => '', 'taxonomy_field' => '', 'value' => '', 'subfield' => '', 'count' => '', 'children' => '', 'order' => 'DESC', 'orderby' => 'date', 'relation' => 'or', 'operator' => 'in', 'trim' => ''), $atts)); if (!empty($type)) { $post_type = CCS_Loop::explode_list($type); } if (empty($field) && isset($atts[0])) { $field = $atts[0]; } if (!empty($taxonomy_field)) { $terms = do_ccs_shortcode('[field ' . $taxonomy_field . ']', false); $terms = CCS_Loop::explode_list($terms); if (empty($terms) || count($terms) == 0) { return; } $taxonomies = array(); $term_objects = array(); foreach ($terms as $term) { $term = self::get_term_by_id($term); $tax = $term->taxonomy; if (!in_array($tax, $taxonomies)) { $taxonomies[] = $term->taxonomy; } $term_objects[] = $term; } $taxonomy = implode(',', $taxonomies); $terms = $term_objects; } /*--------------------------------------------- * * ACF relationship field * */ if ((!empty($field) || !empty($subfield)) && empty($value)) { if (class_exists('CCS_To_ACF') && CCS_To_ACF::is_acf_active()) { return CCS_To_ACF::loop_relationship_field($atts, $content); } } /*--------------------------------------------- * * Related posts by taxonomy * */ if (empty($count)) { $count = 99999; } // Hypothetical maximum number of posts if (!empty($taxonomy)) { self::$state['is_related_posts_loop'] = true; // Support multiple taxonomies $taxonomies = CCS_Loop::explode_list($taxonomy); $relation = strtoupper($relation); $tax_count = 0; $query = array('post_type' => $post_type, 'posts_per_page' => -1, 'order' => $order, 'orderby' => $orderby, 'tax_query' => array()); $target_terms = array(); foreach ($taxonomies as $current_taxonomy) { if ($current_taxonomy == 'tag') { $current_taxonomy = 'post_tag'; } // Get current post's taxonomy terms - unless given if (!isset($terms)) { $term_objects = CCS_Content::get_the_terms_silently($post_id, $current_taxonomy); } else { $term_objects = $terms; } if (!is_array($term_objects) || empty($term_objects)) { continue; } foreach ($term_objects as $term) { $target_terms[$current_taxonomy][] = $term->term_id; } if (empty($target_terms[$current_taxonomy])) { continue; } if ($tax_count == 1) { $query['tax_query']['relation'] = $relation; } $query['tax_query'][] = array('taxonomy' => $current_taxonomy, 'field' => 'id', 'terms' => $target_terms[$current_taxonomy], 'operator' => strtoupper($operator), 'include_children' => $children == 'true'); $tax_count++; } $terms = $target_terms; // print_r($query); echo '<br><br>'; $posts = new WP_Query($query); if ($posts->have_posts()) { while ($posts->have_posts()) { // Set up post data $posts->the_post(); // Skip current post if ($post->ID != $post_id) { // Manually filter out terms.. // For some reason, WP_Query is returning more than we need $condition = false; $tax_count = 0; foreach ($taxonomies as $current_taxonomy) { if ($current_taxonomy == 'tag') { $current_taxonomy = 'post_tag'; } // Include child terms if ($children == 'true' && isset($terms[$current_taxonomy])) { foreach ($terms[$current_taxonomy] as $this_term) { $child_terms = get_term_children($this_term, $current_taxonomy); if (!empty($child_terms)) { foreach ($child_terms as $child_term) { if (!in_array($child_term, $terms[$current_taxonomy])) { $terms[$current_taxonomy][] = $child_term; } } } } } if (isset($terms[$current_taxonomy])) { $tax_count++; if ($relation == 'AND') { if (has_term($terms[$current_taxonomy], $current_taxonomy)) { if ($condition || $tax_count == 1) { $condition = true; } } } else { if (has_term($terms[$current_taxonomy], $current_taxonomy)) { $condition = true; } } } } if ($condition) { // OK, post fits the criteria self::$state['current_related_post_id'] = $post->ID; $current_count++; if ($current_count <= $count) { $outputs[] = do_ccs_shortcode($content); } } } } } wp_reset_postdata(); self::$state['is_related_posts_loop'] = false; } $out = implode('', $outputs); if (!empty($trim)) { $out = CCS_Format::trim($out, $trim); } return $out; }
function if_shortcode($atts, $content = null, $shortcode_name) { $atts_original = $atts; $args = array('type' => '', 'name' => '', 'id' => '', 'author' => '', 'comment_author' => '', 'category' => '', 'tag' => '', 'taxonomy' => '', 'term' => '', 'compare' => '', 'tax_archive' => '', 'parent' => '', 'field' => '', 'custom' => '', 'user_field' => '', 'check' => '', 'value' => '', 'before' => '', 'after' => '', 'date_format' => '', 'in' => '', 'field_2' => '', 'today' => '', 'day_of_week' => '', 'lowercase' => '', 'case' => '', 'loose' => '', 'strict' => '', 'sub' => '', 'format' => '', 'contains' => '', 'exact' => '', 'empty' => 'true', 'zero' => '', 'sticky' => '', 'not' => '', 'start' => '', 'end' => '', 'every' => '', 'first' => '', 'last' => '', 'count' => '', 'each' => '', 'each_field' => '', 'each_value' => '', 'decode' => '', 'x' => '', 'query' => '', 'route' => '', 'pass' => '', 'false' => '', 'pass_empty' => 'true', 'flag' => '', 'no_flag' => ''); extract(shortcode_atts($args, $atts, true)); $atts = CCS_Content::get_all_atts($atts); if (isset($atts['and'])) { return self::if_all_conditions($atts, $content, $shortcode_name); } if (count($atts) == 0) { $condition = true; } else { $condition = false; } $out = ''; // Get [else] block $if_else = self::get_if_else($content, $shortcode_name); $content = $if_else['if']; $else = $if_else['else']; if ((!empty($before) || !empty($after)) && empty($field)) { $field = 'date'; // Default for before/after parameter } if (!empty($no_flag)) { $flag = $no_flag; } $compare = strtoupper($compare); if ($strict == 'true') { $loose = 'false'; } // Alias if ($case == 'true') { $lowercase = 'false'; } // Alias /*--------------------------------------------- * * Get global post info * */ $current_post_id = do_shortcode('[field id]'); $post = get_post($current_post_id); /*--------------------------------------------- * * If exists * */ if (isset($atts['exists'])) { unset($atts['exists']); if (empty($atts)) { $result = do_ccs_shortcode($content); } else { // Check if post exists based on given parameters $result = CCS_Loop::the_loop_shortcode($atts_original, 'X'); // Remove params for rest of condition checks $atts = array(); } $result = trim($result); $condition = !empty($result); } /*--------------------------------------------- * * Today * */ if (!empty($day_of_week)) { $today = $day_of_week; $date_format = 'N'; // 1~7, Mon~Sun } if (!empty($today)) { $result = do_ccs_shortcode('[today' . (!empty($date_format) ? ' format="' . $date_format . '"' : '') . ']'); $condition = $result === $today; } /*--------------------------------------------- * * Taxonomy: category, tags, .. * */ if (!empty($category)) { $taxonomy = "category"; $term = $category; } if (!empty($tag)) { $taxonomy = "post_tag"; $term = $tag; } // Check if current post has taxonomy term if (!empty($taxonomy)) { if ($taxonomy == 'tag') { $taxonomy = 'post_tag'; } $taxonomies = wp_get_post_terms($current_post_id, $taxonomy, array()); $post_tax_array = array(); foreach ($taxonomies as $term_object) { if (is_object($term_object)) { $post_tax_array[] = $term_object->slug; } } $terms = self::comma_list_to_array($term); if (empty($term) && count($post_tax_array)) { // If no term query is set, then check if there's any term $condition = true; } else { foreach ($terms as $term) { if (empty($compare) || $compare == 'OR') { $condition = in_array($term, $post_tax_array) ? true : $condition; } else { // AND $condition = in_array($term, $post_tax_array) ? true : false; if (!$condition) { break; } // Every term must be found } } } } /*--------------------------------------------- * * Inside [for/each] loop * */ // Check if current term has children if (CCS_ForEach::$state['is_for_loop']) { if (isset($atts['children'])) { $current_term = CCS_ForEach::$current_term[CCS_ForEach::$index]; $current_taxonomy = $current_term['taxonomy']; $terms = get_terms($current_taxonomy, array('parent' => $current_term['id'])); if (!empty($terms) && $terms != array()) { $condition = true; } else { $condition = false; } } if (!empty($each)) { $v = do_shortcode('[each slug]'); if ($decode == 'true') { $v = urldecode($v); } $eaches = CCS_Loop::explode_list($each); $condition = in_array($v, $eaches); } if (!empty($each_field)) { $v = do_shortcode('[each ' . $each_field . ']'); if ($decode == 'true') { $v = urldecode($v); } if (!empty($each_value)) { $condition = $v == $each_value; } else { $condition = !empty($v); } } if (isset($atts['for-first'])) { $condition = $condition || CCS_ForEach::$state['for_count'] == 1; } } /*--------------------------------------------- * * Field value contains * */ if (!empty($contains)) { if ($exact === 'true') { $value = $contains; } else { $value = implode(',', explode(' ', $contains)); } $contains_compare = !empty($compare) ? strtoupper($compare) : ''; $compare = 'CONTAINS'; if (empty($loose)) { $loose = 'true'; } // Loose search by default $field = CCS_Loop::explode_list($field); if (count($field) > 1) { // Support searching multiple fields foreach ($field as $this_field) { $condition = '[if field=' . $this_field . ' contains="' . $value . '"' . (!empty($contains_compare) ? ' compare=' . $contains_compare : '') . '].[/if]'; $condition = do_shortcode($condition); $condition = !empty($condition); if ($condition) { break; } // If keyword in any of the fields } $field = ''; // Skip default field value condition } else { $field = $field[0]; } } /*--------------------------------------------- * * Field: field="field_slug" value="this,that" * */ if (!empty($field) || !empty($user_field) || !empty($check)) { // Post field if (!empty($field)) { if ($in == 'timestamp') { $date_format = 'U'; if ($value == 'today') { $value = 'today-between'; } } if ($field == 'date') { if (empty($date_format)) { $date_format = 'Ymd'; } // Published date $check = strtotime($post->post_date . ' +0000'); $check = date($date_format, $check); } elseif ($field == 'excerpt') { $check = get_the_excerpt(); $empty = 'true'; $value = ''; // ? } elseif ($custom == 'true') { $check = get_post_meta($current_post_id, $field, $single = true); } else { // Normal field $check = CCS_Content::get_prepared_field($field); // do_shortcode('[field '.$field.']');// ; } // Date field if (!empty($before) || !empty($after)) { if (!empty($field_2)) { $field_2 = CCS_Content::get_prepared_field($field_2); // Convert to imestamp if ($in !== 'timestamp') { $field_2 = strtotime($field_2 . ' +0000'); } $now = intval($field_2); //echo 'FIELD2:'.$field_2.' = '.$now.'<br>'; } else { $now = current_time('timestamp'); } if (!empty($before) && !empty($after)) { $value_before = date($date_format, strtotime($before . ' +0000', $now)); $value_after = date($date_format, strtotime($after . ' +0000', $now)); $value = $value_before . ' - ' . $value_after; $compare = 'BETWEEN'; } elseif (!empty($before)) { $value = date($date_format, strtotime($before . ' +0000', $now)); $compare = empty($compare) ? '<' : $compare; } elseif (!empty($after)) { $value = date($date_format, strtotime($after . ' +0000', $now)); $compare = empty($compare) ? '>' : $compare; //echo 'VALUE: '.$now.' '.$after.' = '.$value.'<br>'; } } elseif (!empty($field_2)) { // Use second field as value $value = CCS_Content::get_prepared_field($field_2); if (!empty($date_format)) { $check = date($date_format, strtotime($check . ' +0000')); $value = date($date_format, strtotime($value . ' +0000')); } } // User field } elseif (!empty($user_field)) { $field = $user_field; $check = CCS_User::get_user_field($field); if (!empty($sub)) { $check = isset($check[$sub]) ? $check[$sub] : ''; } } else { // Just check passed value // $check == $value } // Array if (!empty($sub)) { $check = isset($check[$sub]) ? $check[$sub] : ''; } // start=".." end=".." if (!empty($start) && !empty($end)) { $value = $start . '..' . $end; // Placeholder $start_value = $start; $end_value = $end; $start = 'true'; $end = 'true'; // start=".." } elseif (!empty($start) && $start != 'true' && empty($value)) { $value = $start; $start = 'true'; // end=".." } elseif (!empty($end) && $end != 'true' && empty($value)) { $value = $end; $end = 'true'; } if ($check === '') { // ( empty($check) || ( $check == false ) ) { // @todo What if field value is boolean, i.e., checkbox? $condition = false; } else { if (!is_array($check)) { $check = array($check); } if ($value !== '') { // Allow false, 0 $values = self::comma_list_to_array($value); $date_values = array('today', 'today-between', 'now', 'future', 'past', 'future not today', 'past and today', 'future-time', 'past-time'); foreach ($check as $check_this) { if ($decode == 'true') { $check_this = urldecode($check_this); } foreach ($values as $this_value) { if (in_array($this_value, $date_values)) { if (empty($date_format)) { $date_format = 'Ymd'; } // ACF date field // Support date values $q = array('field' => $check_this, 'value' => $this_value, 'compare' => $compare, 'date_format' => $date_format); $meta_query = CCS_Loop::prepare_meta_query($q); $check_this = $meta_query['key']; $this_value = $meta_query['value']; $compare = $meta_query['compare']; //debug_array($q); if (is_array($this_value)) { $this_value = implode(',', $this_value); } } //echo 'Check field: '.$field.' '.$check_this.' '.$compare.' '.$this_value.'<br>'; if ($start == 'true' && $end == 'true') { // Check beginning and end of field value if (substr($check_this, 0, strlen($start_value)) == $start_value && substr($check_this, strlen($check_this) - strlen($end_value)) == $end_value) { $condition = true; continue; } else { $condition = false; break; } } elseif ($start == 'true') { // Only check beginning of field value $check_this = substr($check_this, 0, strlen($this_value)); } elseif ($end == 'true') { // Only check end of field value $check_this = substr($check_this, strlen($check_this) - strlen($this_value)); } if ($loose == 'true') { $check_this = CCS_Format::normalize_alphabet($check_this); $this_value = CCS_Format::normalize_alphabet($this_value); } if ($loose == 'true' || $lowercase == 'true') { $check_this = strtolower($check_this); $this_value = strtolower($this_value); } if ($compare == 'AND') { $condition = $this_value == $check_this ? true : false; if (!$condition) { break; } // Every term must be found } elseif ($compare == 'CONTAINS') { $condition = strpos($check_this, $this_value) !== false; if ($contains_compare != 'OR' && !$condition) { break; // Every term must be found } } else { switch ($compare) { case 'MORE': case 'NEW': case 'NEWER': case '>': $condition = $check_this > $this_value ? true : $condition; break; case '>=': $condition = $check_this >= $this_value ? true : $condition; break; case 'LESS': case 'OLD': case 'OLDER': case '<': $condition = $check_this < $this_value ? true : $condition; break; case '<=': $condition = $check_this <= $this_value ? true : $condition; break; case 'BETWEEN': $values = explode(' - ', $this_value); // Hmm..to avoid conflict with ',' if (isset($values[0]) && isset($values[1])) { $condition = $values[0] <= $check_this && $check_this <= $values[1] ? true : $condition; } break; case 'EQUAL': case '=': default: $condition = $check_this == $this_value ? true : $condition; break; } } // End compare } // End for each value } // End for each check } else { // No value specified - just check that there is field value if ($empty == 'true') { if (is_array($check)) { $check = implode('', $check); } // catches ACF repeater $condition = !empty($check) ? true : false; } else { $condition = false; } } } // End if check not empty } // End field value condition /*--------------------------------------------- * * Post type, name, id * */ if (!empty($type)) { $types = self::comma_list_to_array($type); // Enable comma-separated list $current_post_type = isset($post->post_type) ? $post->post_type : null; $condition = in_array($current_post_type, $types) ? true : false; } if (!empty($id)) { $ids = self::comma_list_to_array($id); // Enable comma-separated list if (($find_key = array_search('this', $ids)) !== false) { $depth = CCS_Content::$state['depth']; if (isset(CCS_Content::$state['current_post_id'][$depth - 1])) { $ids[$find_key] = CCS_Content::$state['current_post_id'][$depth - 1]; } elseif (CCS_Loop::$state['is_loop']) { $ids[$find_key] = CCS_Loop::$state['original_post_id']; } else { $ids[$find_key] = get_the_ID(); } } $condition = in_array($current_post_id, $ids) ? true : false; } if (!empty($name)) { $names = self::comma_list_to_array($name); $current_post_name = isset($post->post_name) ? $post->post_name : null; foreach ($names as $each_name) { if ($start == 'true') { // Only check beginning of string $this_value = substr($current_post_name, 0, strlen($each_name)); } else { $this_value = $current_post_name; } $condition = $this_value == $each_name ? true : $condition; } } /*--------------------------------------------- * * Post author * */ if (!empty($author)) { $authors = CCS_Loop::explode_list($author); $author_ids = array(); foreach ($authors as $this_author) { if ($this_author == 'this') { // current author ID $author_ids[] = do_shortcode('[user id]'); } elseif (is_numeric($this_author)) { $author_ids[] = $this_author; } else { // get author ID from user name $author_ids[] = do_shortcode('[users search=' . $this_author . ' search_column=login][user id][/users]'); } } if (CCS_Comments::$state['is_comments_loop']) { $post_id = do_shortcode('[comment post-id]'); } else { $post_id = do_shortcode('[field id]'); } $pass = do_shortcode('[field author-id id=' . $post_id . ']'); if (empty($pass)) { $condition = false; } else { $value = implode(',', $author_ids); } } if (!empty($comment_author)) { if (CCS_Comments::$state['is_comments_loop']) { $authors = CCS_Loop::explode_list($comment_author); $author_ids = array(); foreach ($authors as $this_author) { if ($this_author == 'this') { // current author ID $author_ids[] = do_shortcode('[user id]'); } elseif ($this_author == 'same') { // Same author as current post if ($current_post) { $author_ids[] = do_shortcode('[field author-id]'); } } elseif (is_numeric($this_author)) { $author_ids[] = $this_author; } else { // get author ID from user name $author_ids[] = do_shortcode('[users search=' . $this_author . ' search_column=login][user id][/users]'); } } $check_author = do_shortcode('[comment author-id]'); $condition = in_array($check_author, $author_ids); } else { $this_check = do_shortcode('[comments user='******' count=1].[/comments]'); $condition = !empty($this_check); } } /*--------------------------------------------- * * Post parent * */ if (!empty($parent)) { $current_post_parent = isset($post->post_parent) ? $post->post_parent : 0; if ($current_post_parent == 0) { // Current post has no parent $condition = false; } else { $current_post_parent_slug = self::slug_from_id($current_post_parent); $parents = self::comma_list_to_array($parent); foreach ($parents as $check_parent) { if (is_numeric($check_parent)) { // compare to parent id if (empty($compare) || $compare == 'OR') { $condition = $check_parent == $current_post_parent ? true : $condition; } else { // AND $condition = $check_parent == $current_post_parent ? true : false; if (!$condition) { break; } // Every term must be found } } else { // compare to parent slug if ($start == 'true') { // Only check beginning of string $check_this = substr($current_post_parent_slug, 0, strlen($check_parent)); } else { $check_this = $current_post_parent_slug; } if (empty($compare) || $compare == 'OR') { $condition = $check_parent == $check_this ? true : $condition; } else { // AND $condition = $check_parent == $check_this ? true : false; if (!$condition) { break; } // Every term must be found } } } } } /*--------------------------------------------- * * Attachments * */ if (isset($atts['attached'])) { // Does the current post have any attachments? $current_id = get_the_ID(); $posts = get_posts(array('post_parent' => $current_id, 'post_type' => 'attachment', 'post_status' => 'any', 'posts_per_page' => 1)); if (!empty($posts)) { $condition = true; } else { $condition = false; } } /*--------------------------------------------- * * If child post exists * */ if (isset($atts['children']) && !CCS_ForEach::$state['is_for_loop'] && !CCS_Menu::$state['is_menu_loop']) { if (!empty($post)) { $children_array = get_children(array('post_parent' => $post->ID, 'posts_per_page' => '1', 'post_status' => 'publish')); $condition = count($children_array) > 0; } } /*--------------------------------------------- * * [x] loop index * */ if (!empty($x)) { $condition = $x == CCS_Format::$state['x_loop']; } /*--------------------------------------------- * * Sticky post * */ if (isset($atts['sticky'])) { $sticky = 'true'; } if (!empty($sticky)) { $is_sticky = is_sticky(); $condition = $is_sticky && $sticky == 'true' || !$is_sticky && $sticky == 'false'; } /*--------------------------------------------- * * Post format * */ if (!empty($format) && function_exists('has_post_format')) { $formats = CCS_Loop::explode_list($format); foreach ($formats as $this_format) { $this_format = strtolower($this_format); if (has_post_format($this_format, $current_post_id)) { $condition = true; break; } } } elseif (isset($atts['format'])) { // Check if it exists $this_format = get_post_format($current_post_id); if (!empty($this_format)) { $condition = true; } } /*--------------------------------------------- * * Has CCS gallery field * */ if (isset($atts['gallery']) && class_exists('CCS_Gallery_Field')) { $condition = CCS_Gallery_Field::has_gallery(); } /*--------------------------------------------- * * Template: home, archive, single.. * [if comment] - current post has comment * */ $condition = isset($atts['home']) ? is_front_page() : $condition; $condition = isset($atts['comment']) ? get_comments_number($current_post_id) > 0 : $condition; $condition = isset($atts['image']) ? has_post_thumbnail() : $condition; $condition = isset($atts['loop']) ? CCS_Loop::$state['is_loop'] : $condition; $condition = isset($atts['archive']) ? is_archive() : $condition; $condition = isset($atts['single']) ? is_single() : $condition; $condition = isset($atts['search']) ? is_search() : $condition; $condition = isset($atts['404']) ? is_404() : $condition; $condition = isset($atts['none']) ? !have_posts() : $condition; if (isset($atts['tax_archive'])) { if ($tax_archive == 'true') { $tax_archive = ''; } $condition = is_tax($tax_archive); } if (isset($atts['avatar'])) { $check = do_shortcode('[field avatar]'); $condition = !empty($check); } /*--------------------------------------------- * * Inside [loop] * */ if (CCS_Loop::$state['is_loop']) { /*--------------------------------------------- * * Every X number of posts * */ if (!empty($every)) { $count = CCS_Loop::$state['loop_count']; if (substr($every, 0, 4) == 'not ') { $every = substr($every, 4); // Remove first 4 letters // not Modulo $condition = $every == 0 ? false : $count % $every != 0; } else { // Modulo $condition = $every == 0 ? false : $count % $every == 0; } if ($first == 'true') { $condition = $condition || CCS_Loop::$state['loop_count'] == 1; //unset($atts['first']); } if ($last == 'true') { $condition = $condition || CCS_Loop::$state['loop_count'] == CCS_Loop::$state['post_count']; //unset($atts['last']); } } elseif (!empty($count)) { if ($compare == '>=') { $condition = CCS_Loop::$state['loop_count'] >= $count; } elseif ($compare == '<=') { $condition = CCS_Loop::$state['loop_count'] <= $count; } elseif ($compare == '>' || $compare == 'MORE') { $condition = CCS_Loop::$state['loop_count'] > $count; } elseif ($compare == '<' || $compare == 'LESS') { $condition = CCS_Loop::$state['loop_count'] < $count; } else { $condition = CCS_Loop::$state['loop_count'] == $count; } } /*--------------------------------------------- * * First and last post in loop * */ $condition = isset($atts['first']) ? CCS_Loop::$state['loop_count'] == 1 : $condition; $condition = isset($atts['last']) ? CCS_Loop::$state['loop_count'] == CCS_Loop::$state['post_count'] : $condition; } // End: if inside [loop] /*--------------------------------------------- * * Menu loop * */ if (CCS_Menu::$state['is_menu_loop']) { $condition = isset($atts['first']) ? CCS_Menu::$state['menu_index'][CCS_Menu::$state['depth']] == 1 : $condition; $condition = isset($atts['last']) ? CCS_Menu::$state['menu_index'][CCS_Menu::$state['depth']] == CCS_Menu::$state['total_menu_count'][CCS_Menu::$state['depth']] : $condition; if (isset($atts['children'])) { $children = do_shortcode('[loop menu=children].[/loop]'); if (!empty($children)) { $condition = true; } else { $condition = false; } } if (!empty($every)) { $count = CCS_Menu::$state['menu_index'][CCS_Menu::$state['depth']]; // Modulo $condition = $every == 0 ? false : $count % $every == 0; } } /*--------------------------------------------- * * ACF repeater/flex/gallery * */ if (class_exists('CCS_To_ACF')) { if (CCS_To_ACF::$state['is_repeater_or_flex_loop']) { if (!empty($every)) { $condition = CCS_To_ACF::$state['repeater_index'] % $every == 0; } if (isset($atts['first'])) { $condition = CCS_To_ACF::$state['repeater_index'] == 1; } } if (CCS_To_ACF::$state['is_gallery_loop']) { if (!empty($every)) { $condition = CCS_To_ACF::$state['gallery_index'] % $every == 0; } if (isset($atts['first'])) { $condition = CCS_To_ACF::$state['gallery_index'] == 1; } } } /*--------------------------------------------- * * Inside array field * */ if (CCS_Content::$state['is_array_field']) { if (isset($atts['first'])) { $condition = CCS_Content::$state['array_field_index'] == 1; } if (isset($atts['last'])) { $condition = CCS_Content::$state['array_field_index'] == CCS_Content::$state['array_field_count']; } } /*--------------------------------------------- * * Inside comments loop * */ if (CCS_Comments::$state['is_comments_loop']) { if (isset($atts['first'])) { $condition = CCS_Comments::$state['comments_loop_index'] == 1; } if (isset($atts['last'])) { $condition = CCS_Comments::$state['comments_loop_index'] == CCS_Comments::$state['comments_loop_count']; } } /*--------------------------------------------- * * Passed value * */ if (isset($atts['pass']) && empty($atts['pass']) && $empty != 'true' || $pass_empty != 'true' && empty($pass)) { // pass="******" empty="false" -- pass is empty $condition = false; } elseif (!empty($pass) && empty($value) && $empty != 'true') { // pass="******" empty="false" -- no value set $condition = true; } elseif (!empty($pass) && !empty($value)) { // pass="******" value="something" $values = CCS_Loop::explode_list($value); // Support multiple values $condition = in_array($pass, $values); } /*--------------------------------------------- * * Query * */ $queries = CCS_URL::get_queries(); if (!empty($query)) { $query_check = isset($queries[$query]) ? $queries[$query] : ''; if (!empty($value)) { $condition = $query_check == $value; } else { $condition = !empty($query_check); } } /*--------------------------------------------- * * Route * */ $routes = CCS_URL::get_routes(); // Whole route if (!empty($route)) { $result = implode('/', $routes); $condition = $result == $route; } // Route parts: route_1, route_2, ... for ($i = 0; $i < count($routes); $i++) { if (isset($atts['route_' . ($i + 1)])) { $condition = $atts['route_' . ($i + 1)] == $routes[$i]; if (!$condition) { break; } } elseif (isset($atts[0]) && $atts[0] == 'route_' . ($i + 1)) { // if it exists $condition = !empty($routes[$i]); if (!$condition) { break; } } } /*--------------------------------------------- * * Not / else * */ // Not - also catches compare="not" $condition = isset($atts['not']) ? !$condition : $condition; self::$state['is_if_block'] = true; $out = $condition ? do_ccs_shortcode($content) : do_ccs_shortcode($else); // [if]..[else]..[/if] self::$state['is_if_block'] = false; return $out; }
function users_shortcode($atts, $content) { self::$state['user_query'] = ''; /*--------------------------------------------- * * [if empty] * */ // If empty $middle = CCS_Loop::get_between('[if empty]', '[/if]', $content); $content = str_replace($middle, '', $content); $else = CCS_Loop::extract_else($middle); self::$state['if_empty'] = $middle; self::$state['if_empty_else'] = $else; $outputs = array(); /*--------------------------------------------- * * Prepare parameters * */ $args = array(); // Just pass these $pass_args = array('orderby', 'search', 'number', 'offset', 'meta_key'); // Order by field value $sort_field_num = false; if (isset($atts['orderby'])) { if ($atts['orderby'] == 'id') { $atts['orderby'] = 'ID'; } elseif (isset($atts['field']) && ($atts['orderby'] == 'field' || $atts['orderby'] == 'field_num')) { if ($atts['orderby'] == 'field') { $atts['orderby'] = 'meta_value'; $atts['meta_key'] = $atts['field']; } else { // Sort by field value number $sort_field_num = $atts['field']; unset($atts['orderby']); } unset($atts['field']); } } foreach ($pass_args as $arg) { if (isset($atts[$arg])) { $args[$arg] = $atts[$arg]; } } if (isset($atts['role'])) { if ($atts['role'] == 'admin') { $atts['role'] = 'Administrator'; } $args['role'] = ucwords($atts['role']); // Capitalize word } if (isset($atts['count'])) { $args['number'] = $atts['count']; } if (isset($atts['order'])) { $args['order'] = strtoupper($atts['order']); } if (isset($atts['include'])) { $args['include'] = CCS_Loop::explode_list($atts['include']); } if (isset($atts['exclude'])) { $args['exclude'] = CCS_Loop::explode_list($atts['exclude']); } if (isset($atts['blog_id'])) { $args['blog_id'] = intval($atts['blog_id']); } if (isset($atts['search_columns'])) { $args['search_columns'] = CCS_Loop::explode_list($atts['search_columns']); } if (isset($atts['field']) && isset($atts['value'])) { $compare = isset($atts['compare']) ? strtoupper($atts['compare']) : '='; switch ($compare) { case 'EQUAL': $compare = '='; break; case 'NOT': case 'NOT EQUAL': $compare = '!='; break; case 'MORE': $compare = '>'; break; case 'LESS': $compare = '<'; break; } $multiple = array('IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'); if (in_array($compare, $multiple)) { $value = CCS_Loop::explode_list($atts['value']); } else { $value = $atts['value']; } $args['meta_query'][] = array('key' => $atts['field'], 'value' => $atts['value'], 'compare' => $compare); // Additional query if (isset($atts['relation']) && isset($atts['field_2']) && isset($atts['value_2'])) { $args['meta_query']['relation'] = strtoupper($atts['relation']); $args['meta_query'][] = array('key' => $atts['field_2'], 'value' => $atts['value_2'], 'compare' => isset($atts['compare_2']) ? strtoupper($atts['compare_2']) : '='); } } // Alter query to extend search function if (isset($args['search'])) { self::$state['user_query'] = $args['search']; add_action('pre_user_query', array(__CLASS__, 'extend_search')); } // Main action if (isset($atts['id'])) { $users = array(get_user_by('id', $atts['id'])); } elseif (isset($atts['slug'])) { $users = array(get_user_by('slug', $atts['slug'])); } else { $users = get_users($args); } if (isset($args['search'])) { self::$state['user_query'] = ''; remove_action('pre_user_query', array(__CLASS__, 'extend_search')); } /*--------------------------------------------- * * Filter results * */ // Sort by field value number if ($sort_field_num !== false) { // This is necessary because get_users doesn't do meta_value_num query $new_users = array(); foreach ($users as $user) { $key = $user->get($sort_field_num); $new_users[] = array('user' => $user, 'key' => $key); } usort($new_users, array(__CLASS__, 'sortByFieldNum')); if (isset($args['order']) && $args['order'] == 'DESC') { $new_users = array_reverse($new_users); } $users = array(); foreach ($new_users as $user_array) { $users[] = $user_array['user']; } } self::$state['is_users_loop'] = true; // Users Loop foreach ($users as $user) { self::$state['current_user_object'] = $user; // Support tags $content = str_replace('{USER_ID}', do_shortcode('[user id]'), $content); $content = str_replace('{USER_ROLE}', do_shortcode('[user role out="slug"]'), $content); $outputs[] = do_ccs_shortcode($content); } // [if empty]..[else]..[/if] if (count($users) == 0 && isset(self::$state['if_empty'])) { $outputs[] = do_ccs_shortcode(self::$state['if_empty']); } elseif (isset(self::$state['if_empty_else']) && count($users) > 0) { $outputs[] = do_ccs_shortcode(self::$state['if_empty_else']); } $result = implode('', $outputs); if (isset($atts['trim'])) { $trim = $atts['trim']; if ($trim == 'true') { $trim = null; } $result = trim($result, " \t\n\r\v," . $trim); } self::$state['is_users_loop'] = false; return $result; }
public static function if_get_row_layout($atts, $content) { extract(shortcode_atts(array('name' => ''), $atts)); if (empty($name) && isset($atts[0])) { $name = $atts[0]; } $names = CCS_Loop::explode_list($name); $layout = get_row_layout(); if (in_array($layout, $names)) { return do_ccs_shortcode($content); } else { return null; } }
function for_shortcode($atts, $content = null, $shortcode_name) { $args = array('each' => '', 'term' => '', 'terms' => '', 'orderby' => '', 'order' => '', 'count' => '', 'parent' => '', 'parents' => '', 'children' => '', 'current' => '', 'trim' => '', 'empty' => 'true', 'exclude' => ''); extract(shortcode_atts($args, $atts, true)); // Top parent loop if (!self::$state['is_for_loop']) { self::$state['is_for_loop'] = true; // Nested loop } else { $parent_term = self::$current_term[self::$index]; // Same taxonomy as parent if ($each == 'child' && isset($parent_term['taxonomy'])) { $each = $parent_term['taxonomy']; } // Get parent term unless specified if (empty($parent) && isset($parent_term['id'])) { $parent = $parent_term['id']; } // Nest index self::$index++; } if ($each == 'tag') { $each = 'post_tag'; } $out = ''; $prefix = CCS_Format::get_minus_prefix($shortcode_name); // Get [else] block $if_else = CCS_If::get_if_else($content, $shortcode_name, 'for-else'); $content = $if_else['if']; $else = $if_else['else']; // Get terms according to parameters // @todo Refactor - keep it DRY // @todo Consolidate with CCS_Content::get_taxonomies $query = array('orderby' => !empty($orderby) ? $orderby : 'name', 'order' => $order, 'number' => $count, 'parent' => $parents == 'true' ? 0 : '', 'hide_empty' => $empty == 'true' ? 0 : 1); $term_ids = array(); if (!empty($terms)) { $term = $terms; } // Alias if (!empty($term)) { $terms = CCS_Loop::explode_list($term); // Multiple values support foreach ($terms as $this_term) { if (is_numeric($this_term)) { $term_ids[] = $this_term; } else { /* Get term ID from slug */ $term_id = get_term_by('slug', $this_term, $each); if (!empty($term_id)) { $term_ids[] = $term_id->term_id; } } } if (!empty($term_ids)) { $query['include'] = $term_ids; } else { // Nothing found // Return to parent loop if (self::$index > 0) { self::$index--; } else { self::$state['is_for_loop'] = false; } return do_ccs_shortcode($else); } } // Inside loop, or current is true if (CCS_Loop::$state['is_loop'] && $current != "false" || $current == "true") { if ($current == "true") { $post_id = get_the_ID(); } else { $post_id = CCS_Loop::$state['current_post_id']; } // Inside [loop] $taxonomies = wp_get_post_terms($post_id, $each, $query); // Current and parent parameters together if (!empty($parent)) { if (is_numeric($parent)) { $parent_term_id = $parent; } else { // Get parent term ID from slug $term = get_term_by('slug', $parent, $each); if (!empty($term)) { $parent_term_id = $term->term_id; } else { $parent_term_id = null; } } if (!empty($parent_term_id)) { // Filter out terms that do not have the specified parent // TODO: Why not set this as query for wp_get_post_terms above..? foreach ($taxonomies as $key => $term) { // TODO: What about children parameter for all descendants..? if ($term->parent != $parent_term_id) { unset($taxonomies[$key]); } } } } // Not inside loop } else { if (empty($parent)) { $taxonomies = get_terms($each, $query); if (!empty($term) && $children == 'true') { if (isset($query['include'])) { unset($query['include']); } // Get descendants of each term $new_taxonomies = $taxonomies; foreach ($taxonomies as $term_object) { $query['child_of'] = $term_object->term_id; $new_terms = get_terms($each, $query); if (!empty($new_terms)) { $new_taxonomies += $new_terms; foreach ($new_terms as $new_term) { $term_ids[] = $new_term->term_id; } } } $taxonomies = $new_taxonomies; } // Get terms by parent } else { if (is_numeric($parent)) { $parent_term_id = $parent; } else { // Get parent term ID from slug $term = get_term_by('slug', $parent, $each); if (!empty($term)) { $parent_term_id = $term->term_id; } else { $parent_term_id = null; } } if (!empty($parent_term_id)) { /* Get direct children */ if ($children !== 'true') { // Direct children only $query['parent'] = $parent_term_id; } else { // All descendants $query['child_of'] = $parent_term_id; } $taxonomies = get_terms($each, $query); } else { $taxonomies = null; } // No parent found } } if (count($term_ids) > 0) { $new_taxonomies = array(); // Sort terms according to given ID order: get_terms doesn't do order by ID foreach ($term_ids as $term_id) { foreach ($taxonomies as $term_object) { if ($term_object->term_id == $term_id) { $new_taxonomies[] = $term_object; } } } $taxonomies = $new_taxonomies; } // Array and not empty if (is_array($taxonomies) && count($taxonomies) > 0) { $each_term = array(); $each_term['taxonomy'] = $each; // Taxonomy name $excludes = CCS_Loop::explode_list($exclude); $index = 0; if (empty($count)) { $count = 9999; } // Show all foreach ($taxonomies as $term_object) { // Exclude IDs or slugs $condition = true; foreach ($excludes as $exclude) { if (is_numeric($exclude)) { // Exclude ID if ($exclude == $term_object->term_id) { $condition = false; } } else { // Exclude slug if ($exclude == $term_object->slug) { $condition = false; } } } if ($condition && ++$index <= $count) { $each_term['id'] = $term_object->term_id; $each_term['name'] = $term_object->name; $each_term['slug'] = $term_object->slug; $each_term['description'] = $term_object->description; $term_link = get_term_link($term_object); if (is_wp_error($term_link)) { $term_link = null; } $each_term['url'] = $term_link; $each_term['link'] = '<a href="' . $each_term['url'] . '">' . $each_term['name'] . '</a>'; // Alias for backward compatibility $each_term['name-link'] = $each_term['link']; // Replace {TAGS} // @todo Use a general-purpose function in CCS_Loop for replacing tags $replaced_content = str_replace('{' . $prefix . 'TERM}', $each_term['slug'], $content); $replaced_content = str_replace('{' . $prefix . 'TERM_ID}', $each_term['id'], $replaced_content); $replaced_content = str_replace('{' . $prefix . 'TERM_NAME}', $each_term['name'], $replaced_content); // Make term data available to [each] self::$current_term[self::$index] = $each_term; self::$state['for_count']++; $out .= do_ccs_shortcode($replaced_content); } } // For each term } else { // No taxonomy found $out .= do_ccs_shortcode($else); } // Trim final output if (!empty($trim)) { if ($trim == 'true') { $trim = null; } $out = trim($out, " \t\n\r\v," . $trim); } // Return to parent loop if (self::$index > 0) { self::$index--; } else { self::$state['is_for_loop'] = false; } self::$state['for_count'] = 0; return $out; }
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); }
function comment_form_shortcode($atts, $content) { extract(shortcode_atts(array('fields' => 'author, email, url'), $atts)); $enabled_fields = CCS_Loop::explode_list($fields); self::$state['comment_form_fields'] = array(); $content = do_local_shortcode('comment-form', $content, true); // Prepare arguments $commenter = wp_get_current_commenter(); $req = get_option('require_name_email'); $aria_req = $req ? " aria-required='true'" : ''; /*--------------------------------------------- * * Inputs * */ $options = array(); $rendered_fields = array(); $defined_inputs = self::$state['comment_form_fields']; foreach (self::$state['inputs'] as $each_input) { $do_define = isset($defined_inputs[$each_input]); // Input fields if (in_array($each_input, array('author', 'email', 'url')) && in_array($each_input, $enabled_fields)) { if ($do_define) { $rendered_fields[$each_input] = $defined_inputs[$each_input]; } elseif ($each_input == 'author') { $rendered_fields[$each_input] = '<p class="comment-form-author"><label for="author">' . 'Name' . '</label> ' . ($req ? '<span class="required">*</span>' : '') . '<input id="author" name="author" type="text" value="' . esc_attr($commenter['comment_author']) . '" size="30"' . $aria_req . ' /></p>'; } elseif ($each_input == 'email') { $rendered_fields[$each_input] = '<p class="comment-form-email"><label for="email">' . __('Email', 'domainreference') . '</label> ' . ($req ? '<span class="required">*</span>' : '') . '<input id="email" name="email" type="text" value="' . esc_attr($commenter['comment_author_email']) . '" size="30"' . $aria_req . ' /></p>'; } elseif ($each_input == 'url') { $rendered_fields[$each_input] = '<p class="comment-form-url"><label for="url">' . __('Website', 'domainreference') . '</label>' . '<input id="url" name="url" type="text" value="' . esc_attr($commenter['comment_author_url']) . '" size="30" /></p>'; } // Comment form options } elseif (in_array($each_input, array('comment', 'reply', 'reply_to', 'cancel', 'submit', 'login', 'logged-in', 'before', 'after'))) { if ($each_input == 'login') { if ($do_define) { $template = $defined_inputs[$each_input]; } else { $template = '<p class="must-log-in">' . 'You must be <a href="%s">logged in</a> to post a comment.' . '</p>'; } $options[$each_input] = sprintf($template, wp_login_url(apply_filters('the_permalink', get_permalink()))); } elseif ($each_input == 'logged-in') { if ($do_define) { $template = $defined_inputs[$each_input]; } else { $template = '<p class="logged-in-as">' . 'Logged in as <a href="%1$s">%2$s</a>. ' . '<a href="%3$s" title="Log out of this account">Log out?</a>' . '</p>'; } $user_identity = do_shortcode('[user]'); if (!empty($user_identity)) { $options[$each_input] = sprintf($template, admin_url('profile.php'), $user_identity, wp_logout_url(apply_filters('the_permalink', get_permalink()))); } else { $options[$each_input] = ''; } } elseif ($do_define) { // Option is set by a local shortcode $options[$each_input] = $defined_inputs[$each_input]; } else { if ($each_input == 'comment') { // Default comment textarea $options[$each_input] = '<p><textarea placeholder="" id="comment" class="form-control" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>'; } elseif ($each_input == 'reply') { $options[$each_input] = ''; // Leave a Reply } elseif ($each_input == 'reply_to') { $options[$each_input] = ''; // Leave a Reply to %s } elseif ($each_input == 'cancel') { $options[$each_input] = 'Cancel Reply'; } elseif ($each_input == 'submit') { $options[$each_input] = 'Post Comment'; } elseif ($each_input == 'before') { $options[$each_input] = ''; } elseif ($each_input == 'after') { $options[$each_input] = ''; } } } // End comment form options } // For each field $args = array('fields' => apply_filters('comment_form_default_fields', $rendered_fields), 'id_form' => 'commentform', 'id_submit' => 'commentsubmit', 'title_reply' => $options['reply'], 'title_reply_to' => $options['reply_to'], 'cancel_reply_link' => $options['cancel'], 'label_submit' => $options['submit'], 'comment_field' => $options['comment'], 'must_log_in' => $options['login'], 'logged_in_as' => $options['logged-in'], 'comment_notes_before' => $options['before'], 'comment_notes_after' => $options['after']); // Return comment form ob_start(); comment_form($args); return ob_get_clean(); }