function phase3() { $this->phase = 3; $this->count_iteration(); // Get included IDs and remove any duplicates or empty values. $ids = get_post_meta($this->set['ID'], '_dfrps_cpt_manually_added_ids', true); $ids = array_filter((array) $ids); // If no IDs, update phase and go to Phase 3. if (empty($ids)) { return 'skip'; } // Query API //$data = dfrapi_api_get_products_by_id( $ids, $this->config['num_products_per_update'], $this->meta['_dfrps_cpt_offset'][0] ); $data = dfrapi_api_get_products_by_id($ids, $this->config['num_products_per_api_request'], $this->meta['_dfrps_cpt_offset'][0]); // Update number of API requests. update_post_meta($this->set['ID'], '_dfrps_cpt_last_update_num_api_requests', $this->meta['_dfrps_cpt_last_update_num_api_requests'][0] + 1); // Handle errors & return. if (is_array($data) && array_key_exists('dfrapi_api_error', $data)) { update_post_meta($this->set['ID'], '_dfrps_cpt_errors', $data); $this->handle_error($data); return 'repeat'; } // Delete any errors that are currently being stored. update_post_meta($this->set['ID'], '_dfrps_cpt_errors', false); // If there are products, store product data in options table. if (isset($data['products']) && !empty($data['products'])) { foreach ($data['products'] as $product) { $this->insert_temp_product($product); } } // All products in this batch have been imported into the options table. Now update some meta stuff. update_post_meta($this->set['ID'], '_dfrps_cpt_offset', $this->meta['_dfrps_cpt_offset'][0] + 1); update_post_meta($this->set['ID'], '_dfrps_cpt_last_update_num_products_added', $this->meta['_dfrps_cpt_last_update_num_products_added'][0] + count($data['products'])); // If the number of product IDs is less than the number of products per update // (that means we've queried ALL product IDs). // Move to next phase. $offset = $this->config['num_products_per_api_request'] * ($this->meta['_dfrps_cpt_offset'][0] - 1); $length = $this->config['num_products_per_api_request']; $current_range_of_ids = array_slice($ids, $offset, $length); if (count($current_range_of_ids) < $this->config['num_products_per_api_request']) { update_post_meta($this->set['ID'], '_dfrps_cpt_offset', 1); return 'ready'; } return 'repeat'; }
/** * This function is used to determine the context of * how we should return the products and load the * necessary function to get the products. * * This is ONLY called via an AJAX request, not * directly from another function. */ function dfrps_ajax_get_products() { /** * Possible $_REQUEST values: * * query - This is the API query (multiple filters) * ids - An array of product IDs. * postid - This is the post ID of the Product Set. * page - This is the page number being requested for pagination. * context - This will determine how to out put the list of products and pagination. */ check_ajax_referer('dfrps_ajax_nonce', 'dfrps_security'); // Set $postid variable. $postid = isset($_REQUEST['postid']) && $_REQUEST['postid'] > 0 ? $_REQUEST['postid'] : false; // If $postid doesn't validate, show error. if (!$postid) { _e('No post ID provided. A post ID is required.', DFRPS_DOMAIN); die; } // Possible contexts. $possible_contexts = array('div_dfrps_tab_search', 'div_dfrps_tab_saved_search', 'div_dfrps_tab_included', 'div_dfrps_tab_blocked'); // Set $context variable. $context = isset($_REQUEST['context']) && in_array($_REQUEST['context'], $possible_contexts) ? $_REQUEST['context'] : false; // If $context doesn't validate, show error. if (!$context) { _e('No context provided. Context is required.', DFRPS_DOMAIN); die; } // Set $page variable. $page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1; // Get "num_products_per_search" value for $limit value. $configuration = (array) get_option('dfrps_configuration', array('num_products_per_search' => 10)); $limit = $configuration['num_products_per_search']; // Initialize $args array. $args = array(); // Set offset. $offset = $page > 0 ? ($page - 1) * $limit : 0; // Make sure $limit doesn't go over 10,000. if ($offset + $limit > 10000) { $limit = 10000 - $offset; } // Default $args to pass to any function $args['limit'] = $limit; $args['offset'] = $offset; /** * Based on $context, determine what to do next. */ // Context is "div_dfrps_tab_search" if ($context == 'div_dfrps_tab_search') { /** * A search was performed. We need to save the query * so that it can be requested on subsequent paginated pages. */ // Save post if query is performed but post has not been saved at all. if (get_post_status($postid) == 'auto-draft' && $postid > 0) { $timezone_format = _x('Y-m-d G:i:s', 'timezone date format'); $post = array('ID' => $postid, 'post_title' => __('Auto Save', DFRPS_DOMAIN) . ' - ' . date_i18n($timezone_format), 'post_status' => 'draft'); wp_update_post($post); } // Isolate the query if (isset($_REQUEST['query'])) { parse_str($_REQUEST['query'], $query); } else { $query = array(); $query['_dfrps_cpt_query'] = array(); } // If query is not empty, store it as the temp query. if (!empty($query['_dfrps_cpt_query'])) { // Query exists so save it. $temp_query = $query['_dfrps_cpt_query']; update_post_meta($postid, '_dfrps_cpt_temp_query', $temp_query); } else { // No query exists so grab the last stored query. $temp_query = get_post_meta($postid, '_dfrps_cpt_temp_query', true); } // Get manually blocked product IDs. $blocked = get_post_meta($postid, '_dfrps_cpt_manually_blocked_ids', true); if (is_array($blocked) && !empty($blocked)) { $manually_blocked = $blocked; } else { $manually_blocked = array(); } // Add "manually_excluded" to the $args. $args['manually_blocked'] = $manually_blocked; // Query API if a temp query exists. if (!empty($temp_query)) { $data = dfrapi_api_get_products_by_query($temp_query, $limit, $page, $manually_blocked); //$data = dfrps_api_get_products_by_query( $temp_query, $postid, $context, $page ); } // Print any errors. if (is_array($data) && array_key_exists('dfrapi_api_error', $data)) { echo dfrapi_output_api_error($data); die; } // Add a few more helpful values to the $data array; $data['page'] = $page; $data['postid'] = $postid; $data['limit'] = $limit; $data['offset'] = $offset; echo dfrps_format_product_list($data, $context); die; } // if ( $context == 'div_dfrps_tab_search' ) // Context is "div_dfrps_tab_saved_search" if ($context == 'div_dfrps_tab_saved_search') { // Get query $saved_query = get_post_meta($postid, '_dfrps_cpt_query', true); // Get manually blocked product IDs. $blocked = get_post_meta($postid, '_dfrps_cpt_manually_blocked_ids', true); if (is_array($blocked) && !empty($blocked)) { $manually_blocked = $blocked; } else { $manually_blocked = array(); } // Add "manually_excluded" to the $args. $args['manually_blocked'] = $manually_blocked; // Query API if a saved query exists. if (!empty($saved_query)) { $data = dfrapi_api_get_products_by_query($saved_query, $limit, $page, $manually_blocked); //$data = dfrps_api_get_products_by_query( $saved_query, $postid, $context, $page ); } // Print any errors. if (isset($data) && is_array($data) && array_key_exists('dfrapi_api_error', $data)) { echo dfrapi_output_api_error($data); die; } // Add a few more helpful values to the $data array; $data['page'] = $page; $data['postid'] = $postid; $data['limit'] = $limit; $data['offset'] = $offset; // Update number of products in this saved search. //update_post_meta( $postid, '_dfrps_cpt_saved_search_num_products', intval( $data['found_count'] ) ); echo dfrps_format_product_list($data, $context); die; } // if ( $context == 'div_dfrps_tab_saved_search' ) // Context is "div_dfrps_tab_included" if ($context == 'div_dfrps_tab_included') { $ids = get_post_meta($postid, '_dfrps_cpt_manually_added_ids', true); $ids = array_filter((array) $ids); // Query API if IDs exists. if (!empty($ids)) { $data = dfrapi_api_get_products_by_id($ids, $limit, $page); } // Print any errors. if (isset($data) && is_array($data) && array_key_exists('dfrapi_api_error', $data)) { echo dfrapi_output_api_error($data); die; } // Add a few more helpful values to the $data array; $data['page'] = $page; $data['postid'] = $postid; $data['limit'] = $limit; $data['offset'] = $offset; echo dfrps_format_product_list($data, $context); die; } // if ( $context == 'div_dfrps_tab_included' ) // Context is "div_dfrps_tab_blocked" if ($context == 'div_dfrps_tab_blocked') { $ids = get_post_meta($postid, '_dfrps_cpt_manually_blocked_ids', true); $ids = array_filter((array) $ids); // Query API if IDs exists. if (!empty($ids)) { $data = dfrapi_api_get_products_by_id($ids, $limit, $page); } // Print any errors. if (isset($data) && is_array($data) && array_key_exists('dfrapi_api_error', $data)) { echo dfrapi_output_api_error($data); die; } // Add a few more helpful values to the $data array; $data['page'] = $page; $data['postid'] = $postid; $data['limit'] = $limit; $data['offset'] = $offset; echo dfrps_format_product_list($data, $context); die; } // if ( $context == 'div_dfrps_tab_blocked' ) { echo 'Uh-oh. Something went wrong.'; die; }