prev_settings = new Give_Plugin_Settings(); // Get current tab. $this->current_tab = give_get_current_setting_tab(); $this->current_section = empty( $_REQUEST['section'] ) ? ( current( array_keys( $this->get_sections() ) ) ) : sanitize_title( $_REQUEST['section'] ); // Tab ID. $this->id = $this->current_tab; // Add addon tabs. add_filter( 'give-settings_tabs_array', array( $this, 'add_addon_settings_page' ), 999999 ); // Add save hook to addons. add_action( 'give-settings_get_settings_pages', array( $this, 'setup_addon_save_hook' ), 999999 ); // Add backward compatibility filters plugin settings. $setting_tabs = array( 'general', 'gateways', 'display', 'emails', 'addons', 'licenses' ); // Filter Payment Gateways settings. if ( in_array( $this->current_tab, $setting_tabs ) ) { add_filter( "give_get_settings_{$this->current_tab}", array( $this, 'get_filtered_addon_settings', ), 999999, 1 ); add_filter( "give_get_sections_{$this->current_tab}", array( $this, 'get_filtered_addon_sections', ), 999999, 1 ); } } /** * Default setting tab. * * @since 1.8 * * @param $setting_tab * * @return string */ function set_default_setting_tab( $setting_tab ) { $default_tab = ''; // Set default tab to first setting tab. if ( $sections = array_keys( $this->get_sections() ) ) { $default_tab = current( $sections ); } return $default_tab; } /** * Add addon setting pages. * * @since 1.8 * * @param $pages * * @return mixed */ function add_addon_settings_page( $pages ) { // Previous setting page. $previous_pages = $this->prev_settings->give_get_settings_tabs(); // API and System Info setting tab merge to Tools setting tab, so remove them from tabs. unset( $previous_pages['api'] ); unset( $previous_pages['system_info'] ); // Tab is not register. $pages_diff = array_keys( array_diff( $previous_pages, $pages ) ); // Merge old settings with new settings. $pages = array_merge( $pages, $previous_pages ); if ( in_array( $this->current_tab, $pages_diff ) ) { // Filter & actions. add_filter( "give_default_setting_tab_section_{$this->current_tab}", array( $this, 'set_default_setting_tab', ), 10 ); add_action( "give-settings_sections_{$this->current_tab}_page", array( $this, 'output_sections' ) ); add_action( "give-settings_settings_{$this->current_tab}_page", array( $this, 'output' ), 10 ); add_action( "give-settings_save_{$this->current_tab}", array( $this, 'save' ) ); } return $pages; } /** * Setup save addon data hook. * * @since 1.8 * * @param $pages * * @return mixed */ function setup_addon_save_hook( $pages ) { $page_ids = array(); foreach ( $pages as $page ) { $page_ids = $page->add_settings_page( $page_ids ); } // Previous setting page. $previous_pages = $this->prev_settings->give_get_settings_tabs(); // API and System Info setting tab merge to Tools setting tab, so remove them from tabs. unset( $previous_pages['api'] ); unset( $previous_pages['system_info'] ); // Tab is not register. $pages_diff = array_keys( array_diff( $previous_pages, $page_ids ) ); // Merge old settings with new settings. // $pages = array_merge( $page_ids, $previous_pages ); if ( in_array( $this->current_tab, $pages_diff ) ) { // Filter & actions. add_action( "give-settings_save_{$this->current_tab}", array( $this, 'save' ) ); } return $pages; } /** * Get section name from section title * * @since 1.8 * * @param $field_name * * @return string */ function get_section_name( $field_name ) { // Bailout. if ( empty( $field_name ) ) { return $field_name; } $section_name = explode( ' ', $field_name ); // Output. return strip_tags( implode( ' ', $section_name ) ); } /** * Do not translate string * * @since 1.0 * @access public * * @param $translation * @param $text * * @return mixed */ public function en_translation( $translation, $text ) { return $text; } /** * Get addon sections. * * @since 1.8 * * @param array $sections Array of setting fields (Optional). * * @return mixed */ function get_filtered_addon_sections( $sections = array() ) { // New sections. $new_sections = array(); $sections_ID = array_keys( $sections ); $setting_fields = $this->prev_settings->give_settings( $this->current_tab ); // We need untranslated settings for backward compatibility. add_filter( 'gettext', array( $this, 'en_translation' ), 10, 2 ); $en_setting_fields = $this->prev_settings->give_settings( $this->current_tab ); remove_filter( 'gettext', array( $this, 'en_translation' ), 10 ); if ( ! empty( $setting_fields ) && ! empty( $setting_fields['fields'] ) ) { foreach ( $setting_fields['fields'] as $index => $field ) { // Collect new sections from addons. if ( 'give_title' !== $field['type'] ) { continue; } // Untranslated setting name. $en_setting_field_name = isset( $en_setting_fields['fields'][ $index ]['name'] ) ? $en_setting_fields['fields'][ $index ]['name'] : ''; // Section name. $field['name'] = isset( $field['name'] ) ? $field['name'] : ''; $section_name = $this->get_section_name( $field['name'] ); // Check if section name exit and section title array is not empty. if ( ! empty( $sections ) && ! empty( $en_setting_field_name ) ) { // Bailout: Do not load section if it is already exist. if ( in_array( sanitize_title( $en_setting_field_name ), $sections_ID ) // Check section id. || in_array( $section_name, $sections ) // Check section name. ) { continue; } } // Collect new sections from addons. $new_sections[ sanitize_title( $field['name'] ) ] = $section_name; } } // Add new section. if ( ! empty( $new_sections ) ) { $sections = array_merge( $sections, $new_sections ); } // Remove section tab conditionally. switch ( give_get_current_setting_tab() ) { case 'emails': // unset( $sections['donation-receipt'] ); unset( $sections['new-donation-notification'] ); } // Output. return $sections; } /** * Get setting fields. * * @since 1.8 * * @param array $settings List of settings. * @param array $setting_fields Main tab settings data. * * @return array */ function get_filtered_addon_settings( $settings, $setting_fields = array() ) { global $wp_filter; $new_setting_fields = array(); if ( ! empty( $settings ) ) { // Bailout: If setting array contain first element of type title then it means it is already created with new setting api (skip this section ). if ( isset( $settings[0]['type'] ) && 'title' == $settings[0]['type'] ) { $last_sectionend_pos = 0; foreach ( $settings as $index => $setting ) { // We need setting till last section end. if ( 'sectionend' === $setting['type'] ) { $last_sectionend_pos = ++ $index; } } $settings = array_slice( $settings, 0, $last_sectionend_pos ); return $settings; } // Store title field id. $prev_title_field_id = ''; // Create new setting fields. foreach ( $settings as $index => $field ) { // Bailout: Must need field type to process. if ( ! isset( $field['type'] ) ) { continue; } // Set wrapper class if any. if ( ! empty( $field['row_classes'] ) ) { $field['wrapper_class'] = $field['row_classes']; unset( $field['row_classes'] ); } $field['name'] = ! isset( $field['name'] ) ? '' : $field['name']; $field['desc'] = ! isset( $field['desc'] ) ? '' : $field['desc']; // Modify cmb2 setting fields. switch ( $field['type'] ) { case 'text' : case 'file' : $field['css'] = 'width:25em;'; break; case 'text_small' : $field['type'] = 'text'; break; case 'text_email' : $field['type'] = 'email'; $field['css'] = 'width:25em;'; break; case 'radio_inline' : $field['type'] = 'radio'; $field['class'] = 'give-radio-inline'; break; case 'give_title' : $field['type'] = 'title'; // Since we are showing sections, so there now ned to show horizontal rules. if ( '
' . __( 'Oops, this settings page does not exist.', 'give' ) . '