コード例 #1
0
ファイル: user.php プロジェクト: supahseppe/path-of-gaming
 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['count'])) {
         $args['number'] = $atts['count'];
     }
     if (isset($atts['order'])) {
         $args['order'] = strtoupper($atts['order']);
     }
     if (isset($atts['include'])) {
         $args['include'] = CCS_Format::explode_list($atts['include']);
     }
     if (isset($atts['exclude'])) {
         $args['exclude'] = CCS_Format::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_Format::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_Format::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']));
     } elseif (isset($atts['role'])) {
         // Support query by multiple roles, because get_users() doesn't
         $users = array();
         $roles = CCS_Format::explode_list($atts['role']);
         foreach ($roles as $role) {
             if ($role == 'admin') {
                 $role = 'Administrator';
             } else {
                 $role = ucwords($role);
             }
             // Capitalize word
             $args['role'] = $role;
             $matching_users = get_users($args);
             $users = array_merge($users, $matching_users);
         }
     } 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']);
     }
     // Make a list
     // TODO: Add new function: CCS_Format::make_list()
     if (isset($atts['list'])) {
         $outerTag = $atts['list'] == 'true' ? 'ul' : $atts['list'];
         $innerTag = isset($atts['item']) ? $atts['item'] : 'li';
         $result = '<' . $outerTag . (isset($atts['list_class']) ? ' class="' . implode(' ', array_map('trim', explode(',', $atts['list_class']))) . '"' : '') . (isset($atts['list_style']) ? ' style="' . $atts['list_style'] . '"' : '') . '>';
         foreach ($outputs as $o) {
             $result .= '<' . $innerTag . (isset($atts['item_class']) ? ' class="' . implode(' ', array_map('trim', explode(',', $atts['item_class']))) . '"' : '') . (isset($atts['item_style']) ? ' style="' . $atts['item_style'] . '"' : '') . '>' . $o . '</' . $outerTag . '>';
         }
         $result .= '</' . $outerTag . '>';
     } 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;
 }
