options = get_option( static::OPTION_NAME ); ?>
API
array( $this, 'sanitize' ), // Sanitize. ) ); add_settings_section( '4partners-sync-section-id', // ID. '', // Title. array( $this, 'print_section_info' ), // Callback. '4partners-admin' // Page. ); add_settings_section( '4partners-categories-section-id', // ID. '', // Title. array( $this, 'print_section_info' ), // Callback. '4partners-admin' // Page. ); add_settings_section( '4partners-general-section-id', // ID. '', // Title. array( $this, 'print_section_info' ), // Callback. '4partners-admin' // Page. ); add_settings_field( 'api_key', '', array( $this, 'api_key_callback' ), '4partners-admin', '4partners-general-section-id' ); add_settings_field( 'cron', '', array( $this, 'cron_callback' ), '4partners-admin', '4partners-general-section-id' ); add_settings_field( 'sync', __( 'Synchronization', '4partners' ), array( $this, 'sync_callback' ), '4partners-admin', '4partners-sync-section-id' ); add_settings_field( 'categories', __( 'Categories', '4partners' ), array( $this, 'categories_callback' ), '4partners-admin', '4partners-categories-section-id' ); add_settings_field( 'hidden_fields', '', array( $this, 'hidden_fields_callback' ), '4partners-admin', '4partners-general-section-id' ); } /** * Sanitize each setting field as needed * * @param array $input Contains all settings fields as array keys. * * @return array */ public function sanitize( $input ) { $new_input = array(); if ( isset( $input['api_key'] ) ) { $new_input['api_key'] = sanitize_text_field( $input['api_key'] ); } if ( isset( $input['cron'] ) ) { $new_input['cron'] = sanitize_text_field( $input['cron'] ); } if ( isset( $input['categories'] ) ) { $new_input['categories'] = $input['categories']; } if ( isset( $input['start_sync_categories'] ) ) { $new_input['start_sync_categories'] = array_map( 'sanitize_text_field', $input['start_sync_categories'] ); } else { $new_input['start_sync_categories'] = []; } if ( isset( $input['import_token'] ) ) { $new_input['import_token'] = sanitize_text_field( $input['import_token'] ); } return $new_input; } public function api_key_callback() { printf( '
4Parnters API Key

', isset( $this->options['api_key'] ) ? esc_attr( $this->options['api_key'] ) : '' ); } public function hidden_fields_callback() { printf( '', isset( $this->options['import_token'] ) ? esc_attr( $this->options['import_token'] ) : '' ); } public function cron_callback() { $options = ''; $current_value = isset( $this->options['cron'] ) ? $this->options['cron'] : 'daily'; $available_options = [ static::CRON_DAILY => __( 'Daily', '4partners' ), static::CRON_TWICEDAILY => __( 'Twice daily', '4partners' ), static::CRON_WEEKLY => __( 'Weekly', '4partners' ) ]; foreach ( $available_options as $key => $value ) { $selected = $current_value == $key ? 'selected="selected"' : ''; $options .= ''; } printf( '
%s
', __( 'How often to sync products?', '4partners' ), $options ); } public function sync_callback() { $args = [ 'taxonomy' => 'product_cat', 'hide_empty' => false, 'meta_query' => array( array( 'key' => CategoryService::META_4PARTNERS_ID, 'compare' => 'EXISTS', ) ) ]; global $wpdb; $table = Process::get_sync_table_name(); $datetime = new \DateTime( '7 days ago' ); $versions = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table WHERE created_at > '%s' ORDER BY id DESC", $datetime->format( 'Y-m-d H:i:s' ) ), ARRAY_A ); $categories_by_version = []; foreach ( $versions as $version ) { if ( empty( $categories_by_version[ $version['category_id'] ] ) ) { $version['created_at'] = new \DateTime( $version['created_at'] ); $version['updated_at'] = new \DateTime( $version['updated_at'] ); $categories_by_version[ $version['category_id'] ] = $version; } } $terms = get_terms( $args ); $categories = []; $categories_with_children = []; foreach ( $terms as $term ) { $external_id = get_term_meta( $term->term_id, CategoryService::META_4PARTNERS_ID, true ); $categories_with_children[] = $term->parent; $version = isset( $categories_by_version[ $term->term_id ] ) ? $categories_by_version[ $term->term_id ] : []; $categories[ $term->term_id ] = [ $term->name, $term->parent, $version, $external_id ]; } if ( empty( $categories ) ) { echo '

' . __( 'Please check categories for import from API', '4partners' ) . '

'; } else { ?> $category_data ) { $checked = in_array( $category_id, $this->options['start_sync_categories'] ) ? 'checked' : ''; if ( in_array( $category_id, $categories_with_children ) ) { continue; } $total_categories ++; ?>
id="sync_category_" />
' / ', ] ), ' / ' ); ?>
format( 'd-m-Y' ) ?>
format( 'H:i:s' ) ?>
format( 'd-m-Y' ) ?>
format( 'H:i:s' ) ?>
/
: : / : %
$element ) { $name = $element[0]; $parent_id = intval( $element[1] ); $i ++; echo '
  • '; $checked = isset( $this->options['categories'][ $element_id ] ) ? 'checked' : ''; ?> display_tree_list( $element['children'], $level + 1, $parent_id ); } echo '
  • '; } } public function categories_callback() { $service_locator = Plugin::get_service_locator(); $category_service = $service_locator->get( CategoryService::class ); $categories = $category_service->get_all_from_api(); $external_tree = $category_service->make_category_tree( $categories ); ?>
    display_tree_list( $external_tree ); ?>
    :
    '; $i = 0; foreach ( $array as $element_id => $element ) { $name = $element['name']; $parent_id = intval( $element['parent_id'] ); $i ++; echo '
  • '; $checked = isset( $this->options['categories'][ $element_id ] ) ? 'checked' : ''; ?> display_tree_list( $element['children'], $level + 1, $parent_id ); } echo '
  • '; } echo ''; } public function display_tree_select( array $array, $level = 0, $parent = 0 ) { $i = 0; foreach ( $array as $element_id => $element ) { $name = $element['name']; $parent_id = intval( $element['parent_id'] ); $i ++; ?> display_tree_select( $element['children'], $level + 1, $parent_id ); } } } /** * @param $option * @param false $default * * @return false|mixed */ public function get_option_value( $option, $default = false ) { $all_options = get_option( static::OPTION_NAME, $default ); return isset( $all_options[ $option ] ) ? $all_options[ $option ] : $default; } public function update_option_value( $name, $value ) { $all_options = get_option( static::OPTION_NAME, [] ); $all_options[ $name ] = $value; update_option( static::OPTION_NAME, $all_options ); } /** * @return string[] */ public function get_default_options() { return array( 'api_key' => '', 'cron' => static::CRON_DAILY, 'categories' => [], 'start_sync_categories' => [], CategoryImport::IMPORT_TOKEN_OPTION_NAME => wp_generate_password( 40, false ), ); } public function add_default_options() { $current_options = get_option( static::OPTION_NAME, [] ); $default_options = $this->get_default_options(); if ( empty( $current_options ) ) { add_option( static::OPTION_NAME, $default_options ); } else { foreach ( $default_options as $default_option_key => $default_option_value ) { if ( ! isset( $current_options[ $default_option_key ] ) ) { $current_options[ $default_option_key ] = $default_option_value; } } update_option( static::OPTION_NAME, $current_options ); } } /** * Print the Section text */ public function print_section_info() { //print 'Enter your settings below:'; } }