View difference between Paste ID: DZDDeX9f and 3nKeL0rW
SHOW: | | - or go back to the newest paste.
1
<?php
2
3
/*****************************************************************
4
*
5
* 
6
* a hierarchical list of all posts by nested categories, post title and authors
7
* © Fabio Marzocca - 2015-2022
8
*
9
* Frontend
10
******************************************************************/
11
12
13
14
function ACT_hierarchy_indexes($atts)
15
{
16
17
    if (!isset($_POST['order'])) {
18
        $_POST['order']="category";
19
    }
20
21
    if ($atts['singleuser']) {
22
        $atts['admin'] = "1";
23
    }
24
    ob_start();
25
    
26
    echo '<div class="ACT-wrapper">';
27
    if (count(explode(",", $atts['show'])) > 1) :
28
?>
29
30
<form name="form1" method="post" >
31
        <div align="center" class="styled-select"><?php _e("Group by:", 'list-all-posts-by-authors-nested-categories-and-titles') ?> 
32
          <select name="order"  id="order"  onChange=" ;this.form.submit();">
33
            <?php if (!($atts['singleuser']) and strpos($atts['show'], "Author") !== false) : ?>
34
            <option value="author" <?php if ($_POST['order']  == "author") {
35
                echo "selected";
36
                                   } ?>><?php _e("Author", 'list-all-posts-by-authors-nested-categories-and-titles'); ?> </option>
37
            <?php endif; ?>
38
            <?php if (strpos($atts['show'], "Title") !== false) : ?>
39
            <option value="title" <?php if ($_POST['order']  == "title") {
40
                echo "selected";
41
                                  } ?>><?php _e("Title", 'list-all-posts-by-authors-nested-categories-and-titles'); ?> </option>
42
            <?php endif; ?>
43
                <?php if (strpos($atts['show'], "Category") !== false) : ?>
44
            <option value="category" <?php if ($_POST['order']  == "category") {
45
                echo "selected";
46
                                     } ?>><?php _e("Category", 'list-all-posts-by-authors-nested-categories-and-titles'); ?> </option>
47
                <?php endif; ?>
48
          </select>
49
        </div>
50
    </form>
51
<?php
52
    endif;
53
    
54
    if (count(explode(",", $atts['show'])) == 1) :
55
        $_POST['order'] = strtolower($atts['show']);
56
    endif;
57
    
58
    if (count(explode(",", $atts['show'])) == 2 and strpos($atts['show'], "Category") === false) :
59
        $_POST['order'] = "author";
60
    endif;
61
62
    if ($_POST['order']  == "author") {
63
        ACT_byauthor($atts);
64
    } elseif ($_POST['order']  == "title") {
65
        ACT_bytitle($atts);
66
    } else {
67
        ACT_bycategory($atts);
68
    }
69
    echo "</div> <!-- ACT-wrapper -->";
70
    $output_string=ob_get_contents();
71
    ob_end_clean();
72
    return $output_string;
73
}
74
    