コード例 #2
0
 function comments_shortcode($atts, $content)
 {
     if (empty($content)) {
         return;
     }
     extract(shortcode_atts(array('type' => '', 'count' => 'all', 'status' => '', 'id' => '', 'include' => '', 'exclude' => '', 'format' => '', 'words' => '', 'length' => '', 'category' => '', 'tag' => '', 'taxonomy' => '', 'term' => '', 'term_id' => '', 'offset' => '', 'orderby' => '', 'order' => 'DESC', 'parent' => '', 'author' => '', 'name' => '', 'status' => 'approve', 'user_id' => '', 'user' => ''), $atts));
     // Prepare comments query
     $args = array();
     // Comments count
     // Get all and filter later
     // $args['number'] = $count=='all' ? 9999 : $count;
     $args['number'] = 9999;
     // By post ID
     // If inside [loop] then target current post
     if (empty($id) && CCS_Loop::$state['is_loop']) {
         $id = 'this';
     }
     if (!empty($id)) {
         if ($id == 'this') {
             $id = get_the_ID();
             if (empty($id)) {
                 return;
             }
             // No current post ID
         }
         $args['post_id'] = $id;
     }
     // By post type
     if (!empty($type)) {
         $args['post_type'] = CCS_Format::explode_list($type);
     }
     if (!empty($include)) {
         $args['post__in'] = CCS_Format::explode_list($include);
     }
     if (!empty($exclude)) {
         $args['post__not_in'] = CCS_Format::explode_list($exclude);
     }
     if (!empty($offset)) {
         $args['offset'] = $offset;
     }
     if (!empty($orderby)) {
         $args['orderby'] = $orderby;
     }
     if (!empty($order)) {
         $args['order'] = $order;
     }
     if (!empty($parent)) {
         $args['post_parent'] = CCS_Format::explode_list($parent);
     }
     // Comments by post author or comment author (user)
     if (!empty($author) || !empty($exclude_author) || !empty($user) || !empty($exclude_user)) {
         if (!empty($author) || !empty($exclude_author)) {
             $authors = CCS_Format::explode_list($author);
         } else {
             $authors = CCS_Format::explode_list($user);
         }
         $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 (!empty($author)) {
             $args['post_author__in'] = $author_ids;
         } elseif (!empty($user)) {
             $args['author__in'] = $author_ids;
         } elseif (!empty($exclude_author)) {
             $args['post_author__not_in'] = $author_ids;
         } elseif (!empty($exclude_user)) {
             $args['author__not_in'] = $author_ids;
         }
     }
     if (!empty($name)) {
         $args['name'] = $name;
     }
     if (!empty($status) && $status != 'all') {
         $args['status'] = $status;
     }
     if (!empty($user_id)) {
         $args['user_id'] = CCS_Format::explode_list($user_id);
     }
     // Filter by taxonomy
     $taxonomy_filter = false;
     if (!empty($category)) {
         $taxonomy = 'category';
         $term = $category;
     } elseif (!empty($tag)) {
         $taxonomy = 'tag';
         $term = $tag;
     }
     if (!empty($taxonomy)) {
         $taxonomy_filter = true;
         $terms = array();
         $term_ids = array();
         if (!empty($term)) {
             $terms = CCS_Format::explode_list($term);
         }
         if (!empty($term_id)) {
             $term_ids = CCS_Format::explode_list($term_id);
         }
     }
     // If empty
     $if_empty = CCS_Loop::get_between('[if empty]', '[/if]', $content);
     $content = str_replace($if_empty, '', $content);
     /*---------------------------------------------
      *
      * Init loop
      *
      */
     $index = 0;
     $max = $count == 'all' ? 9999 : $count;
     $out = '';
     $prev_state = self::$state;
     if (isset($atts['debug']) && $atts['debug'] == 'true') {
         ob_start();
         echo 'Comment query: <pre>';
         print_r($args);
         echo '</pre>';
         $out = ob_get_clean();
     }
     // Get comments
     $comments = get_comments($args);
     // Loop through each comment
     self::$state['comments_loop_index'] = 0;
     self::$state['comments_loop_count'] = count($comments);
     foreach ($comments as $comment) {
         if ($index >= $max) {
             break;
         }
         self::$state['comments_loop_index']++;
         // Starts with 1
         $matches = true;
         if ($taxonomy_filter) {
             $matches = false;
             $pid = $comment->comment_post_ID;
             if (!empty($terms)) {
                 $post_tax = do_shortcode('[taxonomy ' . $taxonomy . ' id="' . $pid . '" field=slug]');
                 // Term slugs are separated by space
                 $post_tax = explode(' ', $post_tax);
                 foreach ($terms as $each_term) {
                     if (in_array($each_term, $post_tax)) {
                         $matches = true;
                     }
                 }
             }
             if (!empty($term_ids)) {
                 $post_tax_ids = do_shortcode('[taxonomy ' . $taxonomy . ' id="' . $pid . '" field=id]');
                 // Term IDs are separated by comma..
                 $post_tax_ids = CCS_Format::explode_list($post_tax_ids);
                 foreach ($term_ids as $each_term_id) {
                     if (in_array($each_term_id, $post_tax_ids)) {
                         $matches = true;
                     }
                 }
             }
         }
         if ($matches) {
             self::$state['is_comments_loop'] = true;
             // Keep it true in case nested
             self::$state['current_comment'] = $comment;
             $result = do_ccs_shortcode($content, false);
             $check = trim($result);
             if (!empty($check)) {
                 $index++;
             }
             $out .= $result;
         }
     }
     // Close loop
     self::$state = $prev_state;
     //    self::$state['is_comments_loop'] = false;
     //    self::$state['current_comment'] = '';
     return $out;
 }