__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 , 'activate' ) ); // 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.5.2', 'id' => 'content', 'domain' => '', 'css' => true, 'forcesize' => true, 'admin_only' => true, 'homelink' => true, 'scrolltop' => true, 'scrolltime' => 1500, 'transition' => 'slide', 'transtime' => 2000, 'nodeeplink' => false ); return $defaults; } /** * Initialize the default options during plugin activation * * @return none * @since 0.2 */ function activate() { if ( version_compare( PHP_VERSION, '5.2.0', '<' ) ) { deactivate_plugins( $this->plugin_file ); // Deactivate ourself wp_die( "Sorry, but this plugin requires PHP 5.2 or higher." ); } if ( !get_option ( 'ajax-for-all' ) ) add_option ( 'ajax-for-all' , $this->defaults() ); $this->check_upgrade(); } /** * Check if upgrade is necessary * * @since 0.4 * @return none */ function check_upgrade() { $defaults = $this->defaults(); if ( version_compare( $defaults['version'], $this->get_option( 'version' ), '>' ) ) $this->do_upgrade(); } /** * Upgrade * * @since 0.4 * @return none */ function do_upgrade() { $new = $this->defaults(); $old = get_option( 'ajax-for-all' ); if ( $old['id'] != 'content' ) $new['id'] = $old['id']; if ( $old['admin_only'] == 'on' ) $new['admin_only'] = 'on'; if ( $old['homelink'] != 'on' ) $new['homelink'] = ''; update_option( 'ajax-for-all', $new ); } /** * 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"'; ?> />
__( 'No transition', 'ajax-for-all' ), 'slide' => __( 'Slide up/down', 'ajax-for-all' ), 'fade' => __( 'Fade out/in', 'ajax-for-all' ), ); echo ''; _e( 'Pick one of the available transitions', 'ajax-for-all' ); ?>
'; foreach ( $choices as $choice ) { $selected = ''; if ( $choice == $this->get_option( 'transtime' ) ) $selected = ' selected="selected" '; echo ""; } echo ''; ?>
'; foreach ( $choices as $choice ) { $selected = ''; if ( $choice == $this->get_option( 'scrolltime' ) ) $selected = ' selected="selected" '; echo ""; } echo ''; _e( 'Time to scroll up/towards anchors', 'ajax-for-all' ) ?>
options['scrolltop'] == true ) echo ' checked="checked"'; ?> size="25" />
options['nodeeplink'] == true ) echo ' checked="checked"'; ?> size="25" />
options['css'] == true ) echo ' checked="checked"'; ?> size="25" />
options['forcesize'] == true ) echo ' checked="checked"'; ?> size="25" />
options['homelink'] == true ) echo ' checked="checked"'; ?> /> Please consider blogging about this plugin or making a donation if you don't want the link.