PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/www/wp-content/plugins/ithemes-exchange/lib/admin/class.admin.php

https://github.com/ArzuA/gitwordpress
PHP | 1915 lines | 1205 code | 221 blank | 489 comment | 265 complexity | 1ecfd93288f0de1f77aae400363241b7 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * iThemes Exchange admin class.
  4. *
  5. * This class manages the admin side of the plugin
  6. *
  7. * @package IT_Exchange
  8. * @since 0.1.0
  9. */
  10. class IT_Exchange_Admin {
  11. /**
  12. * @var object $_parent parent class
  13. * @since 0.1.0
  14. */
  15. var $_parent;
  16. /**
  17. * @var string $_current_page current page based on $_GET['page']
  18. * @since 0.3.4
  19. */
  20. var $_current_page;
  21. /**
  22. * @var string $_current_tab
  23. * @since 0.3.4
  24. */
  25. var $_current_tab;
  26. /**
  27. * @var string $status_message informative message for current settings tab
  28. * @since 0.3.6
  29. */
  30. var $status_message;
  31. /**
  32. * @var string $error_message error message for current settings tab
  33. * @since 0.3.6
  34. */
  35. var $error_message;
  36. /**
  37. * Class constructor
  38. *
  39. * @uses add_action()
  40. * @since 0.1.0
  41. * @return void
  42. */
  43. function IT_Exchange_Admin( &$parent ) {
  44. // Set parent property
  45. $this->_parent = $parent;
  46. // Admin Menu Capability
  47. $this->admin_menu_capability = apply_filters( 'it_exchange_admin_menu_capability', 'activate_plugins' );
  48. // Set current properties
  49. $this->set_current_properties();
  50. // Open iThemes Exchange menu when on add/edit iThemes Exchange product post type
  51. add_action( 'parent_file', array( $this, 'open_exchange_menu_on_post_type_views' ) );
  52. // Add actions for iThemes registration
  53. add_action( 'admin_notices', array( $this, 'add_wizard_nag' ) );
  54. add_action( 'admin_menu', array( $this, 'add_exchange_admin_menu' ) );
  55. add_action( 'admin_menu', array( $this, 'add_lower_priority_items_to_exchange_menu' ), 90 );
  56. add_action( 'admin_init', array( $this, 'enable_disable_registered_add_on' ) );
  57. add_action( 'admin_init', array( $this, 'enable_required_add_ons' ) );
  58. add_filter( 'admin_body_class', array( $this, 'add_exchange_class_to_exchange_pages' ) );
  59. add_filter( 'upload_mimes', array( $this, 'uploads_mimes_for_products' ) );
  60. add_filter( 'wp_ajax_ite-country-state-update', array( $this, 'update_country_state_ui_in_general_settings' ) );
  61. // Admin Product Redirects
  62. add_action( 'admin_init', array( $this, 'redirect_post_new_to_product_type_selection_screen' ) );
  63. add_action( 'admin_init', array( $this, 'bounce_user_to_all_products_if_directly_accessing_disabled_product_type' ) );
  64. // Init our custom add/edit layout interface
  65. add_action( 'admin_enqueue_scripts', array( $this, 'it_exchange_admin_wp_enqueue_scripts' ), 9 );
  66. add_action( 'admin_print_styles', array( $this, 'it_exchange_admin_wp_enqueue_styles' ) );
  67. add_action( 'admin_init', array( $this, 'remove_third_party_metaboxes' ) );
  68. add_action( 'admin_init', array( $this, 'setup_add_edit_product_screen_layout' ) );
  69. // Force 2 column view on add / edit products
  70. add_filter( 'screen_layout_columns', array( $this, 'modify_add_edit_page_layout' ) );
  71. add_filter( 'get_user_option_screen_layout_it_exchange_prod', array( $this, 'update_user_column_options' ) );
  72. // Save core settings
  73. add_action( 'admin_init', array( $this, 'save_core_wizard_settings' ), 9 );
  74. add_action( 'admin_init', array( $this, 'save_core_general_settings' ) );
  75. add_action( 'admin_init', array( $this, 'save_core_email_settings' ) );
  76. add_action( 'admin_init', array( $this, 'save_core_page_settings' ), 9 ); // Priority 9 to catch product rewrites
  77. // Email settings callback
  78. add_filter( 'it_exchange_general_settings_tab_callback_email', array( $this, 'register_email_settings_tab_callback' ) );
  79. add_action( 'it_exchange_print_general_settings_tab_links', array( $this, 'print_email_settings_tab_link' ) );
  80. // Page settings callback
  81. add_filter( 'it_exchange_general_settings_tab_callback_pages', array( $this, 'register_pages_settings_tab_callback' ) );
  82. add_action( 'it_exchange_print_general_settings_tab_links', array( $this, 'print_pages_settings_tab_link' ) );
  83. // General Settings Defaults
  84. add_filter( 'it_storage_get_defaults_exchange_settings_general', array( $this, 'set_general_settings_defaults' ) );
  85. // Email Settings Defaults
  86. add_filter( 'it_storage_get_defaults_exchange_settings_email', array( $this, 'set_email_settings_defaults' ) );
  87. // Page Settings Defaults
  88. add_filter( 'it_storage_get_defaults_exchange_settings_pages', array( $this, 'set_pages_settings_defaults' ) );
  89. // Add-on Page Filters
  90. add_action( 'it_exchange_print_add_ons_page_tab_links', array( $this, 'print_enabled_add_ons_tab_link' ) );
  91. add_action( 'it_exchange_print_add_ons_page_tab_links', array( $this, 'print_disabled_add_ons_tab_link' ) );
  92. add_filter( 'it_exchange_add_ons_tab_callback_get-more', array( $this, 'register_get_more_add_ons_tab_callback' ) );
  93. add_action( 'it_exchange_print_add_ons_page_tab_links', array( $this, 'print_get_more_add_ons_tab_link' ) );
  94. // Update existing nav menu post_type entries when permalink structure is changed
  95. add_action( 'update_option_permalink_structure', array( $this, 'maybe_update_ghost_pages_in_wp_nav_menus' ) );
  96. // Remove Quick Edit
  97. add_filter( 'post_row_actions', array( $this, 'it_exchange_remove_quick_edit' ), 10, 2 );
  98. add_filter( 'page_row_actions', array( $this, 'it_exchange_remove_quick_edit' ), 10, 2 );
  99. // User Edit
  100. add_filter( 'user_row_actions', array( $this, 'it_exchange_user_row_actions' ), 10, 2 );
  101. add_action( 'all_admin_notices', array( $this, 'it_exchange_user_edit_load' ) );
  102. add_action( 'show_user_profile', array( $this, 'it_exchange_user_profile' ) );
  103. add_action( 'edit_user_profile', array( $this, 'it_exchange_user_profile' ) );
  104. add_action( 'it_exchange_addon_settings_page_top', array( $this, 'return_to_addons' ) );
  105. add_filter( 'plugin_action_links_ithemes-exchange/init.php', array( $this, 'it_exchange_plugin_row_actions' ), 10, 4 );
  106. add_filter( 'plugin_row_meta', array( $this, 'it_exchange_plugin_row_meta' ), 10, 4 );
  107. }
  108. /**
  109. * Adds actions to the plugins page for the iThemes Exchange plugin
  110. *
  111. * @since 0.4.15
  112. *
  113. * @param array $meta Existing meta
  114. * @param string $plugin_file the wp plugin slug (path)
  115. * @param array $plugin_data the data WP harvested from the plugin header
  116. * @param string $context
  117. * @return array
  118. */
  119. function it_exchange_plugin_row_actions( $actions, $plugin_file, $plugin_data, $context ) {
  120. $actions['quick_setup'] = '<a href="' . get_admin_url( NULL, 'admin.php?page=it-exchange-setup' ) . '">' . __( 'Quick Setup', 'it-l10n-ithemes-exchange' ) . '</a>';
  121. return $actions;
  122. }
  123. /**
  124. * Adds links to the plugin row meta
  125. *
  126. * @since 0.4.18
  127. *
  128. * @param array $meta Existing meta
  129. * @param string $plugin_file the wp plugin slug (path)
  130. * @param array $plugin_data the data WP harvested from the plugin header
  131. * @param string $status the plugin activation status
  132. * @return array
  133. */
  134. function it_exchange_plugin_row_meta( $meta, $plugin_file, $plugin_data, $status ) {
  135. if ( 'ithemes-exchange/init.php' == $plugin_file ) {
  136. $meta[] = '<a href="http://ithemes.com/codex/page/Exchange" target="_blank">' . __( 'Documentation', 'it-l10n-ithemes-exchange' ) . '</a>';
  137. $meta[] = '<a href="http://ithemes.com/exchange/support" target="_blank">' . __( 'Support', 'it-l10n-ithemes-exchange' ) . '</a>';
  138. $meta[] = '<a href="http://ithemes.com/exchange/add-ons" target="_blank">' . __( 'Add-ons', 'it-l10n-ithemes-exchange' ) . '</a>';
  139. $meta[] = '<a href="http://ithemes.com/codex/page/Exchange_Changelog" target="_blank">' . __( 'Changelog', 'it-l10n-ithemes-exchange' ) . '</a>';
  140. }
  141. return $meta;
  142. }
  143. /**
  144. * Adds link to bottom of addons settings pages to return to the addons pages
  145. *
  146. * @since 0.4.5
  147. * @return void
  148. */
  149. function return_to_addons() {
  150. if ( ! empty( $GLOBALS['hook_suffix'] ) && 'exchange_page_it-exchange-addons' == $GLOBALS['hook_suffix'] ) { //only show on add-on-settings pages
  151. $url = add_query_arg( 'page', 'it-exchange-addons', admin_url( 'admin.php' ) );
  152. echo '<div class="it-exchange-return-to-addons"><p><a href="' . $url . '">&larr; ' . __( 'Back to Add-ons', 'it-l10n-ithemes-exchange' ) . '</a></p></div>';
  153. }
  154. }
  155. /**
  156. * Adds iThemes Exchange User row action to users.php row actions
  157. *
  158. * @since 0.4.0
  159. * @return void
  160. */
  161. function it_exchange_user_row_actions( $actions, $user_object ) {
  162. $actions['it_exchange'] = "<a class='it-exchange-cust-info' href='" . esc_url( add_query_arg( array( 'wp_http_referer' => urlencode( stripslashes_deep( $_SERVER['REQUEST_URI'] ) ), 'it_exchange_customer_data' => 1 ), get_edit_user_link( $user_object->ID ) ) ) . "'>" . __( 'Customer Data', 'it-l10n-ithemes-exchange' ) . "</a>";
  163. return $actions;
  164. }
  165. /**
  166. * Adds iThemes Exchange User Meta page to user-edit.php
  167. *
  168. * @since 0.4.0
  169. * @return void
  170. */
  171. function it_exchange_user_profile( $profileuser ) {
  172. if ( current_user_can('edit_users') )
  173. include( 'views/admin-user-profile.php' );
  174. }
  175. /**
  176. * Adds iThemes Exchange User Meta page to user-edit.php
  177. *
  178. * @since 0.4.0
  179. * @return void
  180. */
  181. function it_exchange_user_edit_load() {
  182. //A little hacky
  183. global $pagenow;
  184. if ( in_array( $pagenow, array( 'user-edit.php', 'profile.php' ) )
  185. && !empty( $_REQUEST['it_exchange_customer_data'] ) && current_user_can('edit_users') ) {
  186. add_action( 'it_exchange_print_user_edit_page_tab_links', array( $this, 'print_products_user_edit_tab_link' ) );
  187. add_action( 'it_exchange_print_user_edit_page_tab_links', array( $this, 'print_transactions_user_edit_tab_link' ) );
  188. add_action( 'it_exchange_print_user_edit_page_tab_links', array( $this, 'print_info_user_edit_tab_link' ) );
  189. include( 'views/admin-user-edit.php' );
  190. include( ABSPATH . 'wp-admin/admin-footer.php');
  191. die();
  192. }
  193. }
  194. /**
  195. * Save iThemes Exchange User Meta Options to user-edit.php
  196. *
  197. * @since 0.4.0
  198. * @param int $user_id User ID of meta we're saving
  199. * @return void
  200. */
  201. function it_exchange_edit_user_profile_update( $user_id ) {
  202. if ( isset( $_REQUEST['it_exchange_customer_note'] ) )
  203. update_user_meta( $user_id, '_it_exchange_customer_note', $_REQUEST['it_exchange_customer_note'] );
  204. }
  205. /**
  206. * Prints the tabs for the iThemes Exchange Add-ons Page
  207. *
  208. * @since 0.4.0
  209. * @return void
  210. */
  211. function print_user_edit_page_tabs() {
  212. ?>
  213. <h2 class="nav-tab-wrapper">
  214. <?php do_action( 'it_exchange_print_user_edit_page_tab_links', $this->_current_tab ); ?>
  215. </h2>
  216. <?php
  217. }
  218. /**
  219. * Prints the products tab for the user-edit.php Page
  220. *
  221. * @since 0.4.0
  222. * @return void
  223. */
  224. function print_products_user_edit_tab_link( $current_tab ) {
  225. $active = ( 'products' === $current_tab || false === $current_tab ) ? 'nav-tab-active' : '';
  226. ?><a class="nav-tab <?php echo $active; ?>" href="<?php echo add_query_arg( 'tab', 'products' ); ?>#it-exchange-member-options"><?php _e( 'Products', 'it-l10n-ithemes-exchange' ); ?></a><?php
  227. }
  228. /**
  229. * Prints the transactions tab for the user-edit.php Page
  230. *
  231. * @since 0.4.0
  232. * @return void
  233. */
  234. function print_transactions_user_edit_tab_link( $current_tab ) {
  235. $active = 'transactions' == $current_tab ? 'nav-tab-active' : '';
  236. ?><a class="nav-tab <?php echo $active; ?>" href="<?php echo add_query_arg( 'tab', 'transactions' ); ?>#it-exchange-member-options"><?php _e( 'Transactions', 'it-l10n-ithemes-exchange' ); ?></a><?php
  237. }
  238. /**
  239. * Prints the info tab for the user-edit.php Page
  240. *
  241. * @since 0.4.0
  242. * @return void
  243. */
  244. function print_info_user_edit_tab_link( $current_tab ) {
  245. $active = ( 'info' === $current_tab ) ? 'nav-tab-active' : '';
  246. ?><a class="nav-tab <?php echo $active; ?>" href="<?php echo add_query_arg( 'tab', 'info' ); ?>#it-exchange-member-options"><?php _e( 'Info', 'it-l10n-ithemes-exchange' ); ?></a><?php
  247. }
  248. /**
  249. * Sets the _current_page and _current_tab properties
  250. *
  251. * @since 0.3.4
  252. * @return void
  253. */
  254. function set_current_properties() {
  255. $this->_current_page = empty( $_GET['page'] ) ? false : $_GET['page'];
  256. $this->_current_tab = empty( $_GET['tab'] ) ? false : $_GET['tab'];
  257. }
  258. /**
  259. * Adds the nag to the top of the admin screens if not complete
  260. *
  261. * @since 0.4.0
  262. */
  263. function add_wizard_nag() {
  264. if ( ! empty( $_REQUEST['it_exchange_settings-dismiss-wizard-nag'] ) )
  265. update_option( 'it-exchange-hide-wizard-nag', true );
  266. if ( isset( $_GET['it-exchange-show-wizard-link'] ) )
  267. delete_option( 'it-exchange-hide-wizard-nag' );
  268. if ( true == (boolean) get_option( 'it-exchange-hide-wizard-nag' ) )
  269. return;
  270. if ( 'it-exchange-setup' != $this->_current_page )
  271. include( 'views/admin-wizard-notice.php' );
  272. }
  273. /**
  274. * Adds the main iThemes Exchange menu item to the WP admin menu
  275. *
  276. * @since 0.2.0
  277. * @return void
  278. */
  279. function add_exchange_admin_menu() {
  280. // Add main iThemes Exchange menu item
  281. add_menu_page( 'iThemes Exchange', 'Exchange', $this->admin_menu_capability, 'it-exchange', array( $this, 'print_exchange_setup_page' ) );
  282. // Add setup wizard page without menu item unless we're viewing it.
  283. if ( 'it-exchange-setup' == $this->_current_page )
  284. add_submenu_page( 'it-exchange', 'iThemes Exchange Setup Wizard', 'Setup Wizard', $this->admin_menu_capability, 'it-exchange-setup', array( $this, 'print_exchange_setup_page' ) );
  285. // Add the product submenu pages depending on active product add-ons
  286. $this->add_product_submenus();
  287. // Add Transactions menu item
  288. add_submenu_page( 'it-exchange', 'iThemes Exchange ' . __( 'Payments', 'it-l10n-ithemes-exchange' ), __( 'Payments', 'it-l10n-ithemes-exchange' ), $this->admin_menu_capability, 'edit.php?post_type=it_exchange_tran' );
  289. // Remove default iThemes Exchange sub-menu item created with parent menu item
  290. remove_submenu_page( 'it-exchange', 'it-exchange' );
  291. }
  292. /**
  293. * Adds the help menu at the bottom of the menu
  294. *
  295. * @since 0.4.17
  296. *
  297. * @return void
  298. */
  299. function add_lower_priority_items_to_exchange_menu() {
  300. // Add Settings Menu Item
  301. $settings_callback = array( $this, 'print_exchange_settings_page' );
  302. if ( 'it-exchange-settings' == $this->_current_page && ! empty( $this->_current_tab ) )
  303. $settings_callback = apply_filters( 'it_exchange_general_settings_tab_callback_' . $this->_current_tab, $settings_callback );
  304. add_submenu_page( 'it-exchange', 'iThemes Exchange Settings', 'Settings', $this->admin_menu_capability, 'it-exchange-settings', $settings_callback );
  305. // Add Add-ons menu item
  306. $add_ons_callback = array( $this, 'print_exchange_add_ons_page' );
  307. if ( 'it-exchange-addons' == $this->_current_page && ! empty( $this->_current_tab ) ) {
  308. $add_ons_callback = apply_filters( 'it_exchange_add_ons_tab_callback_' . $this->_current_tab, $add_ons_callback );
  309. }
  310. if ( !empty( $_GET['add-on-settings'] ) && $addon = it_exchange_get_addon( $_GET['add-on-settings'] ) ) {
  311. if ( ! empty( $addon['options']['settings-callback'] ) && is_callable( $addon['options']['settings-callback'] ) )
  312. $add_ons_callback = $addon['options']['settings-callback'];
  313. }
  314. add_submenu_page( 'it-exchange', 'iThemes Exchange Add-ons', 'Add-ons', $this->admin_menu_capability, 'it-exchange-addons', $add_ons_callback );
  315. // Help menu
  316. add_submenu_page( 'it-exchange', __( 'Help', 'it-l10n-ithemes-exchange' ), __( 'Help', 'it-l10n-ithemes-exchange' ), $this->admin_menu_capability, 'it-exchange-help', array( $this, 'print_help_page' ) );
  317. }
  318. /**
  319. * Adds the product submenus based on number of enabled product-type add-ons
  320. *
  321. * @since 0.3.0
  322. * @return void
  323. */
  324. function add_product_submenus() {
  325. // Check for enabled product add-ons. Don't need product pages if we don't have product add-ons enabled
  326. if ( $enabled_product_types = it_exchange_get_enabled_addons( array( 'category' => array( 'product-type' ) ) ) ) {
  327. $add_on_count = count( $enabled_product_types );
  328. add_submenu_page( 'it-exchange', 'All Products', 'All Products', $this->admin_menu_capability, 'edit.php?post_type=it_exchange_prod' );
  329. if ( 1 == $add_on_count ) {
  330. // If we only have one product-type enabled, add standard post_type pages
  331. $product = reset( $enabled_product_types );
  332. add_submenu_page( 'it-exchange', 'Add Product', 'Add Product', $this->admin_menu_capability, 'post-new.php?post_type=it_exchange_prod&it-exchange-product-type=' . $product['slug'] );
  333. } else if ( $add_on_count > 1 ) {
  334. // If we have more than one product type, add them each separately
  335. foreach( $enabled_product_types as $type => $params ) {
  336. $name = empty( $params['options']['labels']['singular_name'] ) ? 'Product' : esc_attr( $params['options']['labels']['singular_name'] );
  337. add_submenu_page( 'it-exchange', 'Add ' . $name, 'Add ' . $name, $this->admin_menu_capability, 'post-new.php?post_type=it_exchange_prod&it-exchange-product-type=' . esc_attr( $params['slug'] ) );
  338. }
  339. }
  340. }
  341. }
  342. /**
  343. * Registers the callback for the email tab
  344. *
  345. * @param mixed default callback for general settings.
  346. * @since 0.3.4
  347. * @return mixed function or class method name
  348. */
  349. function register_email_settings_tab_callback( $default ) {
  350. return array( $this, 'print_email_settings_page' );
  351. }
  352. /**
  353. * Prints the email tab for general settings
  354. *
  355. * @since 0.3.4
  356. * @param $current_tab the current tab
  357. * @return void
  358. */
  359. function print_email_settings_tab_link( $current_tab ) {
  360. $active = 'email' == $current_tab ? 'nav-tab-active' : '';
  361. ?><a class="nav-tab <?php echo $active; ?>" href="<?php echo admin_url( 'admin.php?page=it-exchange-settings&tab=email' ); ?>"><?php _e( 'Email Settings', 'it-l10n-ithemes-exchange' ); ?></a><?php
  362. }
  363. /**
  364. * Registers the callback for the pages tab
  365. *
  366. * @param mixed default callback for general settings.
  367. * @since 0.3.7
  368. * @return mixed function or class method name
  369. */
  370. function register_pages_settings_tab_callback( $default ) {
  371. return array( $this, 'print_pages_settings_page' );
  372. }
  373. /**
  374. * Prints the pages tab for general settings
  375. *
  376. * @since 0.3.7
  377. * @param $current_tab the current tab
  378. * @return void
  379. */
  380. function print_pages_settings_tab_link( $current_tab ) {
  381. $active = 'pages' == $current_tab ? 'nav-tab-active' : '';
  382. ?><a class="nav-tab <?php echo $active; ?>" href="<?php echo admin_url( 'admin.php?page=it-exchange-settings&tab=pages' ); ?>"><?php _e( 'Pages', 'it-l10n-ithemes-exchange' ); ?></a><?php
  383. }
  384. /**
  385. * Prints the tabs for the iThemes Exchange General Settings
  386. *
  387. * @since 0.3.4
  388. * @return void
  389. */
  390. function print_general_settings_tabs() {
  391. $active = empty( $this->_current_tab ) ? 'nav-tab-active' : '';
  392. ?>
  393. <h2 class="nav-tab-wrapper">
  394. <a class="nav-tab <?php echo $active; ?>" href="<?php echo admin_url( 'admin.php?page=it-exchange-settings' ); ?>"><?php _e( 'General', 'it-l10n-ithemes-exchange' ); ?></a>
  395. <?php do_action( 'it_exchange_print_general_settings_tab_links', $this->_current_tab ); ?>
  396. </h2>
  397. <?php
  398. }
  399. /**
  400. * Prints the tabs for the iThemes Exchange Add-ons Page
  401. *
  402. * @since 0.4.0
  403. * @return void
  404. */
  405. function print_add_ons_page_tabs() {
  406. $active = ( empty( $this->_current_tab ) || 'all' == $this->_current_tab ) ? 'nav-tab-active' : '';
  407. ?>
  408. <h2 class="nav-tab-wrapper">
  409. <a class="nav-tab <?php echo $active; ?>" href="<?php echo admin_url( 'admin.php?page=it-exchange-addons' ); ?>"><?php _e( 'All', 'it-l10n-ithemes-exchange' ); ?></a>
  410. <?php do_action( 'it_exchange_print_add_ons_page_tab_links', $this->_current_tab ); ?>
  411. </h2>
  412. <?php
  413. }
  414. /**
  415. * Prints the enabled tab for the Add-ons Page
  416. *
  417. * @since 0.4.0
  418. * @return void
  419. */
  420. function print_enabled_add_ons_tab_link( $current_tab ) {
  421. $active = 'enabled' == $current_tab ? 'nav-tab-active' : '';
  422. ?><a class="nav-tab <?php echo $active; ?>" href="<?php echo admin_url( 'admin.php?page=it-exchange-addons&tab=enabled' ); ?>"><?php _e( 'Enabled', 'it-l10n-ithemes-exchange' ); ?></a><?php
  423. }
  424. /**
  425. * Prints the disabled tab for the Add-ons Page
  426. *
  427. * @since 0.4.0
  428. * @return void
  429. */
  430. function print_disabled_add_ons_tab_link( $current_tab ) {
  431. $active = 'disabled' == $current_tab ? 'nav-tab-active' : '';
  432. ?><a class="nav-tab <?php echo $active; ?>" href="<?php echo admin_url( 'admin.php?page=it-exchange-addons&tab=disabled' ); ?>"><?php _e( 'Disabled', 'it-l10n-ithemes-exchange' ); ?></a><?php
  433. }
  434. /**
  435. * Registers the callback for the get more add-ons tab
  436. *
  437. * @param mixed default callback for add-ons page.
  438. * @since 0.4.0
  439. * @return mixed function or class method name
  440. */
  441. function register_get_more_add_ons_tab_callback( $default ) {
  442. return array( $this, 'print_get_more_add_ons_page' );
  443. }
  444. /**
  445. * Prints the enabled add ons page for iThemes Exchange
  446. *
  447. * @since 0.4.0
  448. * @return void
  449. */
  450. function print_get_more_add_ons_page() {
  451. $add_on_cats = it_exchange_get_addon_categories();
  452. $message = empty( $_GET['message'] ) ? false : $_GET['message'];
  453. if ( 'installed' == $message )
  454. ITUtility::show_status_message( __( 'Add-on installed.', 'it-l10n-ithemes-exchange' ) );
  455. $error = empty( $_GET['error'] ) ? false : $_GET['error'];
  456. if ( 'installed' == $error )
  457. ITUtility::show_error_message( __( 'Error: Add-on not installed.', 'it-l10n-ithemes-exchange' ) );
  458. include( 'views/admin-get-more-addons.php' );
  459. }
  460. /**
  461. * Prints the Get More tab for the Add-ons Page
  462. *
  463. * @since 0.4.0
  464. * @return void
  465. */
  466. function print_get_more_add_ons_tab_link( $current_tab ) {
  467. $active = 'get-more' == $current_tab ? 'nav-tab-active' : '';
  468. ?><a class="nav-tab <?php echo $active; ?>" href="<?php echo admin_url( 'admin.php?page=it-exchange-addons&tab=get-more' ); ?>"><?php _e( 'Get More', 'it-l10n-ithemes-exchange' ); ?></a><?php
  469. }
  470. /**
  471. * Prints the help/support page for iThemes Exchange
  472. *
  473. * @since 0.4.14
  474. * @return void
  475. */
  476. function print_help_page() {
  477. include( 'views/admin-help.php' );
  478. }
  479. /**
  480. * Prints the setup page for iThemes Exchange
  481. *
  482. * @since 0.2.0
  483. * @return void
  484. */
  485. function print_exchange_setup_page() {
  486. $flush_cache = ! empty( $_POST );
  487. $settings = it_exchange_get_option( 'settings_general', $flush_cache );
  488. $form_values = empty( $this->error_message ) ? $settings : ITForm::get_post_data();
  489. $form = new ITForm( $form_values, array( 'prefix' => 'it_exchange_settings' ) );
  490. $form_options = array(
  491. 'id' => apply_filters( 'it_exchange_settings_form_id', 'it-exchange-settings' ),
  492. 'enctype' => apply_filters( 'it_exchange_settings_form_enctype', false ),
  493. );
  494. if ( $messages = it_exchange_get_messages( 'notice' ) ) {
  495. foreach( $messages as $notice ) {
  496. ITUtility::show_status_message( $notice );
  497. }
  498. it_exchange_clear_messages( 'notice' );
  499. }
  500. if ( $messages = it_exchange_get_messages( 'error' ) ) {
  501. foreach( $messages as $error ) {
  502. ITUtility::show_error_message( $error );
  503. }
  504. it_exchange_clear_messages( 'error' );
  505. }
  506. include( 'views/admin-wizard.php' );
  507. }
  508. /**
  509. * Sets the general settings default values
  510. *
  511. * @since 0.3.7
  512. * @return array
  513. */
  514. function set_general_settings_defaults( $values ) {
  515. $defaults = array(
  516. 'company-base-country' => 'US',
  517. 'company-base-state' => 'OK',
  518. 'store-product-order-by' => 'date',
  519. 'store-product-order' => 'DESC',
  520. 'default-currency' => 'USD',
  521. 'currency-symbol-position' => 'before',
  522. 'currency-thousands-separator' => ',',
  523. 'currency-decimals-separator' => '.',
  524. 'site-registration' => 'it',
  525. 'wysiwyg-for-product-description' => '0',
  526. 'enable-gallery-popup' => '1',
  527. 'enable-gallery-zoom' => '1',
  528. 'product-gallery-zoom-action' => 'click',
  529. 'checkout-reg-form' => 'registration',
  530. 'company-email' => get_bloginfo( 'admin_email' ),
  531. 'company-name' => wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ),
  532. 'customer-account-page' => 'Welcome to your account, [it_exchange_customer show="first-name"].<br />Using the links above, you can edit your profile and view your purchases and products.<br />Thanks for being a customer.'
  533. );
  534. $values = ITUtility::merge_defaults( $values, $defaults );
  535. return $values;
  536. }
  537. /**
  538. * Sets the email settings default values
  539. *
  540. * static so we can access it from email_notifications w/o loading entire class
  541. *
  542. * @since 0.4.0
  543. * @return array
  544. */
  545. static function set_email_settings_defaults( $values ) {
  546. $defaults = array(
  547. 'notification-email-address' => esc_attr( get_bloginfo( 'admin_email' ) ),
  548. 'admin-email-address' => esc_attr( get_bloginfo( 'admin_email' ) ),
  549. 'admin-email-name' => wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ),
  550. 'admin-email-subject' => sprintf( __( 'You made a sale! Yabba Dabba Doo! %s', 'it-l10n-ithemes-exchange' ), '[it_exchange_email show=receipt_id]' ),
  551. 'admin-email-template' => sprintf( __( "Your friend %s just bought all this awesomeness from your store!
  552. Order: %s
  553. %s", 'it-l10n-ithemes-exchange' ), '[it_exchange_email show=fullname]', '[it_exchange_email show=receipt_id]', '[it_exchange_email show=order_table]' ),
  554. 'receipt-email-address' => esc_attr( get_bloginfo( 'admin_email' ) ),
  555. 'receipt-email-name' => wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ),
  556. 'receipt-email-subject' => sprintf( __( 'Receipt for Purchase: %s', 'it-l10n-ithemes-exchange' ), '[it_exchange_email show=receipt_id]' ),
  557. 'receipt-email-template' => sprintf( __( "Hello %s,
  558. Thank you for your order. Your order's details are below.
  559. Order: %s
  560. %s
  561. %s", 'it-l10n-ithemes-exchange' ), '[it_exchange_email show=name]', '[it_exchange_email show=receipt_id]', '[it_exchange_email show=order_table options=purchase_message]', '[it_exchange_email show=download_list]' ),
  562. );
  563. $values = ITUtility::merge_defaults( $values, $defaults );
  564. return $values;
  565. }
  566. /**
  567. * Prints the settings page for iThemes Exchange
  568. *
  569. * @since 0.3.4
  570. * @return void
  571. */
  572. function print_exchange_settings_page() {
  573. $flush_cache = ! empty( $_POST );
  574. $settings = it_exchange_get_option( 'settings_general', $flush_cache );
  575. $form_values = empty( $this->error_message ) ? $settings : ITForm::get_post_data();
  576. $form = new ITForm( $form_values, array( 'prefix' => 'it_exchange_settings' ) );
  577. $form_options = array(
  578. 'id' => apply_filters( 'it_exchange_settings_form_id', 'it-exchange-settings' ),
  579. 'enctype' => apply_filters( 'it_exchange_settings_form_enctype', false ),
  580. );
  581. if ( ! empty ( $this->status_message ) )
  582. ITUtility::show_status_message( $this->status_message );
  583. if ( ! empty( $this->error_message ) )
  584. ITUtility::show_error_message( $this->error_message );
  585. include( 'views/admin-settings.php' );
  586. }
  587. /**
  588. * Prints the email page for iThemes Exchange
  589. *
  590. * @since 0.3.4
  591. * @return void
  592. */
  593. function print_email_settings_page() {
  594. $flush_cache = ! empty( $_POST );
  595. $settings = it_exchange_get_option( 'settings_email', $flush_cache );
  596. $form_values = empty( $this->error_message ) ? $settings : ITForm::get_post_data();
  597. $form = new ITForm( $form_values, array( 'prefix' => 'it_exchange_email_settings' ) );
  598. $form_options = array(
  599. 'id' => apply_filters( 'it_exchange_email_settings_form_id', 'it-exchange-email-settings' ),
  600. 'enctype' => apply_filters( 'it_exchange_email_settings_form_enctype', false ),
  601. 'action' => 'admin.php?page=it-exchange-settings&tab=email',
  602. );
  603. if ( ! empty ( $this->status_message ) )
  604. ITUtility::show_status_message( $this->status_message );
  605. if ( ! empty( $this->error_message ) )
  606. ITUtility::show_error_message( $this->error_message );
  607. include( 'views/admin-email-settings.php' );
  608. }
  609. /**
  610. * Prints the pages page for iThemes Exchange
  611. *
  612. * @since 0.3.7
  613. * @return void
  614. */
  615. function print_pages_settings_page() {
  616. $flush_cache = ! empty( $_POST );
  617. $pages = it_exchange_get_pages( $flush_cache );
  618. $settings = array();
  619. // Setup form field values for each page
  620. foreach( $pages as $page => $data ) {
  621. $settings[$page . '-slug'] = it_exchange_get_page_slug( $page );
  622. $settings[$page . '-name'] = it_exchange_get_page_name( $page );
  623. $settings[$page . '-type'] = it_exchange_get_page_type( $page );
  624. $settings[$page . '-wpid'] = it_exchange_get_page_wpid( $page );
  625. }
  626. $form_values = empty( $this->error_message ) ? $settings : ITForm::get_post_data();
  627. $form = new ITForm( $form_values, array( 'prefix' => 'it_exchange_page_settings' ) );
  628. $form_options = array(
  629. 'id' => apply_filters( 'it_exchange_page_settings_form_id', 'it-exchange-page-settings' ),
  630. 'enctype' => apply_filters( 'it_exchange_page_settings_form_enctype', false ),
  631. 'action' => 'admin.php?page=it-exchange-settings&tab=pages',
  632. );
  633. if ( ! empty ( $this->status_message ) )
  634. ITUtility::show_status_message( $this->status_message );
  635. if ( ! empty( $this->error_message ) )
  636. ITUtility::show_error_message( $this->error_message );
  637. include( 'views/admin-page-settings.php' );
  638. }
  639. /**
  640. * Sets the Pages settings default values
  641. *
  642. * @since 0.4.0
  643. * @return array
  644. */
  645. function set_pages_settings_defaults( $values ) {
  646. $registered = it_exchange_get_registered_pages();
  647. $defaults = array();
  648. foreach( $registered as $page => $options ) {
  649. $defaults[$page . '-name'] = $options['name'];
  650. $defaults[$page . '-slug'] = $options['slug'];
  651. $defaults[$page . '-type'] = $options['type'];
  652. $defaults[$page . '-wpid'] = $options['wpid'];
  653. }
  654. $values = ITUtility::merge_defaults( $values, $defaults );
  655. return $values;
  656. }
  657. /**
  658. * Prints the add-ons page in the admin area
  659. *
  660. * @since 0.2.0
  661. * @return void
  662. */
  663. function print_exchange_add_ons_page() {
  664. $add_on_cats = it_exchange_get_addon_categories();
  665. $message = empty( $_GET['message'] ) ? false : $_GET['message'];
  666. if ( 'enabled' == $message ) {
  667. ITUtility::show_status_message( __( 'Add-on enabled.', 'it-l10n-ithemes-exchange' ) );
  668. } else if ( 'disabled' == $message ) {
  669. ITUtility::show_status_message( __( 'Add-on disabled.', 'it-l10n-ithemes-exchange' ) );
  670. } else if ( 'addon-auto-disabled-' == substr( $message, 0, 20 ) ) {
  671. $addon_slug = substr( $message, 20 );
  672. $status_message = __( sprintf( 'iThemes Exchange has automatically disabled an add-on: %s. This is mostly likely due to it being uninstalled or improperlly registered.', $addon_slug ), 'it-l10n-ithemes-exchange' );
  673. ITUtility::show_status_message( $status_message );
  674. }
  675. $error= empty( $_GET['error'] ) ? false : $_GET['error'];
  676. if ( 'enabled' == $error )
  677. ITUtility::show_error_message( __( 'Error: Add-on not enabled.', 'it-l10n-ithemes-exchange' ) );
  678. else if ( 'disabled' == $error )
  679. ITUtility::show_error_message( __( 'Error: Add-on not disabled.', 'it-l10n-ithemes-exchange' ) );
  680. include( 'views/admin-add-ons.php' );
  681. }
  682. /**
  683. * Enable all addons tagged as "required"
  684. *
  685. * @since 0.4.0
  686. *
  687. * @return void
  688. */
  689. function enable_required_add_ons() {
  690. $registered = it_exchange_get_addons();
  691. $enabled = it_exchange_get_enabled_addons();
  692. foreach ( $registered as $slug => $params ) {
  693. if ( !empty( $params['options']['tag'] ) && 'required' === $params['options']['tag'] ) {
  694. if ( empty( $enabled[$slug] ) ) {
  695. $enabled_addon = it_exchange_enable_addon( $slug );
  696. }
  697. }
  698. }
  699. }
  700. /**
  701. * Adds the it-exchange-admin CSS class to body on Exchange admin pages
  702. *
  703. * @since 0.4.17
  704. *
  705. * @param string $classes incoming classes from WP filter
  706. * @return string
  707. */
  708. function add_exchange_class_to_exchange_pages( $classes ) {
  709. if ( $this->is_exchange_admin_page() ) {
  710. $classes = explode( ' ', $classes );
  711. $classes[] = 'it-exchange-admin';
  712. $classes = implode( ' ', $classes );
  713. }
  714. return $classes;
  715. }
  716. /**
  717. * Is this an exchange admin page?
  718. *
  719. * @since 0.4.17
  720. *
  721. * @return boolean
  722. */
  723. function is_exchange_admin_page() {
  724. $is_exchange_admin = false;
  725. // Is this an exchange post type?
  726. $post_types = array(
  727. 'it_exchange_prod',
  728. 'it_exchange_tran',
  729. 'it_exchange_coupon',
  730. 'it_exchange_download',
  731. );
  732. if ( ! empty( $_GET['post_type'] ) && in_array( $_GET['post_type'], (array) apply_filters( 'it_exchange_post_types', $post_types ) ) )
  733. $is_exchange_admin = true;
  734. // Are we editing an exchange product type
  735. if ( ! empty( $_GET['action'] ) && 'edit' == $_GET['action'] && ! empty( $_GET['post'] ) && in_array( get_post_type( $_GET['post'] ), (array) apply_filters( 'it_exchange_post_types', $post_types ) ) )
  736. $is_exchange_admin = true;
  737. // Is this an Exchange page
  738. $pages = array(
  739. 'it-exchange-settings',
  740. 'it-exchange-addons',
  741. 'it-exchange-help',
  742. 'it-exchange-setup',
  743. );
  744. if ( ! empty( $this->_current_page ) && in_array( $this->_current_page, (array) apply_filters( 'it_exchange_admin_pages', $pages ) ) )
  745. $is_exchange_admin = true;
  746. return $is_exchange_admin;
  747. }
  748. /**
  749. * Adds a registered Add-on to list of enabled add-ons
  750. *
  751. * @since 0.2.0
  752. */
  753. function enable_disable_registered_add_on() {
  754. $enable_addon = empty( $_GET['it-exchange-enable-addon'] ) ? false : $_GET['it-exchange-enable-addon'];
  755. $disable_addon = empty( $_GET['it-exchange-disable-addon'] ) ? false : $_GET['it-exchange-disable-addon'];
  756. $tab = empty( $_GET['tab'] ) ? false : $_GET['tab'];
  757. if ( ! $enable_addon && ! $disable_addon )
  758. return;
  759. $registered = it_exchange_get_addons();
  760. // Enable or Disable addon requested by user
  761. if ( $enable_addon ) {
  762. if ( $nonce_valid = wp_verify_nonce( $_GET['_wpnonce'], 'exchange-enable-add-on' ) )
  763. $enabled = it_exchange_enable_addon( $enable_addon );
  764. $message = 'enabled';
  765. } else if ( $disable_addon ) {
  766. if ( $nonce_valid = wp_verify_nonce( $_GET['_wpnonce'], 'exchange-disable-add-on' ) )
  767. $enabled = it_exchange_disable_addon( $disable_addon );
  768. $message = 'disabled';
  769. }
  770. // Redirect if nonce not valid
  771. if ( ! $nonce_valid ) {
  772. wp_safe_redirect( admin_url( '/admin.php?page=it-exchange-addons&tab=' . $tab . '&error=' . $message ) );
  773. die();
  774. }
  775. // Disable any enabled add-ons that aren't registered any more while we're here.
  776. $enabled_addons = it_exchange_get_enabled_addons();
  777. foreach( (array) $enabled_addons as $slug => $params ) {
  778. if ( empty( $registered[$slug] ) )
  779. it_exchange_disable_addon( $slug );
  780. }
  781. $redirect_to = admin_url( '/admin.php?page=it-exchange-addons&tab=' . $tab . '&message=' . $message );
  782. // Redirect to settings page on activation if it exists
  783. if ( $enable_addon ) {
  784. if ( $enabled = it_exchange_get_addon( $enable_addon ) ) {
  785. if ( ! empty( $enabled['options']['settings-callback'] ) && is_callable( $enabled['options']['settings-callback'] ) )
  786. $redirect_to .= '&add-on-settings=' . $enable_addon;
  787. }
  788. }
  789. wp_safe_redirect( $redirect_to );
  790. die();
  791. }
  792. /**
  793. * Opens the iThemes Exchange Admin Menu when viewing the Add New page
  794. *
  795. * @since 0.3.0
  796. * @return string
  797. */
  798. function open_exchange_menu_on_post_type_views( $parent_file, $revert=false ) {
  799. global $submenu_file, $pagenow, $post;
  800. if ( 'post-new.php' != $pagenow && 'post.php' != $pagenow )
  801. return $parent_file;
  802. if ( empty( $post->post_type ) || ( 'it_exchange_prod' != $post->post_type && 'it_exchange_tran' != $post->post_type ) )
  803. return $parent_file;
  804. // Set Add New as bold when on the post-new.php screen
  805. if ( 'post-new.php' == $pagenow ) {
  806. if ( ! empty( $_GET['it-exchange-product-type'] ) )
  807. $submenu_file = "post-new.php?post_type=it_exchange_prod&it-exchange-product-type={$_GET['it-exchange-product-type']}";
  808. else
  809. $submenu_file = 'it-exchange-choose-product-type';
  810. }
  811. // Return it-exchange as the parent (open) menu when on post-new.php and post.php for it_exchange_prod post_types
  812. return 'it-exchange';
  813. }
  814. /**
  815. * Redirects post-new.php to it-exchange-choose-product-type when needed
  816. *
  817. * If we have landed on post-new.php?post_type=it_exchange_prod without the product_type param
  818. * and with multiple product-type add-ons enabled.
  819. *
  820. * @since 0.3.1
  821. * @return void
  822. */
  823. function redirect_post_new_to_product_type_selection_screen() {
  824. global $pagenow;
  825. $product_type_add_ons = it_exchange_get_enabled_addons( array( 'category' => array( 'product-type' ) ) );
  826. $post_type = empty( $_GET['post_type'] ) ? false : $_GET['post_type'];
  827. $product_type = empty( $_GET['it-exchange-product-type'] ) ? false : $_GET['it-exchange-product-type'];
  828. if ( ! empty( $product_type ) )
  829. return;
  830. if ( count( $product_type_add_ons ) > 1 && 'post-new.php' == $pagenow && 'it_exchange_prod' == $post_type ) {
  831. $product_type_add_ons = reset( $product_type_add_ons );
  832. if ( ! empty( $product_type_add_ons['slug'] ) ) {
  833. wp_safe_redirect( add_query_arg( 'it-exchange-product-type', $product_type_add_ons['slug'] ) );
  834. die();
  835. }
  836. }
  837. }
  838. /**
  839. * Prints select options for the currency type
  840. *
  841. * @since 0.3.6
  842. * return array
  843. */
  844. function get_default_currency_options() {
  845. $options = array();
  846. $currency_options = it_exchange_get_data_set( 'currencies' );
  847. foreach( (array) $currency_options as $cc => $currency ) {
  848. $options[$cc] = ucwords( $currency['name'] ) . ' (' . $currency['symbol'] . ')';
  849. }
  850. return $options;
  851. }
  852. /**
  853. * AJAX callback for general settings country states UI
  854. *
  855. * @since 1.2.0
  856. *
  857. * @return void
  858. */
  859. function update_country_state_ui_in_general_settings() {
  860. $base_country = empty( $_POST['ite_base_country_ajax'] ) ? 'US' : $_POST['ite_base_country_ajax'];
  861. $base_state = empty( $_POST['ite_base_state_ajax'] ) ? 'OK' : $_POST['ite_base_state_ajax'];
  862. $states = it_exchange_get_data_set( 'states', array( 'country' => $base_country ) );
  863. // Fire off one of the following two functions.
  864. if ( ! empty( $states ) ) {
  865. ?>
  866. <select name="it_exchange_settings-company-base-state" id="company-base-state">
  867. <?php foreach( $states as $key => $value ) : ?>
  868. <option value="<?php esc_attr_e( $key ); ?>" <?php selected( $key, $base_state ); ?>><?php esc_attr_e( $value ); ?></option>
  869. <?php endforeach; ?>
  870. </select>
  871. <?php
  872. die();
  873. } else {
  874. ?>
  875. <input class="small-text" max-length="3" type="text" name="it_exchange_settings-company-base-state" id="company-base-state" value="">
  876. <p class="description">
  877. <?php printf( __( 'Please use the 2-3 character %sISO abbreviation%s for country subdivisions', 'it-l10n-ithemes-exchange' ), '<a href="http://en.wikipedia.org/wiki/ISO_3166-2" target="_blank">', '</a>' ); ?>
  878. </p>
  879. <?php
  880. die();
  881. }
  882. }
  883. /**
  884. * Save core general settings
  885. *
  886. * Validates data and saves to options.
  887. *
  888. * @since 0.3.4
  889. *
  890. * @return void
  891. */
  892. function save_core_general_settings() {
  893. if ( empty( $_POST ) || 'it-exchange-settings' != $this->_current_page || ! empty( $this->_current_tab ) )
  894. return;
  895. $settings = wp_parse_args( ITForm::get_post_data(), it_exchange_get_option( 'settings_general' ) );
  896. // Check nonce
  897. if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'exchange-general-settings' ) ) {
  898. $this->error_message = __( 'Error. Please try again', 'it-l10n-ithemes-exchange' );
  899. return;
  900. }
  901. if ( ! empty( $this->error_message ) || $error_msg = $this->general_settings_are_invalid( $settings ) ) {
  902. if ( ! empty( $error_msg ) )
  903. $this->error_message = $error_msg;
  904. } else {
  905. it_exchange_save_option( 'settings_general', $settings );
  906. $this->status_message = __( 'Settings Saved.', 'it-l10n-ithemes-exchange' );
  907. }
  908. }
  909. /**
  910. * Save core general settings from Wizard and performs action for other addons to handle saving
  911. *
  912. * Validates data and saves to options.
  913. *
  914. * @since 0.3.4
  915. *
  916. * @return void
  917. */
  918. function save_core_wizard_settings() {
  919. // Abandon if not saving wizard
  920. if ( !( isset( $_REQUEST['it_exchange_settings-wizard-submitted'] ) && 'it-exchange-setup' === $this->_current_page ) )
  921. return;
  922. if ( empty( $_REQUEST['it-exchange-transaction-methods'] ) ) {
  923. it_exchange_add_message( 'error', __( 'You must select at least one Payment Method.', 'it-l10n-ithemes-exchange' ) );
  924. return;
  925. }
  926. // Grab general settings
  927. $general_settings = array();
  928. $default_wizard_general_settings = apply_filters( 'default_wizard_general_settings', array( 'company-email', 'default-currency' ) );
  929. foreach( $default_wizard_general_settings as $var ) {
  930. if ( isset( $_REQUEST['it_exchange_settings-' . $var] ) )
  931. $general_settings[$var] = $_REQUEST['it_exchange_settings-' . $var];
  932. }
  933. $settings = wp_parse_args( $general_settings, it_exchange_get_option( 'settings_general' ) );
  934. if ( it_exchange_has_messages( 'error' ) || $error_msg = $this->general_settings_are_invalid( $settings ) ) {
  935. if ( ! empty( $error_msg ) ) {
  936. it_exchange_add_message( 'error', $error_msg );
  937. return;
  938. }
  939. }
  940. // Save activated product types
  941. $product_addons = it_exchange_get_addons( array( 'category' => 'product-type' ) );
  942. // Filter out simple products
  943. if ( isset( $product_addons['simple-product-type'] ) )
  944. unset( $product_addons['simple-product-type'] );
  945. // Disable all of the product-type addons accept for simple
  946. foreach( $product_addons as $product_addon_slug => $options ) {
  947. it_exchange_disable_addon( $product_addon_slug );
  948. }
  949. // Loop through selected and renable them.
  950. if ( ! empty( $_REQUEST['it-exchange-product-types'] ) && is_array( $_REQUEST['it-exchange-product-types'] ) ) {
  951. foreach( $_REQUEST['it-exchange-product-types'] as $product_addon ) {
  952. it_exchange_enable_addon( $product_addon );
  953. }
  954. }
  955. // Handle core Simple Shipping addon settings here
  956. if ( empty( $_REQUEST['it-exchange-shipping-methods'] ) || ( ! in_array( 'simple-shipping-flat-rate', $_REQUEST['it-exchange-shipping-methods'] ) && ! in_array( 'simple-shipping-free', $_REQUEST['it-exchange-shipping-methods'] ) ) ) {
  957. it_exchange_disable_addon( 'simple-shipping' );
  958. } else {
  959. it_exchange_enable_addon( 'simple-shipping' );
  960. $simple_shipping_options = it_exchange_get_option( 'simple-shipping', true );
  961. $simple_shipping_options['enable-flat-rate-shipping'] = in_array( 'simple-shipping-flat-rate', $_REQUEST['it-exchange-shipping-methods'] );
  962. $simple_shipping_options['enable-free-shipping'] = in_array( 'simple-shipping-free', $_REQUEST['it-exchange-shipping-methods'] );
  963. if ( ! empty( $simple_shipping_options['enable-flat-rate-shipping'] ) ) {
  964. $flat_rate_default_cost = $_REQUEST['it_exchange_settings-simple-shipping-flat-rate-cost'];
  965. $simple_shipping_options['flat-rate-shipping-amount'] = $flat_rate_default_cost;
  966. }
  967. it_exchange_save_option( 'simple-shipping', $simple_shipping_options );
  968. }
  969. // Transaction Methods
  970. $tx_error_msgs = array();
  971. $addons = it_exchange_get_addons( array( 'category' => 'transaction-methods', 'show_required' => false ) );
  972. foreach( $_REQUEST['it-exchange-transaction-methods'] as $add_on ) {
  973. it_exchange_enable_addon( $add_on );
  974. unset( $addons[$add_on] );
  975. $tx_error_msgs = apply_filters( 'it_exchange_save_' . $add_on . '_wizard_settings', $tx_error_msgs );
  976. }
  977. foreach ( $addons as $addon ) {
  978. it_exchange_disable_addon( $addon['slug'] );
  979. }
  980. if ( ! empty( $tx_error_msgs ) ) {
  981. foreach( (array) $tx_error_msgs as $msg ) {
  982. it_exchange_add_message( 'error', $msg );
  983. }
  984. return;
  985. }
  986. // Signup for mailchimp if checkbox was checked
  987. if ( !empty( $_REQUEST['it_exchange_settings-exchange-notifications'] )
  988. && !empty( $_REQUEST['it_exchange_settings-company-email'] ) ) {
  989. $this->mail_chimp_signup( $_REQUEST['it_exchange_settings-company-email'] );
  990. }
  991. // Auto enable any core add-ons we want enabled on setup.
  992. $auto_enabled_addons = array(
  993. 'basic-reporting',
  994. );
  995. foreach( $auto_enabled_addons as $addon ) {
  996. it_exchange_enable_addon( $addon );
  997. }
  998. it_exchange_save_option( 'settings_general', $settings );
  999. do_action( 'it_exchange_enabled_addons_loaded' );
  1000. do_action( 'it_exchange_save_wizard_settings' );
  1001. $settings = it_exchange_get_option( 'settings_general', true );
  1002. $sample_product = empty( $settings['sample-product-id'] ) ? false : it_exchange_get_product( $settings['sample-product-id'] );
  1003. $settings_saved = __( 'Settings Saved. Congrats!', 'it-l10n-ithemes-exchange' );
  1004. $add_product_link = sprintf( __( '%sAdd a Product%s', 'it-l10n-ithemes-exchange' ), '<a href="' . get_admin_url() . 'post-new.php?post_type=it_exchange_prod&it-exchange-product-type=digital-downloads-product-type">', '</a>' );
  1005. $view_addons_link = sprintf( __( '%sEnable Add-ons%s', 'it-l10n-ithemes-exchange' ), '<a href="' . get_admin_url() . 'admin.php?page=it-exchange-addons">', '</a>' );
  1006. $view_sample_link = empty( $sample_product->ID ) ? '' : ' | ' . sprintf( __( '%sView Sample Product%s', 'it-l10n-ithemes-exchange' ), '<a href="' . get_admin_url() . 'post.php?post=' . $sample_product->ID . '&action=edit">', '</a>' );
  1007. it_exchange_add_message( 'notice', $settings_saved . ' ' . $add_product_link . ' | ' . $view_addons_link . $view_sample_link );
  1008. wp_redirect( get_admin_url() . 'admin.php?page=it-exchange-setup&it_exchange_settings-dismiss-wizard-nag=1' );
  1009. die();
  1010. }
  1011. public function mail_chimp_signup( $email ) {
  1012. $email = trim( $email );
  1013. if ( is_email( $email ) ) {
  1014. $mailchimp = 'http://ithemes.us2.list-manage.com/subscribe/post?u=7acf83c7a47b32c740ad94a4e&amp;id=9da0741ac0';
  1015. $query = array(
  1016. 'body' => array(
  1017. 'EMAIL' => $email,
  1018. ),
  1019. );
  1020. wp_remote_post( $mailchimp, $query );
  1021. }
  1022. }
  1023. /**
  1024. * Validate general settings
  1025. *
  1026. * @since 0.3.6
  1027. * @param string $settings submitted settings
  1028. * @return false or error message
  1029. */
  1030. function general_settings_are_invalid( $settings ) {
  1031. $errors = array();
  1032. if ( ! empty( $settings['company-email'] ) && ! is_email( $settings['company-email'] ) )
  1033. $errors[] = __( 'Please provide a valid email address.', 'it-l10n-ithemes-exchange' );
  1034. if ( empty( $settings['currency-thousands-separator'] ) )
  1035. $errors[] = __( 'Thousands Separator cannot be empty', 'it-l10n-ithemes-exchange' );
  1036. if ( empty( $settings['currency-decimals-separator'] ) )
  1037. $errors[] = __( 'Decimals Separator cannot be empty', 'it-l10n-ithemes-exchange' );
  1038. $errors = apply_filters( 'it_exchange_general_settings_validation_errors', $errors );
  1039. if ( ! empty ( $errors ) )
  1040. return implode( '<br />', $errors );
  1041. else
  1042. return false;
  1043. }
  1044. /**
  1045. * Save core email tab settings
  1046. *
  1047. * Validates data and saves to options.
  1048. *
  1049. * @since 0.3.4
  1050. * @return void
  1051. */
  1052. function save_core_email_settings() {
  1053. if ( empty( $_POST ) || 'it-exchange-settings' != $this->_current_page || 'email' != $this->_current_tab )
  1054. return;
  1055. // Check nonce
  1056. if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'exchange-email-settings' ) ) {
  1057. $this->error_message = __( 'Error. Please try again', 'it-l10n-ithemes-exchange' );
  1058. return;
  1059. }
  1060. $settings = wp_parse_args( ITForm::get_post_data(), it_exchange_get_option( 'settings_email' ) );
  1061. if ( ! empty( $this->error_message ) || $error_msg = $this->email_settings_are_invalid( $settings ) ) {
  1062. if ( ! empty( $error_msg ) )
  1063. $this->error_message = $error_msg;
  1064. } else {
  1065. it_exchange_save_option( 'settings_email', $settings );
  1066. $this->status_message = __( 'Settings Saved.', 'it-l10n-ithemes-exchange' );
  1067. }
  1068. }
  1069. /**
  1070. * Validate email settings
  1071. *
  1072. * @since 0.3.6
  1073. * @param string $settings submitted settings
  1074. * @return false or error message
  1075. */
  1076. function email_settings_are_invalid( $settings ) {
  1077. $errors = array();
  1078. if ( empty( $settings['receipt-email-address'] )
  1079. || ( !empty( $settings['receipt-email-address'] ) && ! is_email( $settings['receipt-email-address'] ) ) )
  1080. $errors[] = __( 'Please provide a valid email address.', 'it-l10n-ithemes-exchange' );
  1081. if ( empty( $settings['receipt-email-name'] ) )
  1082. $errors[] = __( 'Email Name cannot be empty', 'it-l10n-ithemes-exchange' );
  1083. if ( empty( $settings['receipt-email-subject'] ) )
  1084. $errors[] = __( 'Email Subject cannot be empty', 'it-l10n-ithemes-exchange' );
  1085. if ( empty( $settings['receipt-email-template'] ) )
  1086. $errors[] = __( 'Email Template cannot be empty', 'it-l10n-ithemes-exchange' );
  1087. if ( !empty( $settings['notification-email-address'] ) ) {
  1088. $emails = explode( ',', $settings['notification-email-address'] );
  1089. foreach( $emails as $email ) {
  1090. if ( !is_email( trim( $email ) ) ) {
  1091. $errors[] = __( 'Invalid email address in Sales Notification Email Address', 'it-l10n-ithemes-exchange' );
  1092. break;
  1093. }
  1094. }
  1095. }
  1096. $errors = apply_filters( 'it_exchange_email_settings_validation_errors', $errors );
  1097. if ( ! empty ( $errors ) )
  1098. return '<p>' . implode( '<br />', $errors ) . '</p>';
  1099. else
  1100. return false;
  1101. }
  1102. /**
  1103. * Save core pages tab settings
  1104. *
  1105. * Validates data and saves to options.
  1106. *
  1107. * @since 0.3.7
  1108. * @return void
  1109. */
  1110. function save_core_page_settings() {
  1111. if ( empty( $_POST ) || 'it-exchange-settings' != $this->_current_page || 'pages' != $this->_current_tab )
  1112. return;
  1113. // Grab page settings from DB
  1114. $existing = it_exchange_get_pages( true );
  1115. $settings = array();
  1116. // Format for settings form
  1117. foreach( $existing as $page => $data ) {
  1118. $settings[$page . '-slug'] = it_exchange_get_page_slug( $page );
  1119. $settings[$page . '-name'] = it_exchange_get_page_name( $page );
  1120. $settings[$page . '-type'] = it_exchange_get_page_type( $page );
  1121. $settings[$page . '-wpid'] = it_exchange_get_page_wpid( $page );
  1122. }
  1123. $settings = wp_parse_args( ITForm::get_post_data(), $settings );
  1124. // If WordPress page is set to 0 somehow, use exchange page
  1125. foreach( $existing as $page => $data ) {
  1126. if ( 'wordpress' == $settings[$page . '-type'] && empty( $settings[$page . '-wpid'] ) )
  1127. $settings[$page . '-type'] = 'exchange';
  1128. }
  1129. // Check nonce
  1130. if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'exchange-page-settings' ) ) {
  1131. $this->error_message = __( 'Error. Please try again', 'it-l10n-ithemes-exchange' );
  1132. return;
  1133. }
  1134. // Trim all slug settings
  1135. foreach( $settings as $key => $value ) {
  1136. if ( 'slug' == substr( $key, -4 ) )
  1137. $settings[$key] = sanitize_title( $value );
  1138. else
  1139. $settings[$key] = trim($value);
  1140. }
  1141. if ( ! empty( $this->error_message ) || $error_msg = $this->

Large files files are truncated, but you can click here to view the full file