75
function ACT_bycategory($atts)
76
{
77
    /* Start browsing categories*/
78
    foreach (get_categories('hide_empty=0') as $cat) :
79
        if (strpos($atts['exclude'], $cat->slug)!== false) :
80
            continue;
81
        endif;
82
	
83
  /* multiple changes  */    
84
        if (!$cat->parent) {
85
             echo "<h2 id='".$cat->name."'><a href='".get_category_link($cat)."'>".wptexturize($cat->name)."</a></h2><ol>";
86
            ACT_traverse_cat_tree( $cat->term_id, $atts);
87
        }
88
    endforeach;
89
}
90
91
92
function ACT_traverse_cat_tree($cat, $atts)
93
{
94
    $postargs = array(
95
        'public'   => true,
96
        '_builtin' => false
97
    );
98
    $post_types = get_post_types( $postargs);
99
100
    array_push($post_types, 'post');
101
    $ordering = 'DESC';
102
    if ($atts['reverse-date']) {
103
        $ordering = 'ASC';
104
    }
105
    $args = array('category__in' => array( $cat ), 'numberposts' => -1, 'order' => $ordering, 'post_type' => $post_types);
106
    
107
    $cat_posts = get_posts( $args );
108
    
109
    if ($cat_posts) :
110
        $i = 0;
111
        foreach ($cat_posts as $post) :
112
            /* exclude admin?  */
113
            if (!$atts['admin']) {
114
                if (is_super_admin($post->post_author)) :
115
                    continue;
116
                endif;
117
            }
118
119
            echo '<li class="subpost">';
120
           $postdate = date_i18n( get_option( 'date_format' ), strtotime($post->post_date)).' - ';
121
    echo ($atts['postdate'] ? $postdate : ''). '<a href="' . get_permalink( $post->ID ) . '">' . wptexturize($post->post_title) . '</a>';
122
            if (!($atts['singleuser'])) :
123
                echo "<span class='righttext'>[".get_the_author_meta( 'first_name', $post->post_author )." ".get_the_author_meta( 'last_name', $post->post_author )."]</span>";
124
            endif;
125
    
126
            echo '</li>';
127
            $i++;
128
            if ($atts['postspercategory'] > -1) :
129
                if ($i >= $atts['postspercategory']) :
130
                    break;
131
                endif;
132
            endif;
133
        endforeach;
134
    endif;
135
    $next = get_categories('hide_empty=0&parent=' . $cat);
136
 
137
    if ($next) :
138
        foreach ($next as $cat) :
139
            if (strpos($atts['exclude'], $cat->slug)!== false) :
140
                continue;
141
            endif;
142
	
143
/* changes 1 n 5  */
144
               echo "<ol><li class='subcat'><a href='".get_category_link($cat)."'>".wptexturize($cat->name)."</a></li>";
145
              ACT_traverse_cat_tree( $cat->term_id, $atts);
146
        endforeach;
147
    endif;
148
    echo '</ol>';
149
}
150
/* list ol closer  */
151
152
function ACT_bytitle($atts)
153
{
154
    $postargs = array(
155
        'public'   => true,
156
        '_builtin' => false
157
    );
158
    $post_types = get_post_types( $postargs);
159
160
    array_push($post_types, 'post');
161
162
    if ($atts['totalpoststitle'] > -1) {
163
        $args = array(  'posts_per_page' => -1, 'post_type' => $post_types);
164
    } else {
165
        $args = array(  'posts_per_page' => -1,
166
                    'orderby' => 'title' ,
167
                    'post_type' => $post_types,
168
                    'order' => 'ASC');
169
    }
170
    $articles = get_posts($args);
171
    echo "<h4></h4>";
172
    if ($articles) :
173
	 
174
/* changes 1 line */
175
        echo "<ol>";
176
        $i = 0;
177
        foreach ($articles as $article) :
178
        /* excluded categories  */
179
            if (has_category(explode(',', $atts['exclude']), $article->ID)) :
180
                continue;
181
            endif;
182
            
183
        /* include admin? */
184
            if (!$atts['admin']) {
185
                if (is_super_admin($article->post_author)) :
186
                    continue;
187
                endif;
188
            }
189
            echo '<li>';
190
            $postdate = date_i18n( get_option( 'date_format' ), strtotime($article->post_date)).' - ';
191
            echo ($atts['postdate'] ? $postdate : '').'<a href="' . get_permalink( $article->ID ) . '">' . wptexturize($article->post_title) . '</a>';
192
            if (!($atts['singleuser'])) :
193
                echo "<span class='righttext'>[".get_the_author_meta( 'first_name', $article->post_author )." ".get_the_author_meta( 'last_name', $article->post_author )."]</span>";
194
            else :
195
                $categories = get_the_category( $article->ID );
196
                $list_cats =null;
197
                foreach ($categories as $cat) :
198
                    $list_cats .= wptexturize($cat->name).", ";
199
                endforeach;
200
                $list_cats = substr($list_cats, 0, -2);
201
                echo "<span class='righttext'>[".$list_cats."]</span>";
202
            endif;
203
            echo '</li>';
204
            $i++;
205
            if ($atts['totalpoststitle'] > -1) :
206
                if ($i >= $atts['totalpoststitle']) :
207
                    break;
208
                endif;
209
            endif;
210
        endforeach;
211
        echo "</ol>";
212
    endif;
213
}
214
    /* changes uper 3rd line */
215
216
function ACT_byauthor($atts)
217
{
218
    $param = 'blog_id=1&orderby=nicename';
219
    $autori= get_users( $param );
220
221
    foreach ($autori as $user) :
222
        /*check if excluded admin */
223
        if (!$atts['admin']) {
224
            if (is_super_admin($user->ID)) :
225
                continue;
226
            endif;
227
        }
228
        /* Array of WP_User objects */
229
        $postargs = array(
230
        'public'   => true,
231
        '_builtin' => false
232
            );
233
            $post_types = get_post_types( $postargs);
234
235
            array_push($post_types, 'post');
236
    
237
            $ordering = 'DESC';
238
        if ($atts['reverse-date']) {
239
            $ordering = 'ASC';
240
        }
241
    
242
        $args= array(
243
            'author'        =>  $user->ID,
244
            'post_type'   => $post_types,
245
            'posts_per_page' =>  -1,
246
            'category__not_in' => get_cats_by_slug(explode(',', $atts['exclude']))
247
                );
248
        if ($atts['postsperauthor'] == -1) {
249
            array_push($args,'order',$ordering );
250
        }
251
            $author_posts=  get_posts( $args );
252
        if (!$author_posts) :
253
            continue;
254
        endif;
255
            echo '<h4><a href="'.get_author_posts_url($user->ID).'">'.$user->display_name.'</a></h4>';
256
	
257
   /* changes 2nd line */     
258
        if ($author_posts) {
259
            echo '<ol>';
260
            $i = 0;
261
            foreach ($author_posts as $author_post) {
262
                $postdate = date_i18n( get_option( 'date_format' ), strtotime($author_post->post_date)).' - ';  
263
                echo '<li>';
264
                echo ($atts['postdate'] ? $postdate : ''). '<a href="' . get_permalink( $author_post->ID ) . '">'.$author_post->post_title.'</a>';
265
                $categories = get_the_category( $author_post->ID );
266
                $list_cats =null;
267
                foreach ($categories as $cat) :
268
                    $list_cats .= wptexturize($cat->name).", ";
269
                endforeach;
270
                $list_cats = substr($list_cats, 0, -2);
271
                echo "<span class='righttext'>[".$list_cats."]</span>";
272
                echo '</li>';
273
                $i++;
274
                if ($atts['postsperauthor'] > -1) :
275
                    if ($i >= $atts['postsperauthor']) :
276
                        break;
277
                    endif;
278
                endif;
279
            }
280
        }
281
            echo '</ol>';
282
    endforeach;
283
}
284
    /* changes uper 3rd line */
285
286
function get_cats_by_slug($catslugs) {
287
    $catids = array();
288
    foreach($catslugs as $slug) {
289
        if (!get_category_by_slug($slug) ) break;
290
        $catids[] = get_category_by_slug($slug)->term_id;
291
    }
292
    return $catids;
293
}
294
295
?>
296