PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/s2member/includes/classes/utils-gets.inc.php

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