PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/www/wp-content/plugins/ithemes-exchange/api/pages.php

https://github.com/ArzuA/gitwordpress
PHP | 359 lines | 170 code | 43 blank | 146 comment | 85 complexity | 89ae4362f53ae2ed0af63dbffe7ca520 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * This file contains API methods used with Exchange frontend pages and product pages
  4. * @package IT_Exchange
  5. * @since 0.4.4
  6. */
  7. /**
  8. * Returns a list of all registered IT Exchange pages (including the products slug)
  9. *
  10. * @since 0.4.4
  11. *
  12. * @param boolean $break_cache pages come from it_storage which caches options. Set this to true to not retreived cached pages
  13. * @return array
  14. */
  15. function it_exchange_get_pages( $break_cache=false, $options=array() ) {
  16. if ( empty( $GLOBALS['it_exchange']['registered_pages'] ) || $break_cache ) {
  17. // Grab registered pages
  18. $registered = it_exchange_get_registered_pages( $options );
  19. $merged = array();
  20. // Grab existing DB data if its present
  21. if ( ! $pages = it_exchange_get_option( 'settings_pages', $break_cache ) )
  22. $pages = array();
  23. // Merge DB data with registered defaults
  24. foreach( $registered as $page => $default_params ) {
  25. $db_params = array();
  26. $db_params['slug'] = empty( $pages[$page . '-slug'] ) ? 0 : $pages[$page . '-slug'];
  27. $db_params['tip'] = empty( $pages[$page . '-tip'] ) ? 0 : $pages[$page . '-tip'];
  28. $db_params['name'] = empty( $pages[$page . '-name'] ) ? 0 : $pages[$page . '-name'];
  29. $db_params['type'] = empty( $pages[$page . '-type'] ) ? 0 : $pages[$page . '-type'];
  30. $db_params['wpid'] = empty( $pages[$page . '-wpid'] ) ? 0 : $pages[$page . '-wpid'];
  31. $merged[$page] = ITUtility::merge_defaults( $db_params, $default_params );
  32. }
  33. if ( !empty( $options ) )
  34. return apply_filters( 'it_exchange_get_pages', $merged, $break_cache );
  35. else
  36. $GLOBALS['it_exchange']['registered_pages'] = $merged;
  37. }
  38. return apply_filters( 'it_exchange_get_pages', $GLOBALS['it_exchange']['registered_pages'], $break_cache );
  39. }
  40. /**
  41. * Get name for page
  42. *
  43. * @since 0.4.0
  44. *
  45. * @param string $page page var
  46. * @return string url
  47. */
  48. function it_exchange_get_page_name( $page, $break_cache=false ) {
  49. $pages = it_exchange_get_pages( $break_cache );
  50. $type = it_exchange_get_page_type( $page );
  51. $page_name = false;
  52. // Return the exchagne page settings if type is exchange or if we're on the page settings tab.
  53. if ( 'exchange' == $type || ( is_admin() && ! empty( $_GET['page'] ) && $_GET['page'] == 'it-exchange-settings' && ! empty( $_GET['tab'] ) && 'pages' == $_GET['tab'] ) ) {
  54. $page_name = empty( $pages[$page]['name'] ) ? false : $pages[$page]['name'];
  55. } else if ( 'wordpress' == $type ) {
  56. $wpid = it_exchange_get_page_wpid( $page );
  57. $page_name = get_the_title( $wpid );
  58. }
  59. return apply_filters( 'it_exchange_get_page_name', $page_name, $page, $break_cache );
  60. }
  61. /**
  62. * Get editable slug for page
  63. *
  64. * @since 0.4.4
  65. *
  66. * @param string $page page var
  67. * @return string
  68. */
  69. function it_exchange_get_page_slug( $page, $break_cache=false ) {
  70. $pages = it_exchange_get_pages( $break_cache );
  71. $type = it_exchange_get_page_type( $page );
  72. $page_slug = false;
  73. // Return the exchagne page settings if type is exchange or if we're on the page settings tab.
  74. if ( 'exchange' == $type || ( is_admin() && ! empty( $_GET['page'] ) && $_GET['page'] == 'it-exchange-settings' && ! empty( $_GET['tab'] ) && 'pages' == $_GET['tab'] && empty( $GLOBALS['it_exchange']['updating_nav'] ) ) ) {
  75. $page_slug = empty( $pages[$page]['slug'] ) ? false : $pages[$page]['slug'];
  76. } else if ( 'wordpress' == $type ) {
  77. $wpid = it_exchange_get_page_wpid( $page );
  78. if ( $wp_page = get_page( $wpid ) )
  79. $page_slug = get_page_uri( $wpid );
  80. }
  81. return apply_filters( 'it_exchange_get_page_slug', $page_slug, $page, $break_cache );
  82. }
  83. /**
  84. * Get editable type for page
  85. *
  86. * @since 0.4.4
  87. *
  88. * @param string $page page var
  89. * @return string
  90. */
  91. function it_exchange_get_page_type( $page, $break_cache=false ) {
  92. $pages = it_exchange_get_pages( $break_cache );
  93. $page_type = empty( $pages[$page]['type'] ) ? false : $pages[$page]['type'];
  94. return apply_filters( 'it_exchange_get_page_type', $page_type, $page, $break_cache );
  95. }
  96. /**
  97. * Get editable WordPress ID (wpid) for page (only used if type is 'wordpress')
  98. *
  99. * @since 0.4.4
  100. *
  101. * @param string $page page var
  102. * @return string
  103. */
  104. function it_exchange_get_page_wpid( $page, $break_cache=false ) {
  105. $pages = it_exchange_get_pages( $break_cache );
  106. $page_wpid = empty( $pages[$page]['wpid'] ) ? '0' : $pages[$page]['wpid'];
  107. return apply_filters( 'it_exchange_get_page_wpid', $page_wpid, $page, $break_cache );
  108. }
  109. /**
  110. * Get permalink for ghost page
  111. *
  112. * @since 0.4.0
  113. *
  114. * @param string $page page setting
  115. * @return string url
  116. */
  117. function it_exchange_get_page_url( $page, $clear_settings_cache=false ) {
  118. $pages = it_exchange_get_pages( $clear_settings_cache );
  119. $type = it_exchange_get_page_type( $page );
  120. $page_url = false;
  121. // Give addons ability to skip this logic
  122. $filtered_url = apply_filters( 'it_exchange_get_page_url', false, $page, $clear_settings_cache );
  123. if ( $filtered_url )
  124. return $filtered_url;
  125. // If page is disabled, attempt to redirect to store. If store is disabled, redirect to site home.
  126. if ( $type == 'disabled' && $page != 'store' ) {
  127. $page = 'store';
  128. $type = it_exchange_get_page_type( 'store' );
  129. } else if ( 'disabled' == $type && 'store' == $page ) {
  130. return get_home_url();
  131. }
  132. // Return the exchange page settings if type is exchange or if we're on the page settings tab.
  133. if ( 'exchange' == $type || ( is_admin() && ! empty( $_GET['page'] ) && $_GET['page'] == 'it-exchange-settings' && ! empty( $_GET['tab'] ) && 'pages' == $_GET['tab'] && empty( $GLOBALS['it_exchange']['updating_nav'] ) ) ) {
  134. if ( empty( $pages[$page]['url'] ) || ! is_callable( $pages[$page]['url'] ) )
  135. return false;
  136. if ( ! $page_url = call_user_func( $pages[$page]['url'], $page ) )
  137. return false;
  138. } else if ( 'wordpress' == $type ) {
  139. if ( $wpid = it_exchange_get_page_wpid( $page ) )
  140. return get_permalink( $wpid );
  141. }
  142. return $page_url;
  143. }
  144. /**
  145. * Is the page using a ghost page?
  146. *
  147. * @since 0.4.4
  148. *
  149. * @param string $page page setting
  150. * @return boolean
  151. */
  152. function it_exchange_is_page_ghost_page( $page, $break_cache=false ) {
  153. $pages = it_exchange_get_pages( $break_cache );
  154. $is_ghost = ( 'exchange' == it_exchange_get_page_type( $page, $break_cache ) );
  155. return apply_filters( 'it_exchange_is_page_ghost_page', $is_ghost, $page, $break_cache );
  156. }
  157. /**
  158. * Tests to see if current page is a specific exchange page or if its an exchange page at all.
  159. *
  160. * Pass in a page as a string to test for a specific string. Leaving it blank will return the current exchange page or false
  161. *
  162. * @since 0.4.0
  163. *
  164. * @param mixed $page optional. the exchange page were checking for
  165. * @return boolean
  166. */
  167. function it_exchange_is_page( $page=false ) {
  168. global $wpdb;
  169. // If no page was passed, return the name of the exchange page or false
  170. if ( empty( $page ) ) {
  171. $is_exchange_page = get_query_var( 'it_exchange_view' );
  172. $is_exchange_page = apply_filters( 'it_exchange_is_this_an_exchange_page', $is_exchange_page );
  173. return $is_exchange_page;
  174. }
  175. // Give addons ability to skip this logic
  176. $filtered_is = apply_filters( 'it_exchange_is_page', null, $page );
  177. if ( ! is_null( $filtered_is ) )
  178. return $filtered_is;
  179. // Page Data
  180. $type = it_exchange_get_page_type( $page );
  181. $slug = it_exchange_get_page_slug( $page );
  182. // If type is disabled, return false
  183. if ( 'disabled' == $type )
  184. return false;
  185. // If type is wordpress, pass it on to the wordpress function
  186. if ( 'wordpress' == $type ) {
  187. $wpid = it_exchange_get_page_wpid( $page );
  188. return is_page( $wpid );
  189. }
  190. if ( ! empty( $_GET['it-exchange-sw-ajax'] ) && ! empty( $_GET['sw-product'] ) ) {
  191. // Are we doing AJAX, if so, grab product ID from it.
  192. return (boolean) it_exchange_get_product( $_GET['sw-product'] );
  193. } else if ( ! $query_var = get_query_var( $slug ) ) {
  194. // If we made it here, page is exchange type. Get query var
  195. return false;
  196. }
  197. // When asking if this is the account page, we need to remember that the account query_var is always set when viewing other
  198. // account based pages. if another exchange pages's query_var is also set, we will return false for account.
  199. if ( 'account' == $page && get_query_var( 'account' ) ) {
  200. // Grab all exchange pages
  201. $account_based_pages = it_exchange_get_pages();
  202. // Loop through exchange pages
  203. foreach ( $account_based_pages as $account_based_page => $values ) {
  204. // Get page slug
  205. $account_based_slug = it_exchange_get_page_slug( $account_based_page );
  206. // If this page slug is set and it isn't 'account', return false for acocunt
  207. if ( get_query_var( $account_based_slug ) && $account_based_slug != 'account' )
  208. return false;
  209. }
  210. return true;
  211. }
  212. // Return true if set and not product
  213. if ( $query_var && 'product' != $page )
  214. return true;
  215. // Try to get the post from the slug
  216. $sql = $wpdb->prepare( 'SELECT ID FROM ' . $wpdb->posts . ' WHERE post_type = "it_exchange_prod" AND post_status = "publish" AND post_name = "%s"', $query_var );
  217. if ( $id = $wpdb->get_var( $sql ) )
  218. return true;
  219. return false;
  220. }
  221. /**
  222. * Registers a page with IT Exchange.
  223. *
  224. * Registering a page with Exchange does the following:
  225. * - Creates a Ghost page for your page
  226. * - Allows WP Admin to rename the slug and the display name of the page
  227. * - Allows WP Admin to turn off its associated Ghost page and replace it with a WP page / shortcode
  228. * - Allows it to be added to our nav list in Appearance -> menus
  229. * - Allows template files to be added to themefolder/exchange/page-slug.php
  230. * - Allows for content-page-slug.php template parts to be added to the list of possible template_names
  231. * - Allows 3rd party add-ons to tell Exchange where to find the template parts
  232. *
  233. * Options:
  234. * - slug required. eg: store
  235. * - name required. eg: __( 'Store', 'it-l10n-ithemes-exchange' )
  236. * - rewrite-rules required. an array. 1st element is priority within all exchange page rewrites. 2nd element is callback that will provide the rewrite array.
  237. * - url required. callback that will provide the url for the page. Make sure to check for permalinks
  238. * - settings-name optional. The title given to the setting on Settings -> Pages
  239. * - type optional. the default value of the select box.
  240. * - menu optional. include this in the Exchange menu options under Appearances -> Menus?
  241. * - optional optional. Is the page requried? If not optional, Disable is removed from dropdown for type on Settings page
  242. *
  243. * Rewrites and URL options:
  244. * - For working examples see it_exchange_register_core_pages() in ithemes-exchange/lib/functions/function.php
  245. *
  246. * @since 0.4.4
  247. *
  248. * @param string $page unique name it-exchange uses to refer to this page
  249. * @param array $options page options
  250. * @return boolean
  251. */
  252. function it_exchange_register_page( $page, $options ) {
  253. $pages = empty( $GLOBALS['it_exchange']['registered_pages'] ) ? array() : (array) $GLOBALS['it_exchange']['registered_pages'];
  254. // Page needs to be sanatized with underscores
  255. $page = str_replace( '-', '_', sanitize_title( $page ) );
  256. // Validate we have the data we need
  257. if ( empty( $options['slug'] ) || empty( $options['name'] ) || empty( $options['url'] ) )
  258. return false;
  259. // Defaults
  260. $defaults = array(
  261. 'settings-name' => ucwords( $options['name'] ),
  262. 'type' => 'exchange',
  263. 'tip' => empty( $options['tip'] ) ? '' : $options['tip'],
  264. 'wpid' => 0,
  265. 'menu' => true,
  266. 'optional' => true,
  267. );
  268. // Merge with defaults
  269. $options = ITUtility::merge_defaults( $options, $defaults );
  270. $pages[sanitize_title( $page )] = $options;
  271. $GLOBALS['it_exchange']['registered_pages'] = $pages;
  272. do_action( 'it_exchnage_register_page', $page, $options );
  273. return true;
  274. }
  275. /**
  276. * Returns a list of registerd pages
  277. *
  278. * This returns pages that are registered, with their defaults.
  279. * It DOES NOT RETURN THE ADMIN'S SETTINGS for those pages
  280. * For the admin's settings, use it_exchange_get_pages()
  281. *
  282. * @since 0.4.4
  283. * @return array
  284. */
  285. function it_exchange_get_registered_pages( $options=array() ) {
  286. $pages = empty( $GLOBALS['it_exchange']['registered_pages'] ) ? array() : (array) $GLOBALS['it_exchange']['registered_pages'];
  287. if ( ! empty( $options['type'] ) ) {
  288. foreach( $pages as $page => $page_options ) {
  289. if ( $options['type'] != it_exchange_get_page_type( $page ) )
  290. unset( $pages[$page] );
  291. }
  292. }
  293. return $pages;
  294. }
  295. /**
  296. * Returns an array of WP page IDs to page names
  297. *
  298. * @since 0.4.0
  299. *
  300. * @return array
  301. */
  302. function it_exchange_get_wp_pages( $options=array() ) {
  303. $defaults = array(
  304. 'post_type' => 'page',
  305. 'posts_per_page' => -1,
  306. 'order' => 'ASC',
  307. 'orderby' => 'title',
  308. );
  309. $options = ITUtility::merge_defaults( $options, $defaults );
  310. if ( ! $pages = get_posts( $options ) )
  311. $returnval = array();
  312. foreach( $pages as $page ) {
  313. $returnval[$page->ID] = get_the_title( $page->ID );
  314. }
  315. return apply_filters( 'it_exchange_get_wp_pages', $returnval, $options );
  316. }