PageRenderTime 1783ms CodeModel.GetById 175ms app.highlight 417ms RepoModel.GetById 912ms 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

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

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