api = [ 'trading' => new Trading_API(), 'finding' => new Finding_API(), 'shopping' => new Shopping_API() ]; $this->db = new Database(); } public function form_new() { $step = ( isset( $_GET[ 'step' ] ) ) ? $_GET[ 'step' ] : '1'; if( $step < 1 || $step > 1 ) { return; } switch( $step ) { case 1: $this->form_new_step_1(); break; } } public function form_edit() { $step = ( isset( $_GET[ 'step' ] ) ) ? $_GET[ 'step' ] : '1'; if( $step < 1 || $step > 1 ) { return; } switch( $step ) { case 1: $this->form_edit_step_1(); break; } } public function form_cats() { $step = ( isset( $_GET[ 'step' ] ) ) ? $_GET[ 'step' ] : '1'; $token = $this->api[ 'trading' ]->get_token(); if( $step < 1 || $step > 1 ) { return; } switch( $step ) { case 1: if ( $token === '' ) { $this->form_cats_get_auth(); } else { $this->form_cats_step_1(); } break; } } public function form_delete() { $step = ( isset( $_GET[ 'step' ] ) ) ? $_GET[ 'step' ] : '1'; if( $step < 1 || $step > 1 ) { return; } switch( $step ) { case 1: $this->form_delete_step_1(); break; } } private function form_new_step_1() { $categories = $this->categories_as_options( $this->api[ 'trading' ]->get_categories() ); $conditions = $this->conditions_as_options(); ?>
categories_as_options( $this->api[ 'trading' ]->get_categories(), $this->settings[ 'default_categories' ] ); $conditions = $this->conditions_as_options( $this->settings[ 'default_condition' ] ); ?>
api[ 'trading' ]->get_session_id(); ?>
'disabled' ] ); ?>
get_local_category_list_version(); $ebay_version = $this->api[ 'trading' ]->make_call( 'get_ebay_category_list_version' ); $ebay_version = $ebay_version->get_payload(); if ( $local_version != $ebay_version ) { $download = true; $title = 'Step 1'; $action = 'cats'; $step = '2'; } else { $download = false; $title = 'Up-To-Date'; $action = ''; $step = ''; } ?>
   
