__construct(); } /** * Setup backend functionality in WordPress * * @return none * @since 0.2 */ function __construct () { AjaxForAll::__construct (); // Load localizations if available load_plugin_textdomain ( 'ajax-for-all' , false , 'ajax-for-all/translations' ); // Activation hook register_activation_hook ( $this->plugin_file , array ( &$this , 'init' ) ); // Whitelist options add_action ( 'admin_init' , array ( &$this , 'register_settings' ) ); // Activate the options page add_action ( 'admin_menu' , array ( &$this , 'add_page' ) ) ; // Enable ajax handler add_action( 'wp_ajax_ajax_for_all', array( &$this, 'ajax' ) ); add_action( 'wp_ajax_nopriv_ajax_for_all', array( &$this, 'ajax' ) ); } /** * Whitelist the AjaxForAll options * * @since 0.2 * @return none */ function register_settings () { register_setting( 'ajax-for-all_options' , 'ajax-for-all' ); } /** * Return plugin default config * * @since 0.2 * @return array */ function defaults () { $defaults = array ( 'version' => '0.3', 'id' => 'content', 'domain' => '', 'css' => true, 'admin_only' => true, 'homelink' => true, ); return $defaults; } /** * Initialize the default options during plugin activation * * @return none * @since 0.2 */ function init() { if ( !get_option ( 'ajax-for-all' ) ) add_option ( 'ajax-for-all' , $this->defaults() ); } /** * Add the options page * * @return none * @since 0.2 */ function add_page() { if ( current_user_can ( 'manage_options' ) && function_exists ( 'add_options_page' ) ) { $options_page = add_options_page ( __( 'Ajax For All' , 'ajax-for-all' ) , __( 'Ajax For All' , 'ajax-for-all' ) , 'manage_options' , 'ajax-for-all' , array ( &$this , 'admin_page' ) ); add_action( 'admin_head-' . $options_page, array( &$this, 'css' ) ); add_filter( 'ozh_adminmenu_icon_ajax-for-all', array ( &$this , 'icon' )); } } /** * Load admin CSS style * * @since 0.2 * @todo isn't there some admin enqueue style function? */ function css() { ?> plugin_url(); $url .= '/images/transmit_blue.png'; return $url; } /** * The main ajax handler. Parse the ajax request, fetch remote content, return * * @param none * * @return mixed array with misc. info * * @since 0.1 * * @todo pass request, cookie etc parameters * @todo fail if POST, can we pass that on? */ function ajax() { $success = false; $jump = false; $jumpto = false; $url = filter_var( $_REQUEST['href'] ); $user = filter_var( $_REQUEST['user'] ); $nonce = filter_var( $_REQUEST['nonce'] ); if ( // @todo functions strpos( $url, get_bloginfo( 'url' ) ) === 0 && strpos( $url, '/wp-admin/' ) === false ) { $success = true; $content = $this->fetch_url( $url, $user, $nonce ); $content = $this->extract_id( $content, $this->get_option( 'id' ) ); $parsed = parse_url( $url ); if ( $parsed['fragment'] ) { $jump = true; $jumpto = $parsed['fragment']; } if ( !$content ) $success = false; } $return = json_encode( array( 'success' => $success, 'content' => $content, 'href' => $url, 'jump' => $jump, 'jumpto' => $jumpto, ) ); die( $return ); } /** * Fetch remote content * * @param string $url the link to fetch * * @return string HTML content * * @since 0.1 * * @todo request, cookie etc */ function fetch_url( $url, $user, $nonce ) { // add user / nonce parameters $parsed_url = parse_url( $url ); $sep = '?'; if ( $parsed_url['query'] ) $sep = '&'; if ( $user ) $url .= $sep . "ajax_for_all_curl_user=$user&ajax_for_all_curl_nonce=$nonce"; $ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); // This is for my encrypted and password-protected dev server if ( defined( 'AFA_HTTP_CREDENTIALS' ) ) { curl_setopt( $ch, CURLOPT_USERPWD, AFA_HTTP_CREDENTIALS ); curl_setopt( $ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ); curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false); } $result = curl_exec ( $ch ); $response = curl_getinfo( $ch ); curl_close ( $ch ); return $result; } /** * Extract an id from an HTML document * * @param string $content website's HTML content * * @return string one id's HTML content * * @since 0.1 */ function extract_id( $content, $id ) { // thanks http://codjng.blogspot.com/2009/10/unicode-problem-when-using-domdocument.html if ( function_exists( 'mb_convert_encoding' ) ) $content = mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'); // require mb_string $dom= new DOMDocument(); error_reporting( 0 ); $dom->loadHTML( $content ); error_reporting( 1 ); $dom->preserveWhiteSpace = false; $element = $dom->getElementById( $id ); $innerHTML = $this->innerHTML( $element ); return( $innerHTML ); } /** * Helper, returns the inner HTML of an element * * @param object DOMElement (i think) * * @return string one id's HTML content * * @since 0.1 */ function innerHTML( $contentdiv ) { $els = $contentdiv->childNodes; foreach( $els as $el ) { if ( $el->nodeType == XML_TEXT_NODE ) { $text = $el->nodeValue; $text = str_replace( '<', '<', $text ); $r .= $text; } // FIXME we should return comments elseif ( $el->nodeType == XML_COMMENT_NODE ) { $r .= ''; } else { $r .= '<'; $r .= $el->nodeName; if ( $el->hasAttributes() ) { $atts = $el->attributes; foreach ( $atts as $att ) $r .= " {$att->nodeName}='{$att->nodeValue}'" ; } $r .= '>'; $r .= $this->innerHTML( $el ); $r .= "nodeName}>"; } } return $r; } /** * Output the options page * * @return none * @since 0.2 */ function admin_page () { ?>

options['admin_only'] == true ) echo ' checked="checked"'; ?> /> The admin only mode is the default so that you can test if the plugin works with your site. Disable it so that your visitors can use the Ajax feature. You might want to disable the link to the plugin's page in your site's footer before doing this.
The default 'content' should work with most themes.
options['css'] == true ) echo ' checked="checked"'; ?> size="25" /> Disable this if your layout breaks. But you will need to style the spinner yourself then.
options['homelink'] == true ) echo ' checked="checked"'; ?> /> Please consider blogging about this plugin or making a donation if you don't want the link.