function output() { echo '<div class="wrap" id="' . $this->key . '">'; echo '<h2>' . dfrapi_setting_pages($this->page) . ' — Datafeedr API</h2>'; if (array_key_exists('dfrapi_api_error', $this->all_networks)) { dfrapi_output_api_error($this->all_networks); } else { echo '<form method="post" action="options.php">'; submit_button(); wp_nonce_field('update-options'); settings_fields($this->page); do_settings_sections($this->page); submit_button(); echo '</form>'; echo '<div id="dfr_unload_message" style="display:none">' . __('You have unsaved changes', DFRAPI_DOMAIN) . '</div>'; } echo '</div>'; }
function last_update_status() { $post = $GLOBALS['post']; $meta = get_post_custom($post->ID); $completed = $meta['_dfrps_cpt_last_update_time_completed'][0]; $update_errors = isset($meta['_dfrps_cpt_errors'][0]) ? maybe_unserialize($meta['_dfrps_cpt_errors'][0]) : ''; // Show last update stats if ($completed == 0) { // Is updating now or has never updated. // Show values from '_dfrps_cpt_previous_update_info' meta field if exists. $meta = isset($meta['_dfrps_cpt_previous_update_info'][0]) ? maybe_unserialize($meta['_dfrps_cpt_previous_update_info'][0]) : array(); } if (!isset($meta['_dfrps_cpt_last_update_time_completed'][0])) { echo __('This Product Set has never been imported.', DFRPS_DOMAIN); } else { if (version_compare(phpversion(), '5.3.0', '>=')) { $datetime1 = new DateTime('@' . $meta['_dfrps_cpt_last_update_time_started'][0]); $datetime2 = new DateTime('@' . $meta['_dfrps_cpt_last_update_time_completed'][0]); $interval = $datetime1->diff($datetime2); $elapsed = $interval->format('%y years, %m months, %a days, %h hours, %i minutes, %S seconds'); $elapsed = str_replace(array('0 years,', ' 0 months,', ' 0 days,', ' 0 hours,', ' 0 minutes,'), '', $elapsed); $elapsed = str_replace(array('1 years, ', ' 1 months, ', ' 1 days, ', ' 1 hours, ', ' 1 minutes'), array('1 year, ', '1 month, ', ' 1 day, ', ' 1 hour, ', ' 1 minute'), $elapsed); } if (version_compare(phpversion(), '5.3.0', '>=')) { $elapsed_row = ' <tr class="alternate"> <td class="row-title">' . __('Elapsed Time', DFRPS_DOMAIN) . '</td> <td class="desc">' . $elapsed . '</td> </tr> '; } if (is_array($update_errors) && !empty($update_errors)) { echo dfrapi_output_api_error($update_errors); } echo ' <table class="widefat last_update_table" cellspacing="0"> <tbody> <tr class="alternate"> <td class="row-title">' . __('Started', DFRPS_DOMAIN) . '</td> <td class="desc">' . date_i18n(get_option('date_format') . ' @ ' . get_option('time_format'), $meta['_dfrps_cpt_last_update_time_started'][0]) . '</td> </tr> <tr> <td class="row-title">' . __('Completed', DFRPS_DOMAIN) . '</td> <td class="desc">' . date_i18n(get_option('date_format') . ' @ ' . get_option('time_format'), $meta['_dfrps_cpt_last_update_time_completed'][0]) . '</td> </tr> ' . $elapsed_row . ' <tr> <td class="row-title">' . __('API Requests', DFRPS_DOMAIN) . '</td> <td class="desc">' . number_format($meta['_dfrps_cpt_last_update_num_api_requests'][0]) . '</td> </tr> <tr class="alternate"> <td class="row-title">' . __('Products Added', DFRPS_DOMAIN) . '</td> <td class="desc">' . number_format($meta['_dfrps_cpt_last_update_num_products_added'][0]) . '</td> </tr> <tr> <td class="row-title">' . __('Products Deleted', DFRPS_DOMAIN) . '</td> <td class="desc">' . number_format($meta['_dfrps_cpt_last_update_num_products_deleted'][0]) . '</td> </tr> </tbody> </table> '; } }
/** * 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; }