PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/ArzuA/gitwordpress
PHP | 544 lines | 246 code | 77 blank | 221 comment | 55 complexity | ad584ac8d2190c76079f746d0409bc75 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * API Functions used to register / retrieve Exchange Add-ons
  4. *
  5. * @package IT_Exchange
  6. * @since 0.2.0
  7. */
  8. /**
  9. * Register an Add-on with iThemes Exchange
  10. *
  11. * Core ‘category’ options for type of add-on
  12. * - product-type Add-ons that create product types. eg: Digital, Membership
  13. * - transaction-method Add-ons that create transactions. eg: Stripe, Tweet
  14. * - admin General purpose admin functionality. eg: Reports, Export
  15. * - other Everything else
  16. *
  17. * @since 0.2.0
  18. *
  19. * @param string $slug string for identifying the add-on in code
  20. * @param array $params key / value pairs.
  21. */
  22. function it_exchange_register_addon( $slug, $params ) {
  23. $name = empty( $params['name'] ) ? false : $params['name'];
  24. $author = empty( $params['author'] ) ? false : $params['author'];
  25. $author_url = empty( $params['author_url'] ) ? false : $params['author_url'];
  26. $description = empty( $params['description'] ) ? '' : $params['description'];
  27. $file = empty( $params['file'] ) ? false : $params['file'];
  28. $options = empty( $params['options'] ) ? array() : (array) $params['options'];
  29. // Basic Validation
  30. $slug = empty( $slug ) ? false : sanitize_key( $slug );
  31. $name = empty( $name ) ? false : sanitize_text_field( $name );
  32. $author = empty( $author ) ? false : sanitize_text_field( $author );
  33. $file = file_exists( $file ) ? $file : false;
  34. if ( ! $slug )
  35. return new WP_Error( 'it_exchange_add_registration_error', __( 'All iThemes Exchange Add-ons require a slug parameter.', 'it-l10n-ithemes-exchange' ) );
  36. if ( ! $name )
  37. return new WP_Error( 'it_exchange_add_registration_error', __( 'All iThemes Exchange Add-ons require a name parameter.', 'it-l10n-ithemes-exchange' ) );
  38. if ( ! $file )
  39. return new WP_Error( 'it_exchange_add_registration_error', __( 'All iThemes Exchange Add-ons require a file parameter.', 'it-l10n-ithemes-exchange' ) );
  40. $allowed_keys = array( 'category', 'tag', 'supports', 'labels', 'settings-callback', 'icon', 'wizard-icon' );
  41. foreach ( $params as $key => $value )
  42. if ( in_array( $key, $allowed_keys ) )
  43. $options[$key] = $value;
  44. if ( empty( $options['category'] ) )
  45. $options['category'] = 'other';
  46. // Add the add-on to our Global
  47. $GLOBALS['it_exchange']['add_ons']['registered'][$slug] = apply_filters( 'it_exchange_register_addon', array(
  48. 'slug' => $slug,
  49. 'name' => $name,
  50. 'author' => $author,
  51. 'author_url' => $author_url,
  52. 'description' => $description,
  53. 'file' => $file,
  54. 'options' => $options,
  55. ) );
  56. }
  57. /**
  58. * Register an Add-on category with iThemes Exchange
  59. *
  60. * When registering an add-on category, you can set required/default support features that any add-on
  61. * in this category will be required to have. If add-ons register in this category without a key
  62. * they will be provided with a the value registered to the add-on category by default.
  63. * - eg: $options['supports'] = array( 'feature' => 'default_value' );
  64. *
  65. * @since 0.2.0
  66. *
  67. * @param string $slug var for identifying the add-on in code
  68. * @param string $name name of add-on used in UI
  69. * @param string $description description of the add-on
  70. * @param array $options key / value pairs.
  71. */
  72. function it_exchange_register_addon_category( $slug, $name, $description, $options = array() ) {
  73. // Basic Validation
  74. $slug = empty( $slug ) ? false : sanitize_key( $slug );
  75. $name = empty( $name ) ? false : sanitize_text_field( $name );
  76. $options['supports'] = empty( $options['supports'] ) ? array() : $options['supports'];
  77. if ( ! $slug )
  78. return new WP_Error( 'it_exchange_add_registration_error', __( 'All iThemes Exchange Add-on categories require a slug parameter.', 'it-l10n-ithemes-exchange' ) );
  79. if ( ! $name )
  80. return new WP_Error( 'it_exchange_add_registration_error', __( 'All iThemes Exchange Add-ons categories require a name parameter.', 'it-l10n-ithemes-exchange' ) );
  81. // Add the add-on to our Global
  82. $GLOBALS['it_exchange']['add_on_categories'][$slug] = apply_filters( 'it_exchange_register_addon_category', array(
  83. 'slug' => $slug,
  84. 'name' => $name,
  85. 'description' => $description,
  86. 'options' => $options,
  87. ) );
  88. }
  89. /**
  90. * Returns an array of registered add-ons
  91. *
  92. * @since 0.2.0
  93. *
  94. * @param array $options For filtering by category, use $options['category'] = array( 'cat1', 'cat2', 'etc' );
  95. * @return array registered add-ons
  96. */
  97. function it_exchange_get_addons( $options=array() ) {
  98. $defaults = array(
  99. 'show_required' => true,
  100. );
  101. $options = wp_parse_args( $options, $defaults );
  102. if ( empty( $GLOBALS['it_exchange']['add_ons']['registered'] ) )
  103. return array();
  104. else
  105. $add_ons = $GLOBALS['it_exchange']['add_ons']['registered'];
  106. // Loop through addons and filter out required if not set to show.
  107. foreach ( $add_ons as $key => $addon ) {
  108. if ( ! $options['show_required'] && ! empty( $addon['options']['tag'] ) && 'required' === $addon['options']['tag'] )
  109. unset( $add_ons[$key] );
  110. }
  111. // Possibly filter by category
  112. if ( ! empty( $options['category'] ) )
  113. $add_ons = it_exchange_filter_addons_by_category( $add_ons, $options['category'] );
  114. ksort( $add_ons );
  115. return apply_filters( 'it_exchange_get_addons', $add_ons, $options );
  116. }
  117. /**
  118. * We do not want to permanently enable all these addons, we just need to load them one time temporarily
  119. *
  120. * @since 0.4.5
  121. *
  122. * @param array $add_ons
  123. * @return void
  124. */
  125. function it_exchange_temporarily_load_addons( $add_ons ) {
  126. $enabled_addons = it_exchange_get_enabled_addons();
  127. // Init all enabled addons
  128. foreach( (array) $add_ons as $slug => $params ) {
  129. if( ! isset( $enabled_addons[$slug] ) ) {
  130. if ( ! empty( $params['file'] ) && is_file( $params['file'] ) ) {
  131. include_once( $params['file'] );
  132. }
  133. }
  134. }
  135. }
  136. /**
  137. * We do not want to permanently enable this addon, we just need to load it one time temporarily
  138. *
  139. * @since 0.4.5
  140. *
  141. * @param string $add_on slug
  142. * @return void
  143. */
  144. function it_exchange_temporarily_load_addon( $add_on ) {
  145. $enabled_addons = it_exchange_get_enabled_addons();
  146. // Init all enabled addons
  147. if( ! isset( $enabled_addons[$add_on] ) ) {
  148. if ( ! empty( $params['file'] ) && is_file( $params['file'] ) ) {
  149. include_once( $params['file'] );
  150. }
  151. }
  152. }
  153. /**
  154. * Returns a specific add-on by its slug
  155. *
  156. * @since 0.3.2
  157. *
  158. * @param string $slug the add-on's slug
  159. * @return array the add_on array
  160. */
  161. function it_exchange_get_addon( $slug ) {
  162. if ( $add_ons = it_exchange_get_addons() ) {
  163. if ( ! empty( $add_ons[$slug] ) )
  164. return $add_ons[$slug];
  165. }
  166. return apply_filters( 'it_exchange_get_addon', false, $slug, $add_ons );
  167. }
  168. /**
  169. * Returns an array of registered add-on categories
  170. *
  171. * @since 0.2.0
  172. *
  173. * @return array registered add-on categories
  174. */
  175. function it_exchange_get_addon_categories() {
  176. if ( empty( $GLOBALS['it_exchange']['add_on_categories'] ) )
  177. $categories = array() ;
  178. else
  179. $categories = $GLOBALS['it_exchange']['add_on_categories'];
  180. return apply_filters( 'it_exchange_get_addon_categories', $categories );
  181. }
  182. /**
  183. * Grabs list of currently enabled add-ons
  184. *
  185. * Can optionally filter by categories
  186. *
  187. * @since 0.3.0
  188. *
  189. * @param array $options For filtering by category, use $options['category'] = array( 'cat1', 'cat2', 'etc' );
  190. * @return array Enabled add-ons
  191. */
  192. function it_exchange_get_enabled_addons( $options=array() ) {
  193. $defaults = array(
  194. 'show_required' => true,
  195. 'break_cache' => false,
  196. );
  197. $options = wp_parse_args( $options, $defaults );
  198. // Grab all registered add-ons
  199. $registered = it_exchange_get_addons();
  200. $enabled = array();
  201. // Grab enabled add-ons from options
  202. if ( false === $enabled_addons = it_exchange_get_option( 'enabled_add_ons', $options['break_cache'] ) )
  203. $enabled_addons = array();
  204. // Set each enabled with registered params
  205. if ( ! empty( $enabled_addons ) ) {
  206. foreach ( $enabled_addons as $addon => $params ) {
  207. if ( ! empty( $registered[$addon] ) ) {
  208. if ( $options['show_required'] )
  209. $enabled[$addon] = $registered[$addon];
  210. else if ( empty( $registered[$addon]['options']['tag'] ) || 'required' !== $registered[$addon]['options']['tag'] )
  211. $enabled[$addon] = $registered[$addon];
  212. }
  213. }
  214. }
  215. if ( ! empty( $options['category'] ) )
  216. $enabled = it_exchange_filter_addons_by_category( $enabled, $options['category'] );
  217. ksort( $enabled );
  218. return apply_filters( 'it_exchange_get_enabled_addons', empty( $enabled ) ? array() : $enabled, $options );
  219. }
  220. /**
  221. * Grabs list of currently disabled add-ons
  222. *
  223. * Can optionally filter by categories
  224. *
  225. * @since 0.4.0
  226. *
  227. * @param array $options For filtering by category, use $options['category'] = array( 'cat1', 'cat2', 'etc' );
  228. * @return array Disabled add-ons
  229. */
  230. function it_exchange_get_disabled_addons( $options=array() ) {
  231. // Grab all registered add-ons
  232. $registered = it_exchange_get_addons();
  233. $disabled = array();
  234. // Grab enabled add-ons from options
  235. if ( false === $enabled_addons = it_exchange_get_option( 'enabled_add_ons' ) )
  236. $enabled_addons = array();
  237. // Loop through registered addons
  238. foreach ( $registered as $slug => $params )
  239. if ( ! in_array( $slug, array_keys( $enabled_addons ) ) )
  240. $disabled[$slug] = $params;
  241. if ( ! empty( $options['category'] ) )
  242. $disabled = it_exchange_filter_addons_by_category( $disabled, $options['category'] );
  243. if ( ! empty( $disabled ) )
  244. ksort( $disabled );
  245. return apply_filters( 'it_exchange_get_disabled_addons', empty( $disabled ) ? array() : $disabled, $options );
  246. }
  247. /**
  248. * Grabs list of currently available add-ons from iThemes
  249. *
  250. * Can optionally filter by categories
  251. *
  252. * @since 0.4.0
  253. *
  254. * @param array $options For filtering by category, use $options['category'] = array( 'cat1', 'cat2', 'etc' );
  255. * @return array All add-ons available from iThemes
  256. */
  257. function it_exchange_get_more_addons( $options=array() ) {
  258. // Grab all registered add-ons
  259. $remote_get = wp_remote_get( 'https://api.ithemes.com/exchange/addons/' );
  260. // Return empty array if remote_get errored out.
  261. if ( is_wp_error( $remote_get ) )
  262. return array();
  263. $addons = json_decode( $remote_get['body'], true );
  264. if ( ! empty( $options['category'] ) )
  265. $addons = it_exchange_filter_addons_by_category( $addons, $options['category'] );
  266. ksort( $addons );
  267. return apply_filters( 'it_exchange_get_more_addons', empty( $addons ) ? array() : $addons, $options );
  268. }
  269. /**
  270. * Resorts addon list from get_more_addons so featured add-ons are on top
  271. *
  272. * @since 0.4.0
  273. *
  274. * @param array $addons Current add-on array from it_exchange_get_more_addons()
  275. * @return array Restorted add-ons array
  276. */
  277. function it_exchange_featured_addons_on_top( $addons ) {
  278. $sorted_addons = array();
  279. foreach( $addons as $slug => $addon ) {
  280. if ( true === $addon['featured'] ) {
  281. $sorted_addons[$slug] = $addon;
  282. unset( $addons[$slug] );
  283. }
  284. }
  285. ksort( $sorted_addons );
  286. return apply_filters( 'it_exchange_get_more_addons', array_merge( $sorted_addons, $addons ), $addons );
  287. }
  288. /**
  289. * Takes an array of add-ons and filters by passed category
  290. *
  291. * @since 0.3.0
  292. *
  293. * @param array $add_ons an array of add-ons formatted like $GLOBALS['it_exchange']['add_ons'] array
  294. * @param array $categories contains categories we want filters: array( 'cat1', 'cat2', 'etc' );
  295. * @return array Filtered add-ons
  296. */
  297. function it_exchange_filter_addons_by_category( $add_ons, $categories ) {
  298. foreach( $add_ons as $slug => $params ) {
  299. if ( ! empty( $params['options']['category'] ) ) {
  300. if ( ! in_array( $params['options']['category'], (array) $categories ) )
  301. unset( $add_ons[$slug] );
  302. }
  303. }
  304. return apply_filters( 'it_exchange_filter_addons_by_category', $add_ons, $categories );
  305. }
  306. /**
  307. * Enable a registerd add_on
  308. *
  309. * @since 0.3.2
  310. *
  311. * @param string $add_on add_on to enable
  312. * @return bool
  313. */
  314. function it_exchange_enable_addon( $add_on ) {
  315. $registered = it_exchange_get_addons();
  316. $enabled_add_ons = it_exchange_get_enabled_addons( array( 'break_cache' => true ) );
  317. $success = false;
  318. if ( isset( $registered[$add_on] ) && ! isset( $enabled_add_ons[$add_on] ) ) {
  319. $enabled_add_ons[$add_on] = $registered[$add_on];
  320. if ( it_exchange_save_option( 'enabled_add_ons', $enabled_add_ons ) ) {
  321. include_once( $registered[$add_on]['file'] );
  322. do_action( 'it_exchange_add_on_enabled', $registered[$add_on] );
  323. update_option( '_it-exchange-flush-rewrites', true );
  324. $success = true;
  325. }
  326. }
  327. return apply_filters( 'it_exchange_enable_addon', $success, $add_on );
  328. }
  329. /**
  330. * Checks if an add-on is enabled
  331. *
  332. * @since 0.4.0
  333. *
  334. * @param string $add_on_slug add_on slug to check
  335. * @return boolean
  336. */
  337. function it_exchange_is_addon_enabled( $add_on_slug ) {
  338. $enabled = array_keys( it_exchange_get_enabled_addons( array( 'break_cache' => true ) ) );
  339. $success = false;
  340. if ( in_array( $add_on_slug, $enabled ) )
  341. $success = true;
  342. return apply_filters( 'it_exchange_is_addon_enabled', $success, $add_on_slug );
  343. }
  344. /**
  345. * Checks if an add-on is registered
  346. *
  347. * @since 0.4.0
  348. *
  349. * @param string $add_on add_on slug to check
  350. * @return bool
  351. */
  352. function it_exchange_is_addon_registered( $add_on_slug ) {
  353. $installed = it_exchange_get_addons();
  354. $success = false;
  355. if ( array_key_exists( $add_on_slug, $installed ) )
  356. $success = true;
  357. return apply_filters( 'it_exchange_is_addon_registered', $success, $add_on_slug );
  358. }
  359. /**
  360. * Disable a registerd add_on
  361. *
  362. * @since 0.3.2
  363. *
  364. * @param string $add_on add_on to disable
  365. * @return bool
  366. */
  367. function it_exchange_disable_addon( $add_on ) {
  368. $registered = it_exchange_get_addons();
  369. $enabled_addons = it_exchange_get_enabled_addons( array( 'break_cache' => true ) );
  370. $success = false;
  371. if ( ! empty( $enabled_addons[$add_on] ) ) {
  372. unset( $enabled_addons[$add_on] );
  373. if ( it_exchange_save_option( 'enabled_add_ons', $enabled_addons ) ) {
  374. if ( ! empty( $registered[$add_on] ) )
  375. do_action( 'it_exchange_add_on_disabled', $registered[$add_on] );
  376. update_option( '_it-exchange-flush-rewrites', true );
  377. $success = true;
  378. }
  379. }
  380. return $success;
  381. }
  382. /**
  383. * Does the given add-on support a specific feature?
  384. *
  385. * @since 0.3.3
  386. *
  387. * @param string $add_on add_on slug
  388. * @param string $feature type of feature we are testing for support
  389. * @return bool
  390. */
  391. function it_exchange_addon_supports( $add_on, $feature ) {
  392. $add_ons = it_exchange_get_addons();
  393. // Return false if add-on is not registered
  394. if ( ! isset( $add_ons[$add_on] ) )
  395. return false;
  396. // Return false if feature is not supported
  397. if ( empty( $add_ons[$add_on]['options']['supports'][$feature] ) )
  398. return false;
  399. return true;
  400. }
  401. /**
  402. * Add's add-on support for a specific feature
  403. *
  404. * @since 0.3.3
  405. *
  406. * @param string $add_on the slug for the add-on being targeted
  407. * @param string $feature the feature slug that needs to be enabled
  408. * @return void
  409. */
  410. function it_exchange_add_addon_support( $add_on, $feature ) {
  411. $add_ons = it_exchange_get_addons();
  412. // Return false if add-on is not registered
  413. if ( ! isset( $add_ons[$add_on] ) )
  414. return false;
  415. // Set add-on support to true for this add-on / feature combo
  416. if ( empty( $add_ons[$add_on]['options'] ) )
  417. $GLOBALS['it_exchange']['add_ons']['registered'][$add_on]['options']['supports'][$feature] = true;
  418. }
  419. /**
  420. * Remove's add-on support for a specific feature
  421. *
  422. * @since 0.3.3
  423. *
  424. * @param string $add_on the slug for the add-on being targeted
  425. * @param string $feature the feature slug that needs to be enabled
  426. * @return void
  427. */
  428. function it_exchange_remove_addon_support( $add_on, $feature ) {
  429. $add_ons = it_exchange_get_addons();
  430. // Return false if add-on is not registered
  431. if ( ! isset( $add_ons[$add_on] ) )
  432. return false;
  433. // Set add-on support to false for this add-on / feature combo
  434. if ( empty( $add_ons[$add_on]['options'] ) )
  435. $GLOBALS['it_exchange']['add_ons']['registered'][$add_on]['options']['supports'][$feature] = false;
  436. }
  437. /**
  438. * Return the default values for an add-on support key
  439. *
  440. * @since 0.3.3
  441. *
  442. * @param string $add_on the slug for the add-on being targeted
  443. * @param string $feature the feature the slug is targeting
  444. * @return mixed the value of the key
  445. */
  446. function it_exchange_get_addon_support( $add_on, $feature ) {
  447. $add_ons = it_exchange_get_addons();
  448. // Return false if feature isn't recorded
  449. if ( empty( $add_ons[$add_on]['options']['supports'][$feature] ) )
  450. return false;
  451. return apply_filters( 'it_exchange_get_addon_support', $add_ons[$add_on]['options']['supports'][$feature], $add_on, $feature );
  452. }
  453. /**
  454. * Returns true if addon is in ithemes-exchange/core-addons directory
  455. *
  456. * @since 0.4.0
  457. *
  458. * @param string $slug the addon slug
  459. * @return boolean
  460. */
  461. function it_exchange_is_core_addon( $slug ) {
  462. $addon = it_exchange_get_addon( $slug );
  463. if ( empty( $addon['file'] ) )
  464. return false;
  465. // Don't add a filter here.
  466. return ( 'ithemes-exchange/core-addons/' == substr( plugin_basename( $addon['file'] ), 0, 29 ) );
  467. }