Source Code Documentation


s2member\includes\classes\utils-gets.inc.php


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

s2member\includes\classes\utils-gets.inc.php

Source Code Documentation