'trading', 'call' => 'GetSessionID', ]; $this->api->call( $args ); return; } private function get_categories() { $args = [ 'call-type' => 'trading', 'call' => 'GetCategories', ]; $this->api->call( $args ); return; } public function get_categories_as_options( $input = [] ) { $selected = ( $input !== [] ) ? $input[ 'shortcode-categories' ] : $this->settings[ 'default_categories' ]; return $this->categories_as_options( $this->api[ 'trading' ]->get_categories(), $selected ); } private function categories_as_options( $cats, $selected_cats = [] ) { if ( is_null( $selected_cats ) ) { $selected_cats = []; } $opts = ''; $cur_lvl = 0; foreach( $cats as $cat ) { if ( $cat->category_level !== $cur_lvl ) { $cur_lvl = $cat->category_level; if ( $cur_lvl === '1' ) { $label = 'Top-Level Categories'; } else { $opts .= ''; $label = 'Level ' . $cur_lvl . ' Categories'; } $opts .= ''; } $selected = ( in_array( $cat->category_id, $selected_cats ) ) ? ' selected' : ''; $opts .= ''; } return $opts; } private function conditions_as_options( $sel_conds = [] ) { $conds = [ '1000' => 'New', '2000' => 'Certified Refurbished', '3000' => 'Used', '4000' => 'Very Good', '5000' => 'Good', '6000' => 'Acceptable', '7000' => 'For Parts or Not Working' ]; if ( $sel_conds === [] ) { $sel_conds = $conds; } $opts = ''; foreach( $conds as $val => $cond ) { $selected = ( in_array( $val, $sel_conds ) ) ? ' selected' : ''; $opts .= ''; } return $opts; } private function set_success( $success ) { if ( count( $success ) ) { foreach( $success as $code => $msg ) { add_settings_error( self::OPT, $code, $msg, 'notice-success is-dismissible' ); } } } private function set_errors( $errors ) { if ( count( $errors ) ) { foreach( $errors as $code => $msg ) { add_settings_error( self::OPT, $code, $msg, 'notice-error is-dismissible' ); } } } private function set_warnings( $warnings ) { if ( count( $warnings ) ) { foreach( $warnings as $code => $msg ) { add_settings_error( self::OPT, $code, $msg, 'notice-warning is-dismissible' ); } } } public function save_as_new_source() { $settings = [ 'app_id' => $_POST[ 'app_id' ], 'default_categories' => $_POST[ 'categories' ], 'default_condition' => $_POST[ 'condition' ], ]; $data = [ 'source' => 'ebay', 'settings' => serialize( $settings ), 'priority' => 0 ]; $this->db->insert( 'sources', $data ); $this->set_success( [ 'saved' => __( 'eBay Settings saved successfully.' ) ] ); } public function save_existing_source() { $settings = [ 'app_id' => $_POST[ 'app_id' ], 'default_categories' => $_POST[ 'categories' ], 'default_condition' => $_POST[ 'condition' ], ]; $data = [ 'source' => 'ebay', 'settings' => serialize( $settings ), 'priority' => 0 ]; $where = [ 'id' => $_POST[ 'source-id' ] ]; $this->db->update( 'sources', $where, $data ); $this->set_success( [ 'saved' => __( 'eBay Settings saved successfully.' ) ] ); } public function download_categories() { $version = $_POST[ 'version' ]; $categories = $this->api[ 'trading' ]->make_call( 'get_ebay_category_list' ); $categories = $categories->get_payload(); $this->save_categories( $version, $categories ); ?>
$_POST[ 'source-id' ] ]; $this->db->delete( 'sources', $where ); $this->set_success( [ 'saved' => __( 'eBay source removed successfully.' ) ] ); } private function get_local_category_list_version() { $wheres = [ 0 => [ 'column' => 'id', 'operator' => '=', 'condition' => '1' ] ]; $results = $this->db->get( 'ebay_cats', $wheres ); if ( is_array( $results ) && isset( $results[ 0 ]->version ) ) { return $results[ 0 ]->version; } else { return false; } } private function save_categories( $version, $categories ) { $now = new DateTime(); $data = [ 'category_id' => '', 'category_level' => '', 'category_name' => $now->format( 'Y-m-d H:i:s' ), 'parent_id' => '', 'version' => $version, ]; $this->db->insert( 'ebay_cats', $data ); $data = []; foreach( $categories as $cat ) { $data = []; $data = [ 'category_id' => $cat[ 'category_id' ], 'category_level' => $cat[ 'category_level' ], 'category_name' => $cat[ 'category_name' ], 'parent_id' => $cat[ 'parent_id' ], 'version' => $version ]; $this->db->insert( 'ebay_cats', $data ); } } public function call_finding_api( $entries_per_page, $keywords, $cats, $condition ) { $app_id = $this->randomize_app_id(); $more_args = [ 'sortOrder' => 'CurrentPriceHighest', 'paginationInput.entriesPerPage' => $entries_per_page, 'HideDuplicateItems' => 'true', 'keywords' => urlencode($keywords) . urlencode(' -(frame,Frameset)'), 'itemFilter(0).name' => 'HideDuplicateItems', 'itemFilter(0).value' => 'true', ]; if ( is_array( $cats ) && count( $cats ) ) { foreach( $cats as $key => $cat ) { $more_args[ 'categoryId(' . $key . ')' ] = $cat; } } if ( $condition === "New" ) { $more_args[ 'itemFilter(1).name' ] = 'Condition'; $more_args[ 'itemFilter(1).value' ] = 'New'; } elseif ( $condition === "Used" ) { $more_args[ 'itemFilter(1).name' ] = 'Condition'; $more_args[ 'itemFilter(1).value' ] = 'Used'; } $args = [ 'operation' => 'findItemsAdvanced', 'version' => '1.11.0', 'app-id' => $app_id, 'global-id' => 'EBAY-US', 'format' => 'JSON', 'more-args' => $more_args ]; return $this->api[ "finding" ]->make_call( $args ); } private function randomize_app_id() { $date = new DateTime(); $time = intval( $date->format( 'v' ) ); if ( $time < 160 ) { return self::APP_ID; } else { return $this->settings[ 'app_id' ]; } } } }