PluginProbe
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions / 260805
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions v260805
260917 260913 260909 260829 260814 260805 110710 110731 110812 110815 110912 110913 110915 110926 110927 111002 111003 111011 111017 111029 111105 111206 111216 111220 120213 All 189 releases
s2member / src / includes / classes / utils-gets.inc.php

utils-gets.inc.php in s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions 260805, at src/includes/classes/utils-gets.inc.php

427 lines 20.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // @codingStandardsIgnoreFile
3 /**
4 * Get utilities.
5 *
6 * Copyright: © 2009-2011
7 * {@link http://websharks-inc.com/ WebSharks, Inc.}
8 * (coded in the USA)
9 *
10 * Released under the terms of the GNU General Public License.
11 * You should have received a copy of the GNU General Public License,
12 * along with this software. In the main directory, see: /licensing/
13 * If not, see: {@link http://www.gnu.org/licenses/}.
14 *
15 * @package s2Member\Utilities
16 * @since 3.5
17 */
18 if(!defined('WPINC')) // MUST have WordPress.
19 exit ('Do not access this file directly.');
20
21 if(!class_exists('c_ws_plugin__s2member_utils_gets'))
22 {
23 /**
24 * Get utilities.
25 *
26 * @package s2Member\Utilities
27 * @since 3.5
28 */
29 class c_ws_plugin__s2member_utils_gets
30 {
31 /**
32 * Retrieves a unique array of all Category IDs in the database.
33 *
34 * @package s2Member\Utilities
35 * @since 3.5
36 *
37 * @uses {@link http://codex.wordpress.org/Function_Reference/get_terms get_terms()}
38 *
39 * @return array Unique array of all Category IDs *(as integers)*.
40 */
41 public static function get_all_category_ids()
42 {
43 if(!($category_ids = wp_cache_get('all_category_ids', 'category')))
44 {
45 $category_ids = get_terms('category', array('fields' => 'ids', 'get' => 'all'));
46 wp_cache_add('all_category_ids', $category_ids, 'category');
47 }
48 if(is_array($category_ids)) // Use a WP function for this.
49 $category_ids = c_ws_plugin__s2member_utils_arrays::force_integers((array)$category_ids);
50
51 return (!empty($category_ids) && is_array($category_ids)) ? array_unique($category_ids) : array();
52 }
53
54 /**
55 * Retrieves a unique array of all child Category IDs, within a specific parent Category.
56 *
57 * @package s2Member\Utilities
58 * @since 3.5
59 *
60 * @param int|string $parent_category A numeric Category ID.
61 *
62 * @return array Unique array of all Category IDs *(as integers)* in ``$parent_category``.
63 */
64 public static function get_all_child_category_ids($parent_category = 0)
65 {
66 if(is_numeric($parent_category) && $parent_category && is_array($child_categories = get_categories('child_of='.$parent_category.'&hide_empty=0')))
67 foreach($child_categories as $child_category) // Go through child Categories.
68 $child_category_ids[] = (int)$child_category->term_id;
69
70 return (!empty($child_category_ids) && is_array($child_category_ids)) ? array_unique($child_category_ids) : array();
71 }
72
73 /**
74 * Retrieves a unique array of all Tag IDs in the database.
75 *
76 * @package s2Member\Utilities
77 * @since 3.5
78 *
79 * @return array Unique array of all Tag IDs *(as integers)*.
80 */
81 public static function get_all_tag_ids()
82 {
83 foreach((array)get_tags('hide_empty=0') as $tag)
84 $tag_ids[] = (int)$tag->term_id; // Collect Tag's ID.
85
86 return (!empty($tag_ids) && is_array($tag_ids)) ? array_unique($tag_ids) : array();
87 }
88
89 /**
90 * Converts a comma-delimited list of: Tag slugs/names/ids, into a unique array of all IDs.
91 *
92 * @package s2Member\Utilities
93 * @since 111101
94 *
95 * @param string $tags Tag slugs/names/IDs; comma-delimited.
96 *
97 * @return array Unique array of Tag IDs *(as integers)*. With Tag slugs/names converted to IDs.
98 */
99 public static function get_tags_converted_to_ids($tags = '')
100 {
101 foreach(preg_split('/['."\r\n\t".';,]+/', (string)$tags) as $tag)
102 {
103 if(($tag = trim($tag)) && is_numeric($tag)) // Force integers.
104 $tag_ids[] = ($tag_id = (int)$tag); // Force integer values here.
105
106 else if($tag && is_string($tag)) // A string (i.e., a tag name or a tag slug)?
107 {
108 if(is_object($term = get_term_by('name', $tag, 'post_tag')))
109 $tag_ids[] = (int)$term->term_id;
110
111 else if(is_object($term = get_term_by('slug', $tag, 'post_tag')))
112 $tag_ids[] = (int)$term->term_id;
113 }
114 }
115 return (!empty($tag_ids) && is_array($tag_ids)) ? array_unique($tag_ids) : array();
116 }
117
118 /**
119 * Retrieves a unique array of all published Post IDs in the database.
120 *
121 * @package s2Member\Utilities
122 * @since 3.5
123 *
124 * @param string $post_type Optional. If provided, return all Post IDs of a specific Post Type.
125 * Otherwise, return all Post IDs that are NOT of these Post Types: `page|attachment|nav_menu_item|revision`.
126 *
127 * @return array Unique array of all Post IDs *(as integers)*, including Custom Post Types; or all Post IDs of a specific Post Type.
128 */
129 public static function get_all_post_ids($post_type = '')
130 {
131 /** @var wpdb $wpdb WordPress DB object instance. */
132 global $wpdb; // Global DB object reference.
133
134 if(is_array($post_ids = $wpdb->get_col("SELECT `ID` FROM `".$wpdb->posts."` WHERE `post_status` = 'publish' AND ".(($post_type) ? "`post_type` = '".esc_sql((string)$post_type)."'" : "`post_type` NOT IN('page','attachment','nav_menu_item','revision')"))))
135 $post_ids = c_ws_plugin__s2member_utils_arrays::force_integers($post_ids);
136
137 return (!empty($post_ids) && is_array($post_ids)) ? array_unique($post_ids) : array();
138 }
139
140 /**
141 * Retrieves a unique array of all published Child Post IDs in the database.
142 *
143 * @package s2Member\Utilities
144 * @since 3.5
145 *
146 * @param int|string|array $parent_ids One or more parent IDs to collect children for.
147 *
148 * @param string $post_type Optional. If provided, return all Post IDs of a specific Post Type.
149 * Otherwise, return all Post IDs that are NOT of these Post Types: `page|attachment|nav_menu_item|revision`.
150 *
151 * @return array Unique array of all Child Post IDs *(as integers)*, including Custom Post Types; or all Child Post IDs of a specific Post Type.
152 */
153 public static function get_all_child_post_ids($parent_ids = array(), $post_type = '')
154 {
155 /** @var wpdb $wpdb WordPress DB object instance. */
156 global $wpdb; // Global DB object reference.
157
158 if(($parent_ids = (array)$parent_ids)) // Force an array value.
159 if(is_array($post_ids = $wpdb->get_col("SELECT `ID` FROM `".$wpdb->posts."` WHERE `post_status` = 'publish'".
160 " AND ".($post_type ? "`post_type` = '".esc_sql((string)$post_type)."'" : "`post_type` NOT IN('page','attachment','nav_menu_item','revision')").
161 " AND `post_parent` IN('".implode("','", $parent_ids)."')"))
162 ) $post_ids = c_ws_plugin__s2member_utils_arrays::force_integers($post_ids);
163
164 return (!empty($post_ids) && is_array($post_ids)) ? array_unique($post_ids) : array();
165 }
166
167 /**
168 * Retrieves a unique array of all published Page IDs in the database.
169 *
170 * @package s2Member\Utilities
171 * @since 3.5
172 *
173 * @return array Unique array of all Page IDs *(as integers)*.
174 */
175 public static function get_all_page_ids()
176 {
177 /** @var wpdb $wpdb WordPress DB object instance. */
178 global $wpdb; // Global DB object reference.
179
180 if(is_array($page_ids = $wpdb->get_col("SELECT `ID` FROM `".$wpdb->posts."` WHERE `post_status` = 'publish' AND `post_type` = 'page'")))
181 $page_ids = c_ws_plugin__s2member_utils_arrays::force_integers($page_ids);
182
183 return (!empty($page_ids) && is_array($page_ids)) ? array_unique($page_ids) : array();
184 }
185
186 /**
187 * Retrieves a unique array of all Singular IDs in the database that require Custom Capabilities.
188 *
189 * @package s2Member\Utilities
190 * @since 111101
191 *
192 * @return array Unique array of all Singular IDs *(as integers)* that require Custom Capabilities.
193 */
194 public static function get_all_singular_ids_with_ccaps()
195 {
196 /** @var wpdb $wpdb WordPress DB object instance. */
197 global $wpdb; // Global DB object reference.
198
199 if(is_array($singular_ids = $wpdb->get_col("SELECT `post_id` FROM `".$wpdb->postmeta."` WHERE `meta_key` = 's2member_ccaps_req' AND `meta_value` != ''")))
200 $singular_ids = c_ws_plugin__s2member_utils_arrays::force_integers($singular_ids);
201
202 return (!empty($singular_ids) && is_array($singular_ids)) ? array_unique($singular_ids) : array();
203 }
204
205 /**
206 * Retrieves a unique array of unavailable Singular IDs that require Custom Capabilities.
207 *
208 * Only returns Singular IDs that require Custom Capabilities;
209 * and ONLY those which are NOT satisfied by ``$user``.
210 *
211 * @package s2Member\Utilities
212 * @since 111101
213 *
214 * @param WP_User $user Optional. A `WP_User` object. If this is a valid `WP_User` object, test against this ``$user``, else all are unavailable.
215 *
216 * @return array Unique array of all Singular IDs *(as integers)* NOT available to ``$user``, due to Custom Capability Restrictions.
217 */
218 public static function get_unavailable_singular_ids_with_ccaps($user = NULL)
219 {
220 /** @var wpdb $wpdb WordPress DB object instance. */
221 global $wpdb; // Global DB object reference.
222
223 if(is_array($results = $wpdb->get_results("SELECT `".$wpdb->postmeta."`.`post_id`, `".$wpdb->postmeta."`.`meta_value`, `".$wpdb->posts."`.`post_type`".
224 " FROM `".$wpdb->posts."`, `".$wpdb->postmeta."` WHERE `".$wpdb->posts."`.`ID` = `".$wpdb->postmeta."`.`post_id`".
225 " AND `".$wpdb->postmeta."`.`meta_key` = 's2member_ccaps_req' AND `".$wpdb->postmeta."`.`meta_value` != ''")))
226 {
227 $bbpress_restrictions_enable = apply_filters('ws_plugin__s2member_bbpress_restrictions_enable', TRUE);
228 $bbpress_installed = c_ws_plugin__s2member_utils_conds::bbp_is_installed(); // bbPress is installed?
229 $bbpress_forum_post_type = $bbpress_installed ? bbp_get_forum_post_type() : ''; // Acquire the current post type for forums.
230 $bbpress_topic_post_type = $bbpress_installed ? bbp_get_topic_post_type() : ''; // Acquire the current post type for topics.
231
232 foreach($results as $r) // Now we need to check Custom Capabilities against ``$user``. If ``$user`` is a valid `WP_User` object, else all are unavailable.
233 {
234 if(!is_object($user) || empty($user->ID)) // No ``$user`` object? Maybe not logged-in?.
235 $singular_ids[] = (int)$r->post_id; // It's NOT available. There is no ``$user``.
236
237 else if(is_array($ccaps = c_ws_plugin__s2member_utils_arrays::maybe_unserialize($r->meta_value))) // Make sure we unserialize.
238 {
239 foreach($ccaps as $ccap) // Test for Custom Capability Restrictions now.
240 if(strlen($ccap) && !$user->has_cap('access_s2member_ccap_'.$ccap))
241 {
242 $singular_ids[] = (int)$r->post_id; // It's NOT available.
243 break; // Break now, no need to continue in this loop.
244 }
245 }
246 if($bbpress_restrictions_enable && $bbpress_installed && $r->post_type === $bbpress_forum_post_type)
247 if(!empty($singular_ids) && in_array((int)$r->post_id, $singular_ids, TRUE))
248 {
249 if(is_array($child_results = $wpdb->get_results("SELECT `".$wpdb->posts."`.`ID` as `post_id` FROM `".$wpdb->posts."`".
250 " WHERE `".$wpdb->posts."`.`post_parent` = '".esc_sql($r->post_id)."'".
251 " AND `".$wpdb->posts."`.`post_type` = '".esc_sql($bbpress_topic_post_type)."'")))
252 foreach($child_results as $child_r) $singular_ids[] = (int)$child_r->post_id;
253 }
254 }
255 }
256 return (!empty($singular_ids) && is_array($singular_ids)) ? array_unique($singular_ids) : array();
257 }
258
259 /**
260 * Retrieves a unique array of all Singular IDs that require Specific Post/Page Access.
261 *
262 * @package s2Member\Utilities
263 * @since 111101
264 *
265 * @param bool $exclude_conflicts Optional. Defaults to false. If true, return ONLY those which are NOT in conflict with any other Restriction Types.
266 * The ``$exclude_conflicts`` argument should be used whenever we introduce a list of option values to a site owner. Helping them avoid mishaps.
267 * Please note, the ``$exclude_conflicts`` argument implements a resource-intensive processing routine.
268 *
269 * @return array Unique array of all Singular IDs *(as integers)* that require Specific Post/Page Access.
270 */
271 public static function get_all_singular_ids_with_sp($exclude_conflicts = FALSE)
272 {
273 if(is_array(($singular_ids = ($GLOBALS['WS_PLUGIN__']['s2member']['o']['specific_ids'] && is_array($singular_ids = preg_split('/['."\r\n\t".'\s;,]+/', $GLOBALS['WS_PLUGIN__']['s2member']['o']['specific_ids']))) ? $singular_ids : array())))
274 $singular_ids = c_ws_plugin__s2member_utils_arrays::force_integers($singular_ids);
275
276 if(!empty($singular_ids) && is_array($singular_ids) && $exclude_conflicts /* Return ONLY those which are NOT in conflict with other Restrictions? */)
277 {
278 $x_ids = array($GLOBALS['WS_PLUGIN__']['s2member']['o']['login_welcome_page'], $GLOBALS['WS_PLUGIN__']['s2member']['o']['membership_options_page'], $GLOBALS['WS_PLUGIN__']['s2member']['o']['file_download_limit_exceeded_page']);
279
280 $x_ids = array_merge($x_ids, c_ws_plugin__s2member_utils_gets::get_all_singular_ids_with_ccaps());
281
282 for($n = 0; $n <= $GLOBALS['WS_PLUGIN__']['s2member']['c']['levels']; $n++)
283 {
284 if($GLOBALS['WS_PLUGIN__']['s2member']['o']['level'.$n.'_catgs'] === 'all')
285 {
286 $catgs = c_ws_plugin__s2member_utils_gets::get_all_category_ids();
287 $x_ids = array_merge($x_ids, c_ws_plugin__s2member_utils_gets::get_singular_ids_in_terms($catgs));
288 continue; // Continue. The `all` specification is absolute. There's nothing more.
289 }
290 foreach(($catgs = preg_split('/['."\r\n\t".'\s;,]+/', $GLOBALS['WS_PLUGIN__']['s2member']['o']['level'.$n.'_catgs'])) as $catg)
291 $catgs = array_merge($catgs, c_ws_plugin__s2member_utils_gets::get_all_child_category_ids($catg));
292
293 $x_ids = array_merge($x_ids, c_ws_plugin__s2member_utils_gets::get_singular_ids_in_terms($catgs));
294 unset ($catgs, $catg); // Just a little housekeeping here.
295 }
296 for($n = 0; $n <= $GLOBALS['WS_PLUGIN__']['s2member']['c']['levels']; $n++)
297 {
298 if($GLOBALS['WS_PLUGIN__']['s2member']['o']['level'.$n.'_ptags'] === 'all')
299 {
300 $tags = c_ws_plugin__s2member_utils_gets::get_all_tag_ids();
301 $x_ids = array_merge($x_ids, c_ws_plugin__s2member_utils_gets::get_singular_ids_in_terms($tags));
302 continue; // Continue. The `all` specification is absolute. There's nothing more.
303 }
304 $tags = c_ws_plugin__s2member_utils_gets::get_tags_converted_to_ids($GLOBALS['WS_PLUGIN__']['s2member']['o']['level'.$n.'_ptags']);
305
306 $x_ids = array_merge($x_ids, c_ws_plugin__s2member_utils_gets::get_singular_ids_in_terms($tags));
307 unset ($tags); // Just a little housekeeping here.
308 }
309 for($n = 0; $n <= $GLOBALS['WS_PLUGIN__']['s2member']['c']['levels']; $n++)
310 {
311 if($GLOBALS['WS_PLUGIN__']['s2member']['o']['level'.$n.'_posts'] === 'all')
312 {
313 $x_ids = array_merge($x_ids, c_ws_plugin__s2member_utils_gets::get_all_post_ids());
314 continue; // Continue. The `all` specification is absolute. There's nothing more.
315 }
316 foreach(($posts = preg_split('/['."\r\n\t".'\s;,]+/', $GLOBALS['WS_PLUGIN__']['s2member']['o']['level'.$n.'_posts'])) as $p)
317 if(strpos($p, 'all-') === 0 && preg_match('/^all-(.+)$/', $p, $m) /* Protecting `all-` of a specific Post Type? */)
318 if((is_array($p_of_type = c_ws_plugin__s2member_utils_gets::get_all_post_ids($m[1])) || (substr($m[1], -1) === 's' && is_array($_p_of_type = c_ws_plugin__s2member_utils_gets::get_all_post_ids(substr($m[1], 0, -1))))) && !empty($p_of_type))
319 $x_ids = array_merge($x_ids, $p_of_type); // Merge all Posts of this Post Type.
320
321 $x_ids = array_merge($x_ids, $posts); // Merge together.
322 unset ($posts, $p, $m, $p_of_type); // Just a little housekeeping here.
323 }
324 for($n = 0; $n <= $GLOBALS['WS_PLUGIN__']['s2member']['c']['levels']; $n++)
325 {
326 if($GLOBALS['WS_PLUGIN__']['s2member']['o']['level'.$n.'_pages'] === 'all')
327 {
328 $x_ids = array_merge($x_ids, c_ws_plugin__s2member_utils_gets::get_all_page_ids());
329 continue; // Continue. The `all` specification is absolute. There's nothing more.
330 }
331 $pages = preg_split('/['."\r\n\t".'\s;,]+/', $GLOBALS['WS_PLUGIN__']['s2member']['o']['level'.$n.'_pages']);
332
333 $x_ids = array_merge($x_ids, $pages); // Merge.
334 unset ($pages); // Just a little housekeeping here.
335 }
336 $x_ids = array_unique(c_ws_plugin__s2member_utils_arrays::force_integers($x_ids));
337 $singular_ids = array_diff($singular_ids, $x_ids);
338 }
339 return (!empty($singular_ids) && is_array($singular_ids)) ? array_unique($singular_ids) : array();
340 }
341
342 /**
343 * Retrieves a unique array of unavailable Singular IDs that require Specific Post/Page Access.
344 *
345 * Only returns Singular IDs that require Specific Post/Page Access;
346 * and ONLY those which are NOT satisfied by the current Visitor.
347 *
348 * @package s2Member\Utilities
349 * @since 111101
350 *
351 * @param bool $exclude_conflicts Optional. Defaults to false. If true, return ONLY those which are NOT in conflict with any other Restriction Types.
352 * The ``$exclude_conflicts`` argument should be used whenever we introduce a list of option values to a site owner. Helping them avoid mishaps.
353 * Please note, the ``$exclude_conflicts`` argument implements a resource-intensive processing routine.
354 *
355 * @return array Unique array of all Singular IDs *(as integers)* NOT available to current Visitor, due to Specific Post/Page Restrictions.
356 */
357 public static function get_unavailable_singular_ids_with_sp($exclude_conflicts = FALSE)
358 {
359 if($GLOBALS['WS_PLUGIN__']['s2member']['o']['specific_ids'] && is_array($_singular_ids = preg_split('/['."\r\n\t".'\s;,]+/', $GLOBALS['WS_PLUGIN__']['s2member']['o']['specific_ids'])))
360 foreach($_singular_ids as $_singular_id) // Now check access to this Singular, against the current Visitor, via read-only ``c_ws_plugin__s2member_sp_access::sp_access()``.
361 if(is_numeric($_singular_id) && !c_ws_plugin__s2member_sp_access::sp_access($_singular_id, 'read-only'))
362 $singular_ids[] = (int)$_singular_id;
363
364 if(!empty($singular_ids) && is_array($singular_ids) && $exclude_conflicts)
365 {
366 $all_singular_ids_not_conflicting = c_ws_plugin__s2member_utils_gets::get_all_singular_ids_with_sp('exclude-conflicts');
367 foreach($singular_ids as $s => $singular_id) // Weed out anything that's in conflict here.
368 if(!in_array($singular_id, $all_singular_ids_not_conflicting))
369 unset($singular_ids[$s]); // Housekeeping.
370 }
371 return (!empty($singular_ids) && is_array($singular_ids)) ? array_unique($singular_ids) : array();
372 }
373
374 /**
375 * Retrieves a unique array of all published Singulars, protected with Specific Post/Page Access.
376 *
377 * @package s2Member\Utilities
378 * @since 111101
379 *
380 * @uses {@link http://codex.wordpress.org/Function_Reference/get_posts get_posts()}
381 *
382 * @param bool $exclude_conflicts Optional. Defaults to false. If true, return ONLY those which are NOT in conflict with any other Restriction Types.
383 * The ``$exclude_conflicts`` argument should be used whenever we introduce a list of option values to a site owner. Helping them avoid mishaps.
384 * Please note, the ``$exclude_conflicts`` argument implements a resource-intensive processing routine.
385 *
386 * @return array Unique array of all Singulars *(i.e., Posts/Pages )* protected with Specific Post/Page Access.
387 * Includes Custom Post Types also, as specified by site owner's Specific Post/Page Restrictions.
388 */
389 public static function get_all_singulars_with_sp($exclude_conflicts = FALSE)
390 {
391 $singulars = ($GLOBALS['WS_PLUGIN__']['s2member']['o']['specific_ids'] && is_array($singulars = get_posts('post_status=publish&post_type=any&include='.$GLOBALS['WS_PLUGIN__']['s2member']['o']['specific_ids']))) ? $singulars : array();
392
393 if(!empty($singulars) && is_array($singulars) && $exclude_conflicts)
394 {
395 $all_singular_ids_not_conflicting = c_ws_plugin__s2member_utils_gets::get_all_singular_ids_with_sp('exclude-conflicts');
396 foreach($singulars as $s => $singular) // Weed out anything that's in conflict here.
397 if(!in_array($singular->ID, $all_singular_ids_not_conflicting))
398 unset($singulars[$s]); // Housekeeping.
399 }
400 return (!empty($singulars) && is_array($singulars)) ? c_ws_plugin__s2member_utils_arrays::array_unique($singulars) : array();
401 }
402
403 /**
404 * Retrieves a unique array of Singular IDs in the database, within specific term IDs.
405 *
406 * Only returns Singular IDs that are within the ``$terms`` passed through this function.
407 *
408 * @package s2Member\Utilities
409 * @since 110912
410 *
411 * @param array $terms Required. An array of term IDs.
412 *
413 * @return array Unique array of all Singular IDs *(as integers)* within the ``$terms`` passed through this function.
414 */
415 public static function get_singular_ids_in_terms($terms = array())
416 {
417 /** @var wpdb $wpdb WordPress DB object instance. */
418 global $wpdb; // Global DB object reference.
419
420 if(!empty($terms) && is_array($terms) && is_array($singular_ids = $wpdb->get_col("SELECT `object_id` FROM `".$wpdb->term_relationships."` WHERE `term_taxonomy_id` IN (SELECT `term_taxonomy_id` FROM `".$wpdb->term_taxonomy."` WHERE `term_id` IN('".implode("','", $terms)."'))")))
421 $singular_ids = c_ws_plugin__s2member_utils_arrays::force_integers($singular_ids);
422
423 return (!empty($singular_ids) && is_array($singular_ids)) ? array_unique($singular_ids) : array();
424 }
425 }
426 }
427