'com', 'public_key' => '', 'private_key' => '', 'associate_tag' => '', 'keywords' => '', 'search_index' => '', 'unit' => 'List', 'units' => array(), 'use_tags' => 1, 'insert_first' => 0, 'insert_last' => 0, 'version' => AMZRP_VERSION ); if ($default) { update_option('amzrp_op', $amzrp_default); $amzrp_options = $amzrp_default; return $amzrp_default; } $options = get_option('amzrp_op'); if (isset($options) && !empty($options)){ return $options; } else { return $amzrp_default; } } // if both logged in and not logged in users can send this AJAX request, // add both of these actions, otherwise add only the appropriate one add_action( 'wp_ajax_nopriv_amzrp_call', 'amzrp_call' ); add_action( 'wp_ajax_amzrp_call', 'amzrp_call' ); function amzrp_call($default = false) { if (!$default) { $nonce = $_POST['amzrp_nonce']; // check to see if the submitted nonce matches with the // generated nonce we created earlier if ( ! wp_verify_nonce( $nonce, 'amzrp-nonce' ) ) die ( 'Busted!'); } global $amzrp_options; $options = $amzrp_options; extract($options); if (!$default){ extract($_POST); } $amzrp = new AMZRP_API_REQUEST( $public_key, $private_key, $associate_tag, $region ); $amzrp_response = $amzrp->getItemByKeyword($keywords, $search_index); if ($amzrp_response){ amzrp_render_ad($unit,$amzrp_response); } else if (!$default){ amzrp_call(true); } // IMPORTANT: don't forget to "exit" exit; } function amzrp_render_ad($unit,$amzrp_items) { global $amzrp_units; if (!$amzrp_units->units[$unit]->is_enabled()){ $unit = $amzrp_units->default_unit; } $amzrp_units->units[$unit]->render($amzrp_items); } /** * The Sortcode * */ add_shortcode('amz-related-products', 'amzrp_sc'); function amzrp_sc($atts) { global $post; global $amzrp_options; global $wp_scripts; global $amzrp_ads_number; global $amzrp_units; $options = $amzrp_options; extract($options); $ad_id = wp_generate_password(3, false); $tags_str = ""; if ($use_tags){ $posttags = get_the_tags(); if ($posttags) { $i = 0; foreach($posttags as $tag) { $tags_str .= $tag->name. ','; if ($i == 2) break; $i++; } } } if (empty($tags_str)){ $tags_str = $keywords; } $amzrp_default_sc =array( 'id' => $ad_id, 'keywords' => $tags_str, 'search_index' => $search_index, 'unit' => $unit ); $final_atts = shortcode_atts($amzrp_default_sc, $atts); extract($final_atts); $json_request = json_encode(array_merge($final_atts, array( 'action' =>'amzrp_call', 'div_pref' => 'ad-', "amzrp_nonce" => wp_create_nonce('amzrp-nonce') ))); if (!wp_script_is( 'amzrp-ajax-request')){ wp_enqueue_script( 'amzrp-ajax-request', plugin_dir_url( __FILE__ ) . 'js/amzrp.0.1.js', array( 'jquery' ) ); $data = $wp_scripts->get_data('amzrp-ajax-request', 'data')."\n"; $data .= "var amzrpUrl = '".admin_url( 'admin-ajax.php' )."'; \n"; $data .= "var amzrpAds = new Array(); \n"; $data .= "amzrpAds[".$amzrp_ads_number++."] = ".$json_request."; \n"; $wp_scripts->add_data('amzrp-ajax-request', 'data',$data); } else { $data = $wp_scripts->get_data('amzrp-ajax-request', 'data'); $data .= "amzrpAds[".$amzrp_ads_number++."] = ".$json_request."; \n"; $wp_scripts->add_data('amzrp-ajax-request', 'data',$data); } $output = ""; if (array_key_exists($unit,$amzrp_units->units)){ $unit_to_render = $amzrp_units->units[$unit]; $is_enabled = $unit_to_render->is_enabled(); if (empty($is_enabled) || !$unit_to_render->is_enabled()) { $unit_to_render = $amzrp_units->units[$amzrp_units->default_unit]; } $output .= $unit_to_render->pre_render($ad_id); } return $output; } /** * Automatization * */ add_filter( 'the_content', 'amzrp_add_shc', 20 ); function amzrp_add_shc( $content ) { if( !is_single() ) { return $content; } else { global $amzrp_options; $options = $amzrp_options; extract($options); $new_content = ""; if ($insert_first){ $new_content = do_shortcode("[amz-related-products]").$content; } else { $new_content = $content; } if ($insert_last) { $new_content = $new_content.do_shortcode("[amz-related-products]"); } // Returns the content. return $new_content; } } /** * Admin * */ if ( is_admin() ){ // admin actions // Create menu add_action( 'admin_menu', 'amzrp_plugin_menu' ); // Adding WordPress plugin action links add_filter( 'plugin_action_links_' . AMZRP_PLUGIN_FILE, 'amzrp_plugin_action_links' ); // Adding WordPress plugin meta links add_filter( 'plugin_row_meta', 'amzrp_plugin_meta_links', 10, 2 ); // Ajax request to after region selection add_action( 'wp_ajax_amzrp_select_region', 'amzrp_select_region' ); // Ajax request previews add_action( 'wp_ajax_amzrp_previews', 'amzrp_previews' ); // Enqueue admin script add_action( 'admin_enqueue_scripts', 'amzrp_admin_enqueue_scripts' ); } function amzrp_admin_enqueue_scripts($hook) { if( 'settings_page_amz-related-products' == $hook || 'media-upload-popup' == $hook ) { wp_enqueue_script( 'amzrp_admin_script', plugin_dir_url( __FILE__ ) . 'js/amzrp.admin.js', array( 'jquery' ) ); wp_localize_script( 'amzrp_admin_script', 'amzrpSettings', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'amzrp_admin_nonce' => wp_create_nonce('amzrp-admin-nonce'), 'action' => 'amzrp_select_region') ); wp_register_style( 'amzrp_admin_style', plugins_url('/styles/amzrp-admin-style.css', __FILE__) ); wp_enqueue_style( 'amzrp_admin_style' ); do_action('amzrp_admin_unit_script'); } else { return; } } function amzrp_select_region(){ $nonce = $_POST['amzrp_admin_nonce']; // check to see if the submitted nonce matches with the // generated nonce we created earlier if ( ! wp_verify_nonce( $nonce, 'amzrp-admin-nonce' ) ) die ( 'Busted!'); // generate the response $response = json_encode( amzrp_get_search_index($_POST['region']) ); // response output header( "Content-Type: application/json" ); echo $response; // IMPORTANT: don't forget to "exit" exit; } function amzrp_previews(){ $nonce = $_POST['amzrp_admin_nonce']; // check to see if the submitted nonce matches with the // generated nonce we created earlier if ( ! wp_verify_nonce( $nonce, 'amzrp-admin-nonce' ) ) die ( 'Busted!'); extract($_POST); $amzrp = new AMZRP_API_REQUEST( $public_key, $private_key, $associate_tag, $region ); $amzrp_response = $amzrp->getItemByKeyword($keywords, $search_index); if ($amzrp_response){ amzrp_render_ad($unit,$amzrp_response); } else { _e("

Error, your request failed. Please, check the following items: