30 | 30 | |
31 | 31 | // Force the browse query_var when querying for a users favorites |
32 | 32 | if ( !empty( $query->query_vars['favorites_user'] ) ) { |
33 | 33 | $query->query_vars['browse'] = 'favorites'; |
34 | 34 | } |
35 | 35 | |
36 | 36 | // eliminate draft posts from showing up in the directory |
37 | 37 | if ( !isset( $query->query_vars['post_status'] ) ) { |
38 | 38 | $query->query_vars['post_status'] = 'publish'; |
39 | 39 | } |
40 | 40 | |
41 | 41 | switch ( $query->query_vars['browse'] ) { |
42 | 42 | case 'new': |
43 | 43 | $query->query_vars['orderby'] = 'post_date'; |
44 | 44 | $query->query_vars['order'] = 'DESC'; |
50 | 52 | break; |
51 | 53 | |
52 | 54 | case 'featured': |
53 | 55 | // Pages > 1 don't exist. |
54 | 56 | if ( isset( $query->query_vars['paged'] ) && $query->query_vars['paged'] > 1 ) { |
55 | 57 | // Force a 404 |
56 | 58 | $query->query_vars['post__in'] = array( 0 ); |
57 | 59 | } |
58 | 60 | |
59 | 61 | $query->query_vars['posts_per_page'] = $query->found_posts = 15; |
60 | 62 | // Featured themes require it to have been updated within the last year, not the default 2. |
61 | 63 | $query->query_vars['date_query']['recent_themes_only'] = array( |
62 | 64 | 'column' => 'post_modified', |
63 | 65 | 'after' => date( 'Y-m-d', strtotime( '-1 year' ) ) |
64 | 66 | ); |
65 | 67 | |
66 | 68 | // Some themes are always featured by ways of the menu_order |
67 | 69 | $query->query_vars['orderby'] = 'menu_order DESC, RAND(' . date( 'Ymd' ) . ')'; |
68 | 70 | $query->query_vars['no_found_rows'] = true; |
69 | 72 | break; |
70 | 73 | |
71 | 74 | case 'favorites': |
72 | 75 | $favorites = array(); |
73 | 76 | |
74 | 77 | if ( ! empty( $query->query_vars['favorites_user'] ) ) { |
75 | 78 | $user_id = get_user_by( 'login', $query->query_vars['favorites_user'] )->ID; |
76 | 79 | } elseif ( is_user_logged_in() ) { |
77 | 80 | $user_id = get_current_user_id(); |
78 | 81 | } |
79 | 82 | |
80 | 83 | if ( ! empty( $user_id ) ) { |
81 | 84 | $favorites = array_filter( (array) get_user_meta( $user_id, 'theme_favorites', true ) ); |
82 | 85 | } |
83 | 86 | |
84 | 87 | if ( $favorites ) { |
85 | 88 | $query->query_vars['post_name__in'] = $favorites; |
86 | 89 | } else { |
87 | 90 | // Force a 404 |
88 | 91 | $query->query_vars['post__in'] = array( 0 ); |
89 | 92 | } |
90 | 93 | |
91 | 94 | $query->query_vars['orderby'] = 'post_title'; |
92 | 95 | $query->query_vars['order'] = 'ASC'; |
93 | 97 | break; |
94 | 98 | |
95 | 99 | case 'popular': |
96 | 100 | // Only include themes that have existed for at least 2 weeks into the popular listing |
97 | 101 | // This avoids cases where a new theme skews our popularity algorithms. |
98 | 102 | $query->query_vars['date_query']['existing_themes_only'] = array( |
99 | 103 | 'column' => 'post_date', |
100 | 104 | 'before' => date( 'Y-m-d', strtotime( '-2 weeks' ) ) |
101 | 105 | ); |
102 | 106 | |
103 | 107 | // Sort by the popularity meta key |
104 | 108 | $query->query_vars['meta_key'] = '_popularity'; |
105 | 109 | $query->query_vars['meta_type'] = 'DECIMAL(20,10)'; |
106 | 110 | $query->query_vars['orderby'] = 'meta_value DESC'; |
107 | 112 | break; |
108 | 113 | } |
109 | 114 | |
110 | 115 | // Unless a specific theme, or author is being requested, limit results to the last 2 years. |
111 | 116 | if ( empty( $query->query_vars['name'] ) && empty( $query->query_vars['author_name'] ) && ! in_array( $query->query_vars['browse'], array( 'favorites', 'new', 'updated' ) ) ) { |
112 | 117 | $query->query_vars['date_query']['recent_themes_only'] = array( |
113 | 118 | 'column' => 'post_modified', |
114 | 119 | 'after' => date( 'Y-m-d', strtotime( '-2 years' ) ), |
115 | 120 | ); |
116 | 121 | } |
117 | 122 | |
118 | 123 | // Prioritize translated themes for localized requests, except when viewing a specific ordered themes. |
119 | 124 | if ( 'en_US' !== get_locale() && ! in_array( $query->query_vars['browse'], array( 'favorites', 'new', 'updated' ) ) ) { |
120 | 125 | add_filter( 'posts_clauses', 'wporg_themes_prioritize_translated_posts_clauses' ); |
121 | 126 | } |