options = get_option( 'Amazon Reloaded for WordPress Options' ) ) ) { $this->options = array( 'tld' => 'com', 'id' => '' ); } } /// Hook Functions /** * Hook for activate action. * * Adds the plugin's option to the database. */ function on_activate( ) { if( false === get_option( 'Amazon Reloaded for WordPress Options' ) ) { add_option( 'Amazon Relaoded for WordPress Options', $this->options ); } } /** * Hook for deactivate action. * * Removes the plugin's option from the database. */ function on_deactivate( ) { if( false !== get_option( 'Amazon Reloaded for WordPress Options' ) ) { delete_option( 'Amazon Reloaded for WordPress Options' ); } } /** * Hook for the admin_menu action. * * Enqueues proper javascript files for operation of the plugin. Also adds the appropriate options page * that lets the user manage their Amazon associate id and country. */ function on_admin_menu( ) { // If we're on the write post or write page interface, add the Amazon Reloaded javascript if( $this->is_write_page( ) ) { wp_enqueue_script( 'amazon-reloaded', get_bloginfo( 'wpurl' ) . '/wp-content/plugins/amazon-reloaded-for-wordpress/js/amazon-reloaded.js', array( 'jquery' ) ); } // Add the plugin option page add_options_page( 'Amazon Reloaded', 'Amazon Reloaded', 8, basename( __FILE__ ), array( &$this, 'options_page' ) ); // Add the meta box to the post and page interfaces. It is assigned a type of normal so it appears below the categories add_meta_box( 'Amazon Reloaded', 'Amazon Reloaded', array( &$this, 'meta_box_output' ), 'post', 'normal' ); add_meta_box( 'Amazon Reloaded', 'Amazon Reloaded', array( &$this, 'meta_box_output' ), 'page', 'normal' ); } /** * Hook for the admin_head action. * * Outputs an appropriate link to the Amazon Reloaded for WordPress CSS sheet. */ function on_admin_head( ) { if( $this->is_write_page( ) ) { echo ''; } } /** * Hook for wp_ajax_amazon_reloaded_for_wordpress action. * * Request results from Amazon.com's web services and prints them to output. */ function on_wp_ajax_amazon_reloaded_for_wordpress( ) { $request_url = $this->get_web_service_request_url( ); // We do a manual one-step redirect here if( function_exists( 'curl_init' ) ) { // Start the cURL session $session = curl_init( $request_url ); // Set some cURL options curl_setopt( $session, CURLOPT_HEADER, true ); curl_setopt( $session, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $session, CURLOPT_FOLLOWLOCATION, false ); // Execute the cURL request $redirect_response = curl_exec( $session ); // Grab the location URL from the initial response preg_match( "/Location: (.*)/si", $redirect_response, $matches ); // Closes the initial session curl_close( $session ); // Start the cURL session that will grab the actual results $redirected_session = curl_init( $matches[ 1 ] ); // Set some cURL options curl_setopt( $redirected_session, CURLOPT_HEADER, false ); curl_setopt( $redirected_session, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $redirected_session, CURLOPT_FOLLOWLOCATION, false ); // Execute the cURL request $response = curl_exec( $redirected_session ); // Close the cURL connection curl_close( $redirected_session ); } else if( ini_get( 'allow_url_fopen' ) ) { $response = file_get_contents( $request_url ); } else { // Echo an error message echo "

Your server does not have the cURL extension or allow remote opening of files. WP-Amazon Reloaded will not function.

"; exit( ); } echo $response; exit( ); } /// Utility Functions /** * Returns a boolean value indicating whether the currently requested page is a * part of the write page or write post interface. * * @return boolean true if the currently requested page is a write interface for posts or pages. */ function is_write_page( ) { return strpos( $_SERVER[ 'REQUEST_URI' ], 'post-new.php' ) || strpos( $_SERVER[ 'REQUEST_URI' ], 'page-new.php' ) || strpos( $_SERVER[ 'REQUEST_URI' ], 'post.php' ) || strpos( $_SERVER[ 'REQUEST_URI' ], 'page.php' ); } /** * Returns the appropriate url for a web service request based on POST parameters. * * @return string the appropriate url to use in a web service request. */ function get_web_service_request_url( ) { $xslt_document_location = path_join( get_bloginfo( 'wpurl' ), 'wp-content/plugins/amazon-reloaded-for-wordpress/resources/amazon-reloaded.xslt' ); $request = "http://ecs.amazonaws."; $request .= $this->options[ 'tld' ]; $request .= "/onca/xml?Service=AWSECommerceService"; $request .= "&AWSAccessKeyId=" . urlencode( AMAZON_KEY_ID ); $request .= "&AssociateTag=" . urlencode( $this->options[ 'id' ] ); $request .= "&Version=2008-04-07"; $request .= "&Operation=ItemSearch"; $request .= "&SearchIndex=Blended"; $request .= "&ContentType=text/html"; $request .= "&Style=" . urlencode( $xslt_document_location ); $request .= "&ResponseGroup=Images,ItemAttributes"; $request .= "&Keywords=" . urlencode( $_POST[ 'keywords' ] ); return $request; } /** * Provides the output for the plugin's meta box. */ function meta_box_output( ) { require_once ( path_join( dirname( __FILE__ ), 'pages/meta_box.php' ) ); } /** * Provides the output for the plugin's option page. */ function options_page( ) { require_once ( path_join( dirname( __FILE__ ), 'pages/options.php' ) ); } /** * Provides an easy mechanism to save the options value to the WordPress database * for persistence. * * @return boolean true if the save was successful. */ function save_options( ) { return update_option( 'Amazon Reloaded for WordPress Options', $this->options ); } /** * Setter method for the Amazon associate id option. * * @param string $id the id to be set. */ function set_id( $id ) { $this->options[ 'id' ] = $id; } /** * Setter method for the tld option used to form the correct web services URL. * * @param string $tld the tld to be set. */ function set_tld( $tld ) { $this->options[ 'tld' ] = $tld; } } } if( class_exists( 'Amazon_Reloaded_For_WordPress' ) ) { $amazon_reloaded = new Amazon_Reloaded_For_WordPress( ); } ?>