Documentation here. Version: 5 alpha 2 Author: Casey Bisson Author URI: http://maisonbisson.com/blog/ */ class bSuite { function bSuite(){ global $wpdb; $this->search_table = $wpdb->prefix . 'bsuite4_search'; $this->hits_incoming = ( empty( $wpdb->base_prefix ) ? $wpdb->prefix : $wpdb->base_prefix ) .'bsuite4_hits_incoming'; $this->hits_terms = ( empty( $wpdb->base_prefix ) ? $wpdb->prefix : $wpdb->base_prefix ) .'bsuite4_hits_terms'; $this->hits_targets = ( empty( $wpdb->base_prefix ) ? $wpdb->prefix : $wpdb->base_prefix ) .'bsuite4_hits_targets'; $this->hits_searchphrases = ( empty( $wpdb->base_prefix ) ? $wpdb->prefix : $wpdb->base_prefix ) .'bsuite4_hits_searchphrases'; // $this->hits_searchwords = ( empty( $wpdb->base_prefix ) ? $wpdb->prefix : $wpdb->base_prefix ) .'bsuite4_hits_searchwords'; $this->hits_sessions = ( empty( $wpdb->base_prefix ) ? $wpdb->prefix : $wpdb->base_prefix ) .'bsuite4_hits_sessions'; $this->hits_shistory = ( empty( $wpdb->base_prefix ) ? $wpdb->prefix : $wpdb->base_prefix ) .'bsuite4_hits_shistory'; $this->hits_pop = ( empty( $wpdb->base_prefix ) ? $wpdb->prefix : $wpdb->base_prefix ) .'bsuite4_hits_pop'; $this->loadavg = $this->get_loadavg(); // establish web path to this plugin's directory $this->path_web = plugins_url( basename( dirname( __FILE__ ))); $this->is_quickview = FALSE; // register and queue javascripts wp_register_script( 'bsuite', $this->path_web . '/js/bsuite.js', array('jquery'), '20080503' , TRUE ); wp_enqueue_script( 'bsuite' ); // jQuery text highlighting plugin http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html wp_register_script( 'highlight', $this->path_web . '/js/jquery.highlight-1.js', array('jquery'), '1' , TRUE ); wp_enqueue_script( 'highlight' ); // is this wpmu? if( function_exists( 'is_site_admin' )) $this->is_mu = TRUE; else $this->is_mu = FALSE; if ( isset( $_GET['bsuite_mycss'] ) && !is_admin() ) add_action( 'init', array( &$this, 'bsuite_mycss_printstyles' )); // // register hooks // // shortcodes add_shortcode('include', array(&$this, 'shortcode_include')); add_shortcode('icon', array(&$this, 'shortcode_icon')); add_shortcode('feed', array(&$this, 'shortcode_feed')); // filter the_excerpt and x_rss through do_shortcode(). wish this was in core // http://trac.wordpress.org/ticket/7093 add_filter('the_content_rss', 'do_shortcode', 11); add_filter('the_excerpt', 'do_shortcode', 11); add_filter('the_excerpt_rss', 'do_shortcode', 11); add_filter('widget_text', 'do_shortcode', 11); // bsuite post icons add_action('wp_ajax_bsuite_icon_form', array( &$this, 'icon_ajax_form' )); add_action('wp_ajax_bsuite_icon_upload', array( &$this, 'icon_ajax_upload' )); add_action('wp_ajax_bsuite_icon_delete', array( &$this, 'icon_ajax_delete' )); $this->icon_sizes_default(); // initialize default icons // tokens // tokens are deprecated. please use shortcode functionality instead. add_filter('bsuite_tokens', array(&$this, 'tokens_default')); add_filter('the_content', array(&$this, 'tokens_the_content'), 0); add_filter('the_content_rss', array(&$this, 'tokens_the_content_rss'), 0); add_filter('the_excerpt', array(&$this, 'tokens_the_excerpt'), 0); add_filter('the_excerpt_rss', array(&$this, 'tokens_the_excerpt_rss'), 0); add_filter('get_the_excerpt ', array(&$this, 'tokens_the_excerpt'), 0); add_filter('widget_text', array(&$this, 'tokens'), 0); // bsuggestive related posts add_filter('save_post', array(&$this, 'bsuggestive_delete_cache')); if( get_option( 'bsuite_insert_related' )){ add_filter('the_content', array(&$this, 'bsuggestive_bypageviews_the_content'), 5); add_filter('the_content', array(&$this, 'bsuggestive_the_content'), 5); } // sharelinks if( get_option( 'bsuite_insert_sharelinks' )) add_filter('the_content', array(&$this, 'sharelinks_the_content'), 6); // redirection add_filter('template_redirect', array(&$this, 'searchsmart_direct'), 8); add_filter('post_link', array(&$this, 'searchsmart_post_link_direct'), 11, 2); // default CSS if( get_option( 'bsuite_insert_css' )){ add_action('wp_head', 'wp_print_styles', 9); wp_register_style( 'bsuite-default', $this->path_web .'/css/default.css' ); wp_enqueue_style( 'bsuite-default' ); } // bstat add_action('get_footer', array(&$this, 'bstat_js')); // cron add_filter('cron_schedules', array(&$this, 'cron_reccurences')); if( $this->loadavg < get_option( 'bsuite_load_max' )){ // only do cron if load is low-ish add_filter('bsuite_interval', array(&$this, 'bstat_migrator')); } // machine tags add_action('save_post', array(&$this, 'machtag_save_post'), 2, 2); // cms goodies add_filter('user_has_cap', array(&$this, 'edit_current_user_can'), 10, 3); add_filter('save_post', array(&$this, 'edit_publish_page')); // add_action('dbx_page_advanced', array(&$this, 'edit_insert_excerpt_form')); // add_action('dbx_page_sidebar', array(&$this, 'edit_insert_category_form')); // add_action('edit_form_advanced', array(&$this, 'edit_post_form')); // add_action('edit_page_form', array(&$this, 'edit_page_form')); add_action('widgets_init', array(&$this, 'widgets_register')); /* // user-contributed tags add_action('preprocess_comment', array(&$this, 'uctags_preprocess_comment'), 1); */ // activation and menu hooks register_activation_hook(__FILE__, array(&$this, 'activate')); add_action('admin_menu', array(&$this, 'admin_menu_hook')); add_action('admin_init', array(&$this, 'admin_init')); add_action('init', array(&$this, 'init')); // end register WordPress hooks } function admin_init(){ /* // set things up so authors can edit their own pages $role = get_role('author'); if ( ! empty($role) ) { $role->add_cap('edit_pages'); $role->add_cap('edit_published_pages'); } */ register_setting( 'bsuite-options', 'bsuite_insert_related', 'absint' ); register_setting( 'bsuite-options', 'bsuite_insert_sharelinks', 'absint' ); register_setting( 'bsuite-options', 'bsuite_searchsmart', 'absint' ); register_setting( 'bsuite-options', 'bsuite_swhl', 'absint' ); register_setting( 'bsuite-options', 'bsuite_who_can_edit' ); register_setting( 'bsuite-options', 'bsuite_managefocus_month', 'absint' ); register_setting( 'bsuite-options', 'bsuite_managefocus_author', 'absint' ); register_setting( 'bsuite-options', 'bsuite_insert_css', 'absint' ); register_setting( 'bsuite-options', 'bsuite_migration_interval', 'absint' ); register_setting( 'bsuite-options', 'bsuite_migration_count', 'absint' ); register_setting( 'bsuite-options', 'bsuite_load_max', 'absint' ); } function init(){ if( get_option( 'bsuite_mycss_replacethemecss' ) && !is_admin() ){ add_filter( 'stylesheet_uri', array( &$this, 'bsuite_mycss_hidesstylesheet' ), 11 ); add_filter( 'locale_stylesheet_uri', array( &$this, 'bsuite_mycss_hidesstylesheet' ), 11 ); } if(( get_option( 'bsuite_mycss' ) || get_option( 'bsuite_mycss_replacethemecss' )) && !is_admin() ){ wp_register_style( 'bsuite-mycss', get_option('home') .'/index.php?bsuite_mycss=print' ); wp_enqueue_style( 'bsuite-mycss' ); } if( 0 < get_option( 'bsuite_mycss_maxwidth' )) $GLOBALS['content_width'] = absint( get_option( 'bsuite_mycss_maxwidth' )); if( !isset( $GLOBALS['content_width'] )) $GLOBALS['content_width'] = 500; load_plugin_textdomain( 'bsuite', FALSE, basename( dirname( __FILE__ )) .'/lang/' ); /* // handle user-contributed tags via comments if( strpos( $_SERVER['PHP_SELF'], 'wp-comments-post.php' ) && ( !empty( $_REQUEST['bsuite_uctags'] ))) $_REQUEST['comment'] = 'BSUITE_UCTAG'; */ // add_rewrite_endpoint( 'quickview', EP_PERMALINK ); // this doesn't quite work as I want it to } function admin_menu_hook() { if( (( 'edit.php' == basename( $_SERVER['PHP_SELF'] )) || ( 'edit-pages.php' == basename( $_SERVER['PHP_SELF'] ))) && ( !count( $_GET )) ){ global $current_user; if( !current_user_can( 'edit_others_posts' ) ) die( wp_redirect( admin_url( basename( $_SERVER['PHP_SELF'] ) .'?author='. $current_user->id . ( ( get_option( 'bsuite_managefocus_month' ) && ( 'edit-pages.php' <> basename( $_SERVER['PHP_SELF'] )) ) ? '&m='. date( 'Ym' ) : '') ))); die( wp_redirect( admin_url( basename( $_SERVER['PHP_SELF'] ) .'?s'. ( get_option( 'bsuite_managefocus_author' ) ? '&author='. $current_user->id : '' ) . ( ( get_option( 'bsuite_managefocus_month' ) && ( 'edit-pages.php' <> basename( $_SERVER['PHP_SELF'] )) ) ? '&m='. date( 'Ym' ) : '') ))); } /* // the machine tags js and style wp_register_script( 'bsuite-machtags', $this->path_web . '/js/bsuite-machtags.js', array('jquery-ui-sortable'), '1' ); wp_enqueue_script( 'bsuite-machtags' ); wp_register_style( 'bsuite-machtags', $this->path_web .'/css/machtags.css' ); wp_enqueue_style( 'bsuite-machtags' ); */ /* // add the sweet categories and tags JS from the post editor to the page editor wp_register_script( 'edit_page', $this->path_web . '/js/edit_page.js', array('jquery'), '1' ); if( strpos( $_SERVER['REQUEST_URI'], 'admin/page' )){ wp_enqueue_script( 'edit_page' ); wp_enqueue_script( 'jquery-ui-tabs' ); wp_enqueue_script( 'suggest' ); wp_enqueue_script( 'ajaxcat' ); } */ // add the options page add_options_page('bSuite Settings', 'bSuite', 'manage_options', plugin_basename( dirname( __FILE__ )) .'/ui_options.php' ); // the bstat reports are handled in a seperate file add_submenu_page('index.php', 'bSuite bStat Reports', 'bStat Reports', 'edit_posts', plugin_basename( dirname( __FILE__ )) .'/ui_stats.php' ); // the custom css page add_theme_page( __('Custom CSS'), __('Custom CSS'), 'switch_themes', plugin_basename( dirname( __FILE__ )) .'/ui_mycss.php' ); // add the post icon widget to the post and page editors add_meta_box('bsuite_post_icon', __('bSuite Post Icon'), array( &$this, 'icon_editor_iframe' ), 'post', 'advanced', 'high'); add_meta_box('bsuite_post_icon', __('bSuite Post Icon'), array( &$this, 'icon_editor_iframe' ), 'page', 'advanced', 'high'); } // // shortcode functions // function shortcode_icon( $arg ){ // [innerindex ] global $id; $arg = shortcode_atts( array( 'post_id' => $id, 'size' => 's', 'width' => 0, 'height' => 0, ), $arg ); return( $this->icon_get_h( $arg['post_id'], $arg['size'], $arg['width'], $arg['height'] )); } function shortcode_include( $arg ){ // [include ] global $id, $post; $arg = shortcode_atts( array( 'post_id' => FALSE, 'url' => FALSE, 'field' => 'post_excerpt', ), $arg ); if( !( $arg[ 'post_id' ] || $arg[ 'url' ] )) return( FALSE ); if( isset( $arg[ 'url' ] )) $include_id = url_to_postid( $arg[ 'url' ] ); if( (int) $arg[ 'post_id' ] ) $include_id = (int) $arg[ 'post_id' ]; if( !$include_id || ( $id == $include_id )) return( FALSE ); $post_orig = unserialize( serialize( $post )); // how else to prevent passing object by reference? $id_orig = $id; $post = get_post( $arg[ 'post_id' ] ); $id = $post->ID; if( ( 'post_excerpt' == $arg[ 'field' ] ) && !( get_post_field( $arg[ 'field' ], $include_id ))) $arg[ 'field' ] = 'post_content'; $content = apply_filters( 'the_content', get_post_field( $arg[ 'field' ], $include_id )); $post = $post_orig; $id = $id_orig; return( $content ); } function shortcode_feed( $arg ){ // [feed ] $arg = shortcode_atts( array( 'title' => FALSE, 'div_class' => FALSE, 'ul_class' => 'feed', 'ol_class' => FALSE, 'feed_url' => FALSE, 'count' => 5, 'template' => '
  • %%title%%

    %%content%%

  • ', ), $arg ); if( ! $arg[ 'feed_url' ] ) return( FALSE ); $prefix = $suffix = ''; if( $arg['div_class'] ){ $prefix .= '
    '; $suffix .= '
    '; if( $arg['title'] ) $prefix .= '

    '. $arg['title'] .'

    '; if( $arg['ul_class'] ){ $prefix .= ''. $suffix; }else if( $arg['ol_class'] ){ $prefix .= '
      '; $suffix = '
    '. $suffix; } }else{ if( $arg['title'] ) $prefix .= '

    '. $arg['title'] .'

    '; if( $arg['ul_class'] ){ $prefix .= ''. $suffix; }else if( $arg['ol_class'] ){ $prefix .= '
      '; $suffix = '
    '. $suffix; } } return( $prefix . $this->get_feed( $arg['feed_url'], $arg['count'], $arg['template'], TRUE) . $suffix ); } // // token functions // tokens are [[token]] in the content of a post. // function tokens_get(){ // establish list of tokens static $tokens = FALSE; if($tokens) return($tokens); $tokens = array(); $tokens = apply_filters('bsuite_tokens', $tokens); return($tokens); } function tokens_fill($thing) { // match tokens $return = $thing[0]; $thing = explode('|', trim($thing[0], '[]'), 2); $tokens = &$this->tokens_get(); if($tokens[$thing[0]]) $return = call_user_func_array($tokens[$thing[0]], is_array( $thing[1] ) ? $thing[1] : array( $thing[1] )); return($return); } function tokens($content) { // find tokens in the page $content = preg_replace_callback( '/\[\[([^\]\]])*\]\]/', array(&$this, 'tokens_fill'), $content); return($content); } function tokens_the_content($content) { $this->is_content = TRUE; $content = $this->tokens($content); $this->is_content = FALSE; return($content); } function tokens_the_content_rss($content) { $this->is_content = TRUE; $this->is_rss = TRUE; $content = $this->tokens($content); $this->is_content = FALSE; $this->is_rss = FALSE; return($content); } function tokens_the_excerpt($content) { $this->is_excerpt = TRUE; $content = $this->tokens($content); $this->is_excerpt = FALSE; return($content); } function tokens_the_excerpt_rss($content) { $this->is_excerpt = TRUE; $this->is_rss = TRUE; $content = $this->tokens($content); $this->is_excerpt = FALSE; $this->is_rss = FALSE; return($content); } function tokens_default($tokens){ // setup some default tokens $tokens['date'] = array(&$this, 'token_get_date'); $tokens['pagemenu'] = array(&$this, 'token_get_pagemenu'); $tokens['feed'] = array(&$this, 'token_get_feed'); $tokens['redirect'] = array(&$this, 'token_get_redirect'); return($tokens); } function token_get_date($stuff = 'F j, Y, g:i a'){ // [[date|options]] return(date($stuff)); } function token_get_pagemenu($stuff = NULL){ // [[pagemenu|depth|extra]] // [[pagemenu|1|sort_column=post_date&sort_order=DESC]] global $id; $stuff = explode('|', $stuff); return(wp_list_pages("child_of=$id&depth=1&echo=0&sort_column=menu_order&title_li=&$stuff[0]")); } function token_get_redirect($stuff){ // [[redirect|$url]] if(!headers_sent()) header("Location: $stuff"); return('redirect: '. $stuff .''); } function token_get_feed($stuff){ // [[feed|feed_url|count]] $stuff = explode('|', $stuff); if(!$stuff[1]) $stuff[1] = 5; if(!$stuff[2]) $stuff[2] = '
  • %%title%%
    %%content%%
  • '; return($this->get_feed($stuff[0], $stuff[1], $stuff[2], TRUE)); } // end token-related functions // // post icons // function icon_sizes_default(){ $this->icon_sizes = array( 's' => array( 'file' => dirname( __FILE__ ) .'/img/post_icon_default/s.jpg', 'url' => $this->path_web .'/img/post_icon_default/s.jpg', 'w' => '100', 'h' => '100', ), 'm' => array( 'file' => dirname( __FILE__ ) .'/img/post_icon_default/m.jpg', 'url' => $this->path_web .'/img/post_icon_default/m.jpg', 'w' => '250', 'h' => '200', ), 'l' => array( 'file' => dirname( __FILE__ ) .'/img/post_icon_default/l.jpg', 'url' => $this->path_web .'/img/post_icon_default/l.jpg', 'w' => '500', 'h' => '375', ), 'b' => array( 'file' => dirname( __FILE__ ) .'/img/post_icon_default/b.jpg', 'url' => $this->path_web .'/img/post_icon_default/b.jpg', 'w' => '1280', 'h' => '975', ), ); } function icon_is_editing( $post_id ) { global $current_user; if ( !$post = get_post( $post_id ) ) return false; $lock = get_post_meta( $post->ID, '_edit_lock', true ); $last = get_post_meta( $post->ID, '_edit_last', true ); $time_window = apply_filters( 'wp_check_post_lock_window', AUTOSAVE_INTERVAL * 2 ); if ( $lock && $lock > time() - $time_window && $last == $current_user->ID ) return( TRUE ); return( FALSE ); } function icon_handle_upload() { $_FILES['import']['name'] = substr( md5( uniqid( microtime())), 0, 4 ) . strrchr( $_FILES['import']['name'] , '.' ); $overrides = array( 'test_form' => false ); $file = wp_handle_upload( $_FILES['import'], $overrides ); return( $file ); } function icon_form( $post_id = 0 ) { require_once( ABSPATH .'/wp-admin/includes/template.php'); if( 0 >= (int) $post_id ) return( FALSE ); ?> '; $img = array_pop( $img ); ?>
    zoom in on the icon.
    ()
    = (int) $_REQUEST['post_ID'] ) die(0); if( !$this->icon_is_editing( (int) $_REQUEST['post_ID'] )) wp_die(__('You cannot add an icon to a post you`re not currently editing.')); $img = get_post_meta( (int) $_REQUEST['post_ID'], 'bsuite_post_icon', TRUE ); $this->unlink_recursive( dirname( $img['s']['file'] ), TRUE ); delete_post_meta( (int) $_REQUEST['post_ID'], 'bsuite_post_icon' ); die( $this->icon_form( (int) $_REQUEST['post_ID'] )); } function icon_ajax_form( ){ if (!current_user_can('upload_files')) wp_die(__('You do not have permission to upload files.')); if( 0 >= (int) $_REQUEST['post_ID'] ) die(0); if( !$this->icon_is_editing( (int) $_REQUEST['post_ID'] )) wp_die(__('You cannot add an icon to a post you`re not currently editing.')); die( $this->icon_form( (int) $_REQUEST['post_ID'] )); } function icon_ajax_upload( ){ // security checks check_admin_referer('bsuite-icon-upload'); if (!current_user_can('upload_files')) wp_die(__('You do not have permission to upload files.')); // make sure we have a post ID if( 0 >= (int) $_REQUEST['post_ID'] ) die('form is incomplete'); if( !$this->icon_is_editing( (int) $_REQUEST['post_ID'] )) wp_die(__('You cannot add an icon to a post you`re not currently editing.')); // get the file that's being uploaded $file = $this->icon_handle_upload(); // make sure that we've got an image if( 'image' === strtolower( substr( $file['type'], 0, strpos( $file['type'], '/' )))){ // get the post id $post_id = (int) $_REQUEST['post_ID']; if( $this->icon_resize( $file['file'], $post_id ) ){ sleep(1); // give the database a moment to process the input // die with the editor form die( $this->icon_form( $post_id ) ); } die(0); }else{ // don't know what to do with it, so delete it and die @unlink( $file['file'] ); die(0); } } function icon_resize( $image_ori , $post_id , $freshen = FALSE ){ // set a new directory for the file $uploads = wp_upload_dir( 'icon-'. substr( str_pad( $post_id, 2, '0', STR_PAD_LEFT ), -2) ); $uploads['path'] .= '/'. $post_id; // create the directory, remove any previous if existing if( is_dir( $uploads['path'] )){ if( preg_match( '/^http/i', $image_ori )){ $headers = wp_remote_head( $image_ori ); if( filectime( $uploads['path'] ) > strtotime( $headers['headers']['last-modified'] ) && !$freshen ) return( get_post_meta( $post_id, 'bsuite_post_icon', TRUE ) ); } $this->unlink_recursive( $uploads['path'] ); mkdir( $uploads['path'], 0775, TRUE ); }else{ mkdir( $uploads['path'], 0775, TRUE ); } // set the destination path of the file $uploads['path'] .= '/o'. strrchr( basename( $image_ori ) , '.' ); // check if it's a remote image, get it if it is if( preg_match( '/^http/i', $image_ori )){ // fetch the remote url and write it to the placeholder file $headers = wp_get_http( $image_ori, $uploads['path'] ); //print_r( $headers ); //Did we get a result? if( !$headers ) { @unlink( $uploads['path'] ); return FALSE; }elseif ( $headers['response'] != '200' ) { @unlink( $uploads['path'] ); return FALSE; }elseif ( isset( $headers['content-length'] ) && filesize( $uploads['path'] ) != $headers['content-length'] ) { @unlink( $uploads['path'] ); return FALSE; } }else{ // move the uploaded file into that directory, delete the old file (redundent, i know) rename( $image_ori, $uploads['path']); @unlink( $image_ori ); } // set base paths and urls $image_ori = $uploads['path']; $url_base = $uploads['url'] .'/'. $post_id .'/'; // okay, let's process that image $img = array(); // make the square version if( $img_tmp = image_resize( $image_ori, 150, 150, TRUE, 's' )){ $img_dims = @getimagesize( $img_tmp ); $img_scale = wp_crop_image( $img_tmp, ( $img_dims[0] / 2 ) - 50 , ( $img_dims[1] / 2 ) - 50, 100, 100, 100, 100, FALSE, $img_tmp ); if( $img_tmp <> $img_scale ) @unlink( $img_tmp ); rename( $img_scale, str_replace( basename( $img_scale ), substr( basename( $img_scale ), 2), $img_scale )); $img_scale = str_replace( basename( $img_scale ), substr( basename( $img_scale ), 2), $img_scale ); $img['s']['file'] = $img_scale; $img['s']['url'] = $url_base . basename( $img_scale ); list( $img['s']['w'],$img['s']['h'] ) = getimagesize( $img_scale ); } // make the medium version if( $img_tmp = image_resize( $image_ori, 278, 278, FALSE, 'm' )){ $img_dims = @getimagesize( $img_tmp ); $img_scale = wp_crop_image( $img_tmp, $img_dims[0] * .05 , $img_dims[1] * .05, $img_dims[0] * .9, $img_dims[1] * .9, $img_dims[0] * .9, $img_dims[1] * .9, FALSE, $img_tmp ); if( $img_tmp <> $img_scale ) @unlink( $img_tmp ); rename( $img_scale, str_replace( basename( $img_scale ), substr( basename( $img_scale ), 2), $img_scale )); $img_scale = str_replace( basename( $img_scale ), substr( basename( $img_scale ), 2), $img_scale ); $img['m']['file'] = $img_scale; $img['m']['url'] = $url_base . basename( $img_scale ); list( $img['m']['w'],$img['m']['h'] ) = getimagesize( $img_scale ); } // make the large version if( $img_scale = image_resize( $image_ori, 500, 500, FALSE, 'l' )){ rename( $img_scale, str_replace( basename( $img_scale ), substr( basename( $img_scale ), 2), $img_scale )); $img_scale = str_replace( basename( $img_scale ), substr( basename( $img_scale ), 2), $img_scale ); $img['l']['file'] = $img_scale; $img['l']['url'] = $url_base . basename( $img_scale ); list( $img['l']['w'],$img['l']['h'] ) = getimagesize( $img_scale ); } // make the big version if( $img_scale = image_resize( $image_ori, 1280, 1280, FALSE, 'b' )){ rename( $img_scale, str_replace( basename( $img_scale ), substr( basename( $img_scale ), 2), $img_scale )); $img_scale = str_replace( basename( $img_scale ), substr( basename( $img_scale ), 2), $img_scale ); $img['b']['file'] = $img_scale; $img['b']['url'] = $url_base . basename( $img_scale ); list( $img['b']['w'],$img['b']['h'] ) = getimagesize( $img_scale ); } // finally, delete the original @unlink( $image_ori ); // the the image to the post_meta add_post_meta( $post_id, 'bsuite_post_icon', $img, TRUE ) or update_post_meta( $post_id, 'bsuite_post_icon', $img); return $img; } function icon_editor_iframe( ){ global $post_ID; echo ''; } function icon_get_default( $post_id, $size = 's' ){ if( is_array( $this->icon_sizes[ $size ] )) return( $this->icon_sizes[ $size ] ); else return( $this->icon_sizes['s'] ); } function icon_get_tofit( $img, $size = 's' ){ if( is_array( $this->icon_sizes[ $size ] )){ if( is_array( $img )){ // what area are we looking for? $area_expected = $this->icon_sizes[ $size ]['w'] * $this->icon_sizes[ $size ]['h']; // what areas do we have? $larger = $smaller = array(); foreach( $img as $key => $val ){ $area = $val['w'] * $val['h']; if( $area >= ( $area_expected * .8)) $larger[ $key ] = $area; if( $area <= ( $area_expected * 1.2)) $smaller[ $key ] = $area; } // which of those is closest? (biased toward larger size) if( count( $larger ) ){ asort( $larger, SORT_NUMERIC ); $nearest = key( $larger ); }else if( count( $smaller ) ){ asort( $smaller, SORT_NUMERIC ); $nearest = key( array_slice( $smaller, -1, 1, TRUE )); } // okay, we got one, let's use it $return = $img[ $nearest ]; $return['w'] = $this->icon_sizes[ $size ]['w']; $return['h'] = $this->icon_sizes[ $size ]['h']; return( $return ); } }else{ // we don't know what size you're looking for // we'll try the 's' size instead return( $this->icon_get_tofit( $img, 's' )); } } function icon_get_a( $post_id, $size = 's' ){ $img = apply_filters( 'bsuite_post_icon', get_post_meta( $post_id, 'bsuite_post_icon', TRUE ), $post_id ); if( is_array( $img )) if( is_array( $img[ $size ] )) return( $img[ $size ] ); else return( $this->icon_get_tofit( $img, $size )); else return( $this->icon_get_default( $post_id, $size )); } function icon_get_h( $post_id, $size = 's', $nostyle = FALSE, $ow = 0, $oh = 0 ){ if( $img = $this->icon_get_a( $post_id, $size )){ if( $nostyle || strpos( current_filter(), 'rss' )) return( ''. attribute_escape( get_the_title( $post_id )) .'' ); return( ''. attribute_escape( get_the_title( $post_id )) .'' ); } return( FALSE ); } // end post icon related functions function bsuite_mycss_printstyles(){ @header('Content-Type: text/css; charset=' . get_option('blog_charset')); echo get_option( 'bsuite_mycss' ); die(); } function bsuite_mycss_hidesstylesheet( $input ){ return( $this->path_web . '/css/empty.css' ); } function mycss_sanitize( $input ){ $input = wp_filter_nohtml_kses( $input ); $input = preg_replace('/\/\*.*?\*\//sm', '', $input); // strip comments $safecss = ''; foreach( explode( "\n", $input ) as $line ) $safecss .= $this->mycss_cleanline( $line ); return( $safecss ); } function mycss_cleanline( $input ){ $evil = 0; $filtered = wp_kses_decode_entities( $input ); $filtered = preg_replace('/expression[^\(]?\(.*?\)/i', '', $filtered, -1, $flag ); // strip expressions if( $flag ) $evil++; $filtered = preg_replace('/@import/i', '', $filtered, -1, $flag ); // strip @import if( $flag ) $evil++; $filtered = preg_replace('/about:/i', '', $filtered, -1, $flag ); // strip about: uris if( $flag ) $evil++; $filtered = preg_replace_callback('/([\w]*?):\/\//si', array( $this, 'mycss_cleanuri' ), $filtered, -1, $flag ); // strip non http uris if( $flag ) $evil++; return( $evil ? $filtered : $input ); } function mycss_cleanuri( $input ){ if( !preg_match( '/^http:\/\//', $input[0] )) return ''; return( $input[0] ); } // // sharelinks // function sharelinks(){ global $wp_query; // exit if 404 if($wp_query->is_404) return(FALSE); // identify the based post ID, if any, and establish some basics $post_id = FALSE; if(!empty($wp_query->is_singular) && !empty($wp_query->query_vars['p'])) $post_id = $wp_query->query_vars['p']; else if(!empty($wp_query->is_singular) && !empty($wp_query->queried_object_id)) $post_id = $wp_query->queried_object_id; else if( !empty( $this->bsuggestive_to )) $post_id = $this->bsuggestive_to; if($post_id){ $the_permalink = urlencode(get_permalink($post_id)); $the_title = urlencode(get_the_title($post_id)); $the_excerpt = apply_filters('the_excerpt', get_the_excerpt()); }else{ $the_permalink = strtolower(substr($_SERVER['SERVER_PROTOCOL'], 0, strpos($_SERVER['SERVER_PROTOCOL'], '/'))) . '://' . $_SERVER['HTTP_HOST'] . add_query_arg('bsuite_share'); unset($wp_query->query['bsuite_share']); unset($wp_query->query['attachment']); if(count($wp_query->query)) $the_title = get_bloginfo('name') .' ('. wp_specialchars( implode(array_unique(explode('|', strtolower(implode(array_values($wp_query->query), '|')))), ', ')) .')'; else $the_title = get_bloginfo('name'); $the_excerpt = ''; } $content = ''; // powered by $content .= '

    Powered by bSuite.

    '; return( $content ); //return(array('the_id' => $post_id, 'the_title' => urldecode($the_title), 'the_permalink' => urldecode($the_permalink), 'the_content' => $content, )); } function sharelinks_the_content( $content ) { if( is_single() && $sharelinks = $this->sharelinks() ) return( $content . $sharelinks); return( $content ); } // end sharelinks related functions // // link to me // function link2me_links( $post_id ){ if( !$post_id ) return( FALSE ); //''. strip_tags( get_the_title( $post_id )) .''; //not using this now //echo '

    Hi!

    '; return( apply_filters('bsuite_link2me', array( array('code' => get_permalink($post_id), 'name' => __( 'Permalink', 'bsuite' ))), $post_id)); } function link2me( $post_id ){ if( !$post_id ){ global $id; $post_id = $id; } if( !$links = $this->link2me_links( $post_id )) return( FALSE ); if( count( $links ) ){ $return = ''; return( $return ); } return( FALSE ); } // // Stats Related // function bstat_js() { if( !$this->didstats ){ ?> get_var("SELECT name FROM $this->hits_terms WHERE ". $wpdb->prepare( "term_id = %s", (int) $id )); wp_cache_add( $id, $name, 'bstat_terms', 0 ); } return( $name ); } function bstat_is_term( $term ) { global $wpdb; $cache_key = md5( substr( $term, 0, 255 ) ); if ( !$term_id = wp_cache_get( $cache_key, 'bstat_termids' )) { $term_id = (int) $wpdb->get_var("SELECT term_id FROM $this->hits_terms WHERE ". $wpdb->prepare( "name = %s", substr( $term, 0, 255 ))); wp_cache_add( $cache_key, $term_id, 'bstat_termids', 0 ); } return( $term_id ); } function bstat_insert_term( $term ) { global $wpdb; if ( !$term_id = $this->bstat_is_term( $term )) { if ( false === $wpdb->insert( $this->hits_terms, array( 'name' => $term ))){ new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error); return( 1 ); } $term_id = (int) $wpdb->insert_id; } return( $term_id ); } function bstat_is_session( $session_cookie ) { global $wpdb; if ( !$sess_id = wp_cache_get( $session_cookie, 'bstat_sessioncookies' )) { $sess_id = (int) $wpdb->get_var("SELECT sess_id FROM $this->hits_sessions WHERE ". $wpdb->prepare( "sess_cookie = %s", $session_cookie )); wp_cache_add( $session_cookie, $sess_id, 'bstat_sessioncookies', 10800 ); } return($sess_id); } function bstat_insert_session( $session ) { global $wpdb; $s = array(); if ( !$session_id = $this->bstat_is_session( $session->in_session )) { $this->session_new = TRUE; $s['sess_cookie'] = $session->in_session; $s['sess_date'] = $session->in_time; $se = unserialize( $session->in_extra ); $s['sess_ip'] = $se['ip']; $s['sess_br'] = $se['br']; $s['sess_bb'] = $se['bb']; $s['sess_bl'] = $se['bl']; $s['sess_ba'] = urldecode( $se['ba'] ); // could use INET_ATON and INET_NTOA to reduce storage requirements for the IP address, // but it's not human readable when browsing the table if ( false === $wpdb->insert( $this->hits_sessions, $s )){ new WP_Error('db_insert_error', __('Could not insert session into the database'), $wpdb->last_error); return( FALSE ); } $session_id = (int) $wpdb->insert_id; wp_cache_add($session->in_session, $session_id, 'bstat_sessioncookies', 10800 ); } return( $session_id ); } function bstat_migrator( $debug = FALSE ) { if( $debug ) echo "

    Start

    "; global $wpdb, $blog_id; if( !$this->get_lock( 'migrator' )) return( TRUE ); // also use the options table if ( get_option( 'bsuite_doing_migration') > time() ) return( TRUE ); update_option( 'bsuite_doing_migration', time() + 250 ); $status = get_option ( 'bsuite_doing_migration_status' ); if( $debug ) echo "

    Get Locks

    "; $getcount = 1 < get_option( 'bsuite_migration_count' ) ? absint( get_option( 'bsuite_migration_count' )) : 100; $since = date('Y-m-d H:i:s', strtotime('-1 minutes')); $res = $wpdb->get_results( "SELECT * FROM $this->hits_incoming WHERE in_time < '$since' ORDER BY in_time ASC LIMIT $getcount" ); $status['count_incoming'] = count( $res ); update_option( 'bsuite_doing_migration_status', $status ); if( $debug ) echo "

    Got {$status['count_incoming']} records from incoming table

    "; foreach( $res as $hit ){ $object_id = $object_type = $session_id = 0; if( $debug ) echo "

    Bamn! 1

    "; if( !strlen( $hit->in_to )) $hit->in_to = get_option( 'siteurl' ) .'/'; if( $hit->in_session ) $session_id = $this->bstat_insert_session( $hit ); if( $debug ) echo "

    Bamn! 2

    "; $hit->in_blog = absint( $hit->in_blog ); $switch_blog = FALSE; if( function_exists( 'switch_to_blog' ) && absint( $blog_id ) <> $hit->in_blog ) { $switch_blog = TRUE; switch_to_blog( $hit->in_blog ); } if( $debug ) echo "

    Bamn! 3 $hit->in_to

    "; $object_id = url_to_postid( $hit->in_to ); // determine the target if( ( 1 > $object_id ) || (('posts' <> get_option( 'show_on_front' )) && $object_id == get_option( 'page_on_front' )) ){ $object_id = $this->bstat_insert_term( $hit->in_to ); $object_type = 1; } $targets[] = "($hit->in_blog, $object_id, $object_type, 1, '$hit->in_time')"; if( $debug ) echo "

    Bamn! 4

    "; // look for search words if( ( $referers = implode( $this->get_search_terms( $hit->in_from ), ' ') ) && ( 0 < strlen( $referers ))) { $term_id = $this->bstat_insert_term( $referers ); $searchwords[] = "($hit->in_blog, $object_id, $object_type, $term_id, 1)"; } if( $session_id ){ if( $referers ) $shistory[] = "($session_id, $hit->in_blog, $term_id, 2)"; if( $this->session_new ){ $in_from = $this->bstat_insert_term( $hit->in_from ); if( $referers ) $shistory[] = "($session_id, $hit->in_blog, $in_from, 3)"; } $shistory[] = "($session_id, $hit->in_blog, $object_id, $object_type)"; } if( $debug ) echo "

    Bamn! 5

    "; if( $switch_blog && function_exists( 'restore_current_blog' ) ) restore_current_blog( $hit->in_blog ); } $status['count_targets'] = count( $targets ); $status['count_searchwords'] = count( $searchwords ); $status['count_shistory'] = count( $shistory ); update_option( 'bsuite_doing_migration_status', $status ); if( $debug ) echo "

    Found {$status['count_targets']} URL targets, {$status['count_searchwords']} search phrases, and {$status['count_shistory']} sessions

    "; if( count( $targets ) && !$status['did_targets'] ){ if ( false === $wpdb->query( "INSERT INTO $this->hits_targets (object_blog, object_id, object_type, hit_count, hit_date) VALUES ". implode( $targets, ',' ) ." ON DUPLICATE KEY UPDATE hit_count = hit_count + 1;" )) return new WP_Error('db_insert_error', __('Could not insert bsuite_hits_target into the database'), $wpdb->last_error); $status['did_targets'] = 1 ; update_option( 'bsuite_doing_migration_status', $status ); } if( count( $searchwords ) && !$status['did_searchwords'] ){ if ( false === $wpdb->query( "INSERT INTO $this->hits_searchphrases (object_blog, object_id, object_type, term_id, hit_count) VALUES ". implode( $searchwords, ',' ) ." ON DUPLICATE KEY UPDATE hit_count = hit_count + 1;" )) return new WP_Error('db_insert_error', __('Could not insert bsuite_hits_searchword into the database'), $wpdb->last_error); $status['did_searchwords'] = 1; update_option( 'bsuite_doing_migration_status', $status ); } if( count( $shistory ) && !$status['did_shistory'] ){ if ( false === $wpdb->query( "INSERT INTO $this->hits_shistory (sess_id, object_blog, object_id, object_type) VALUES ". implode( $shistory, ',' ) .';' )) return new WP_Error('db_insert_error', __('Could not insert bsuite_hits_session_history into the database'), $wpdb->last_error); $status['did_shistory'] = count( $shistory ); update_option( 'bsuite_doing_migration_status', $status ); } if( count( $res )){ if ( false === $wpdb->query( "DELETE FROM $this->hits_incoming WHERE in_time < '$since' ORDER BY in_time ASC LIMIT ". count( $res ) .';')) return new WP_Error('db_insert_error', __('Could not clean up the incoming stats table'), $wpdb->last_error); if( $getcount > count( $res )) $wpdb->query( "OPTIMIZE TABLE $this->hits_incoming;"); } if( $debug ) echo "

    Deleted records from incoming table

    "; if ( get_option( 'bsuite_doing_migration_popr') < time() && $this->get_lock( 'popr' )){ if ( get_option( 'bsuite_doing_migration_popd') < time() && $this->get_lock( 'popd' ) ){ $wpdb->query( "TRUNCATE $this->hits_pop" ); $wpdb->query( "INSERT INTO $this->hits_pop (blog_id, post_id, date_start, hits_total) SELECT object_blog AS blog_id, object_id AS post_id, MIN(hit_date) AS date_start, SUM(hit_count) AS hits_total FROM $this->hits_targets WHERE object_type = 0 AND hit_date >= DATE_SUB( NOW(), INTERVAL 45 DAY ) GROUP BY object_id" ); update_option( 'bsuite_doing_migration_popd', time() + 64800 ); } $wpdb->query( "UPDATE $this->hits_pop p LEFT JOIN ( SELECT object_blog, object_id, COUNT(*) AS hit_count FROM ( SELECT sess_id, sess_date FROM ( SELECT sess_id, sess_date FROM $this->hits_sessions ORDER BY sess_id DESC LIMIT 12500 ) a WHERE sess_date >= DATE_SUB( NOW(), INTERVAL 1 DAY ) ) s LEFT JOIN $this->hits_shistory h ON h.sess_id = s.sess_id WHERE h.object_type = 0 GROUP BY object_blog, object_id ) h ON ( h.object_id = p.post_id AND h.object_blog = p.blog_id ) SET hits_recent = h.hit_count" ); update_option( 'bsuite_doing_migration_popr', time() + 1500 ); } /* $posts = $wpdb->get_results("SELECT object_id, AVG(hit_count) AS hit_avg FROM $this->hits_targets WHERE hit_date >= DATE_SUB(CURDATE(),INTERVAL 30 DAY) AND object_type = 0 GROUP BY object_id ORDER BY object_id ASC", ARRAY_A); $avg = array(); foreach($posts as $post) $avg[$post['object_id']] = $post['hit_avg']; $posts = $wpdb->get_results("SELECT object_id, hit_count * (86400/TIME_TO_SEC(TIME(NOW()))) AS hit_now FROM $this->hits_targets WHERE hit_date = CURDATE() AND object_type = 0 ORDER BY object_id ASC", ARRAY_A); $now = array(); foreach($posts as $post) $now[$post['object_id']] = $post['hit_now']; $diff = array(); foreach($posts as $post) $diff[$post['object_id']] = intval(($now[$post['object_id']] - $avg[$post['object_id']]) * 1000 ); $win = count(array_filter($diff, create_function('$a', 'if($a > 0) return(TRUE);'))); $lose = count($diff) - $win; $sort = array_flip($diff); ksort($sort); if(!empty($sort)){ foreach(array_slice(array_reverse($sort), 0, $detail_lines) as $object_id){ echo '
  • '. get_the_title($object_id) .'
    Up: '. number_format($diff[$object_id] / 1000, 0) .' Avg: '. number_format($avg[$object_id], 0) .' Today: '. number_format($now[$object_id], 0) ."
  • \n"; } } */ //print_r($wpdb->queries); if( $debug ) echo "

    Done

    "; update_option( 'bsuite_doing_migration', 0 ); update_option( 'bsuite_doing_migration_status', array() ); return(TRUE); } function get_search_engine( $ref ) { // a lot of inspiration and code for this function was taken from // Search Hilite by Ryan Boren and Matt Mullenweg global $wp_query; if( empty( $ref )) return false; $referer = urldecode( $ref ); if (preg_match('|^http://(www)?\.?google.*|i', $referer)) return('google'); if (preg_match('|^http://search\.yahoo.*|i', $referer)) return('yahoo'); if (preg_match('|^http://search\.live.*|i', $referer)) return('windowslive'); if (preg_match('|^http://search\.msn.*|i', $referer)) return('msn'); if (preg_match('|^http://search\.lycos.*|i', $referer)) return('lycos'); $home = parse_url( get_settings( 'siteurl' )); $ref = parse_url( $referer ); if ( strpos( ' '. $ref['host'] , $home['host'] )) return('internal'); return(FALSE); } function get_search_terms( $ref ) { // a lot of inspiration and code for this function was taken from // Search Hilite by Ryan Boren and Matt Mullenweg // if( !$engine = $this->get_search_engine( $ref )) // return(FALSE); $engine = $this->get_search_engine( $ref ); $referer = parse_url( $ref ); parse_str( $referer['query'], $query_vars ); $query_array = array(); switch ($engine) { case 'google': if( $query_vars['q'] ) $query_array = explode(' ', urldecode( $query_vars['q'] )); break; case 'yahoo': if( $query_vars['p'] ) $query_array = explode(' ', urldecode( $query_vars['p'] )); break; case 'windowslive': if( $query_vars['q'] ) $query_array = explode(' ', urldecode( $query_vars['q'] )); break; case 'msn': if( $query_vars['q'] ) $query_array = explode(' ', urldecode( $query_vars['q'] )); break; case 'lycos': if( $query_vars['query'] ) $query_array = explode(' ', urldecode( $query_vars['query'] )); break; case 'internal': if( $query_vars['s'] ) $query_array = explode(' ', urldecode( $query_vars['s'] )); // also need to handle the case where a search matches the /search/ pattern break; } $query_array = array_filter( array_map( array(&$this, 'trimquotes') , $query_array )); return $query_array; } function post_hits( $args = '' ) { global $wpdb, $blog_id; $defaults = array( 'return' => 'formatted', 'days' => 0, 'template' => '
  • %%title%% (%%hits%%)
  • ' ); $args = wp_parse_args( $args, $defaults ); $post_id = (int) $args['post_id'] > 1 ? 'AND object_id = '. (int) $args['post_id'] : ''; $date = ''; if($args['days'] > 1) $date = "AND hit_date > '". date("Y-m-d", mktime(0, 0, 0, date("m") , date("d") - $args['days'], date("Y"))) ."'"; // here's the query, but let's try to get the data from cache first $request = "SELECT FORMAT(SUM(hit_count), 0) AS hits, FORMAT(AVG(hit_count), 0) AS average FROM $this->hits_targets WHERE 1=1 AND object_blog = ". absint( $blog_id ) ." $post_id AND object_type = 0 $date "; if ( !$result = wp_cache_get( (int) $args['post_id'] .'_'. (int) $args['days'], 'bstat_post_hits' ) ) { $result = $wpdb->get_results($request, ARRAY_A); wp_cache_add( (int) $args['post_id'] .'_'. (int) $args['days'], $result, 'bstat_post_hits', 1800 ); } if(empty($result)) return(NULL); if($args['return'] == 'array') return($result); if($args['return'] == 'formatted'){ $list = str_replace(array('%%avg%%','%%hits%%'), array($result[0]['average'], $result[0]['hits']), $args['template']); return($list); } } function pop_posts( $args = '' ) { global $wpdb, $bsuite, $blog_id; if( !$this->get_lock( 'pop_posts' )) return( FALSE ); $args = wp_parse_args( $args, array( 'count' => 15, 'return' => 'formatted', 'show_icon' => 0, 'show_title' => 1, 'show_counts' => 1, 'icon_size' => 's', )); $date = 'AND hit_date = DATE(NOW())'; if($args['days'] > 1) $date = "AND hit_date > '". date("Y-m-d", mktime(0, 0, 0, date("m") , date("d") - $args['days'], date("Y"))) ."'"; $limit = 'LIMIT '. ( absint( $args['count'] ) * 2 ); $request = "SELECT object_id, SUM(hit_count) AS hit_count FROM $this->hits_targets WHERE 1=1 AND object_blog = ". absint( $blog_id ) ." AND object_type = 0 $date GROUP BY object_id ORDER BY hit_count DESC $limit"; $result = $wpdb->get_results($request, ARRAY_A); if(empty($result)) return(NULL); if($args['return'] == 'array') return($result); if($args['return'] == 'formatted'){ $list = ''; foreach($result as $post){ $list .='
  • '. ( $args['show_icon'] ? ''. $this->icon_get_h( $post['object_id'], $args['icon_size'] ) .'' : '' ) . ( $args['show_title'] ? ''. get_the_title( $post['object_id'] ) . '' : '' ) . ( $args['show_counts'] ? ' ('. $post['hit_count'] .')' : '' ) .'
  • '; } return($list); } } function pop_refs( $args = '' ) { global $wpdb, $bsuite, $blog_id; if( !$this->get_lock( 'pop_refs' )) return( FALSE ); $defaults = array( 'count' => 15, 'return' => 'formatted', 'template' => '
  • %%title%% (%%hits%%)
  • ' ); $args = wp_parse_args( $args, $defaults ); $limit = 'LIMIT '. (int) $args['count']; $request = "SELECT COUNT(*) AS hit_count, name FROM ( SELECT object_blog, object_id FROM $this->hits_shistory WHERE object_blog = ". absint( $blog_id ) ." AND object_type = 2 ORDER BY sess_id DESC LIMIT 1000 ) a LEFT JOIN $this->hits_terms t ON a.object_id = t.term_id WHERE t.status = '' GROUP BY object_blog, object_id ORDER BY hit_count DESC $limit"; $result = $wpdb->get_results($request, ARRAY_A); if(empty($result)) return(NULL); if($args['return'] == 'array') return($result); if($args['return'] == 'formatted'){ $list = ''; foreach($result as $row){ $list .= str_replace(array('%%title%%','%%hits%%'), array($row['name'], $row['hit_count']), $args['template']); } return($list); } } // end stats functions // // Searchsmart // function searchsmart_direct(){ global $wp_query, $wp_rewrite; // redirect when there's a redirection order for the post if( $wp_query->is_singular && get_post_meta( $wp_query->post->ID, 'redirect', TRUE )) wp_redirect( get_post_meta( $wp_query->post->ID, 'redirect', TRUE ), '301'); // redirects ?s={search_term} to /search/{search_term} if permalinks are working if( isset( $_GET['s'] ) && !empty( $wp_rewrite->permalink_structure ) ) wp_redirect(get_option('siteurl') .'/'. $wp_rewrite->search_base .'/'. urlencode( $_GET['s'] ), '301'); // serves the quickview links if( $wp_query->is_singular && isset( $_GET['quickview'] ) ) $this->quickview(); // redirects the search to the single page if the search returns only one item if( !$wp_query->is_singular && 1 === $wp_query->post_count && !$wp_query->is_feed ) wp_redirect( get_permalink( $wp_query->post->ID ) , '302'); return(TRUE); } function searchsmart_post_link_direct( $permalink, $post ) { $final_link = $permalink; if( $redirect = get_post_meta( $post->ID, 'redirect', TRUE )) $final_link = $redirect; return apply_filters( 'bsuite_searchsmart_post_link_direct', $final_link, $permalink, $post ); } // end Searchsmart function quickview(){ global $wp_query; // make wp think this is a posts page, not a singular page $wp_query->is_single = FALSE; $wp_query->is_page = FALSE; $wp_query->is_singular = FALSE; $wp_query->is_posts_page = TRUE; $this->is_quickview = TRUE; //loop if (have_posts()){ while (have_posts()){ the_post(); ob_start(); ?>

    ID, $authordata->user_nicename).'" title="' . sprintf(__('View all posts by %s', 'sandbox'), $authordata->display_name) . '">'.get_the_author().'') ?> · · ', " ·\n"); ?>
    term_relationships t_ra LEFT JOIN $wpdb->term_taxonomy t_ta ON t_ta.term_taxonomy_id = t_ra.term_taxonomy_id WHERE t_ra.object_id IN (". implode( ',', $id ) .") AND t_ta.taxonomy IN ('". implode( $taxonomies, "','") ."') ) ttid LEFT JOIN $wpdb->term_relationships t_r ON t_r.term_taxonomy_id = ttid.term_taxonomy_id LEFT JOIN $wpdb->posts p ON t_r.object_id = p.ID WHERE p.ID NOT IN( $ignore_ids ) AND p.post_status = 'publish' GROUP BY p.ID ORDER BY hits DESC, p.post_date_gmt DESC LIMIT 150", $id) ); } return FALSE; } function bsuggestive_getposts( $id ) { global $wpdb; if ( !$related_posts = wp_cache_get( implode( ',', (array) $id ), 'bsuite_related_posts' ) ) { if( $the_query = $this->bsuggestive_query( $id ) ){ $related_posts = $wpdb->get_col($the_query); wp_cache_set( implode( ',', (array) $id ), $related_posts, 'bsuite_related_posts', time() + 900000 ); // cache for 25 days return($related_posts); // if we have to go to the DB to get the posts, then this will get returned } return( FALSE ); // if there's nothing in the cache and we've got no query } return $related_posts; // if the cache is still warm, then we return this } function bsuggestive_delete_cache( $id ) { $id = (int) $id; if ( !$id ) return FALSE; wp_cache_delete( $id, 'bsuite_related_posts' ); } function bsuggestive_the_related($before = '
  • ', $after = '
  • ') { global $post; $report = FALSE; $id = (int) $post->ID; if ( !$id ) return( FALSE ); // no ID, no service if( ( $posts = $this->bsuggestive_getposts( $id )) && is_array( $posts )){ $posts = array_slice( $posts, 0, 5 ); $report = ''; foreach($posts as $post_id){ // $post = &get_post( $post_id ); $url = get_permalink($post_id); $linktext = get_the_title($post_id); $report .= $before . "$linktext". $after; } } return($report); } function bsuggestive_the_content( $content ) { if( $related = $this->bsuggestive_the_related() ) return( $content .'' ); return( $content ); } function bsuggestive_bypageviews_getposts( $id ) { global $wpdb, $blog_id; $id = absint( $id ); $ignore_ids = ( array_filter( apply_filters( 'bsuite_suggestive_ignoreposts', array( $id )))); if( is_array( $ignore_ids )) $ignore_ids = implode( ',', array_filter( array_map( 'absint' , $ignore_ids ))); else $ignore_ids = $id; if ( !$related_posts = wp_cache_get( $id, 'bsuite_relatedbypageviews_posts' ) ) { if( $related_posts = $wpdb->get_col( "SELECT h.object_id, COUNT(h.object_id) AS hits FROM ( SELECT sess_id FROM $this->hits_shistory WHERE object_id = $id AND object_blog = ". absint( $blog_id ) ." AND object_type = 0 GROUP BY sess_id DESC LIMIT 250 ) s LEFT JOIN $this->hits_shistory h ON h.sess_id = s.sess_id LEFT JOIN ( SELECT blog_id, post_id FROM $this->hits_pop ORDER BY hits_recent DESC LIMIT 7 ) pop ON ( pop.post_id = h.object_id AND pop.blog_id = h.object_blog ) WHERE h.object_id NOT IN( $ignore_ids ) AND h.object_blog = ". absint( $blog_id ) ." AND h.object_type = 0 AND pop.post_id IS NULL GROUP BY h.object_id ORDER BY hits DESC LIMIT 0,150" )){ wp_cache_set( $id, $related_posts, 'bsuite_relatedbypageviews_posts', time() + 90000 ); // cache it for 25 hours return( $related_posts ); // if we have to go to the DB to get the posts, then this will get returned } return( FALSE ); // if there's nothing in the cache and we've got no query } return($related_posts); // if the cache is still warm, then we return this } function bsuggestive_bypageviews_the_related($before = '
  • ', $after = '
  • ') { global $post; $report = FALSE; $id = (int) $post->ID; if ( !$id ) return( FALSE ); // no ID, no service if( ( $posts = $this->bsuggestive_bypageviews_getposts( $id )) && is_array( $posts )){ $posts = array_slice( $posts, 0, 5 ); $report = ''; foreach($posts as $post_id){ // $post = &get_post( $post_id ); $url = get_permalink($post_id); $linktext = get_the_title($post_id); $report .= $before . "$linktext". $after; } } return($report); } function bsuggestive_bypageviews_the_content( $content ) { if( $related = $this->bsuggestive_bypageviews_the_related() ) return( $content . '' ); return( $content ); } // end bSuggestive /* // // user-contributed comments // function uctags_preprocess_comment( $comment ) { $comment['bsuite_uctags'] = !empty( $_REQUEST['bsuite_uctags'] ); if($comment['bsuite_uctags']){ print_r( array_map( 'trim', explode( ',', wp_filter_nohtml_kses( $_REQUEST['bsuite_uctags'] )))); //die; } print_r( $_REQUEST ); die; return( $comment ); } // end user-contributed comment functions */ function pagetree(){ // identify the family tree of a page, return an array global $wp_query; $tree = NULL; if ($wp_query->is_page){ $object = $wp_query->get_queried_object(); // cycle through the tree and build an array $parent_id = $object->post_parent; $tree[] = $object->ID; while ($parent_id){ $page = get_page($parent_id); $tree[] = $page->ID; $parent_id = $page->post_parent; } // the tree is in reverse order. $tree = array_reverse($tree); } return $tree; } // // cron utility functions // function cron_reccurences( $schedules ) { $schedules['bsuite_interval'] = array('interval' => ( get_option( 'bsuite_migration_interval' ) ? get_option( 'bsuite_migration_interval' ) : 90 ), 'display' => __( 'bSuite interval. Set in bSuite options page.' )); return( $schedules ); } function cron_register() { // take a look at Glenn Slaven's tutorial on WP's psudo-cron: // http://blog.slaven.net.au/archives/2007/02/01/timing-is-everything wp_clear_scheduled_hook('bsuite_interval'); wp_schedule_event( time() + 120, 'bsuite_interval', 'bsuite_interval' ); } // end cron functions function get_lock( $lock ){ global $wpdb; if( !$lock = preg_replace( '/[^a-z|0-9|_]/i', '', str_replace( ' ', '_', $lock ))) return( FALSE ); // use a named mysql lock to prevent simultaneous execution // locks automatically drop when the connection is dropped // http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_get-lock if( 0 == $wpdb->get_var( 'SELECT GET_LOCK("'. $wpdb->prefix . 'bsuitelock_'. $lock .'", ".001")' )) return( FALSE ); return( TRUE ); } function release_lock( $lock ){ global $wpdb; if( !$lock = preg_replace( '/[^a-z|0-9|_]/i', '', str_replace( ' ', '_', $lock ))) return( FALSE ); if( 0 == $wpdb->get_var( 'SELECT RELEASE_LOCK("'. $wpdb->prefix . 'bsuitelock_'. $lock .'", ".001")' )) return( FALSE ); return( TRUE ); } // // loadaverage related functions // function get_loadavg(){ static $result = FALSE; if($result) return($result); if(function_exists('sys_getloadavg')){ $load_avg = sys_getloadavg(); }else{ $load_avg = &$this->sys_getloadavg(); } return( round( $load_avg[0], 2 )); } function sys_getloadavg(){ // the following code taken from tom pittlik's comment at // http://php.net/manual/en/function.sys-getloadavg.php $str = substr( strrchr( shell_exec( 'uptime' ),':' ),1 ); $avs = array_map( 'trim', explode( ',', $str )); return( $avs ); } // end load average related functions // A short but powerfull recursive function // that works also if the dirs contain hidden files // // taken from http://us.php.net/manual/en/function.unlink.php // // contributions from: // ayor at ayor dot biz (20-Dec-2007 09:02) // ggarciaa at gmail dot com (04-July-2007 01:57) // stefano at takys dot it (28-Dec-2005 11:57) // // $dir = the target directory // $DeleteMe = if true delete also $dir, if false leave it alone function unlink_recursive($dir, $DeleteMe = FALSE) { if(!$dh = @opendir($dir)) return; while (false !== ($obj = readdir($dh))) { if($obj=='.' || $obj=='..') continue; if (!@unlink($dir.'/'.$obj)) $this->unlink_recursive($dir.'/'.$obj, true); } closedir($dh); if ($DeleteMe){ @rmdir($dir); } } // timers function timer_start( $name = 1 ) { $mtime = microtime(); $mtime = explode(' ', $mtime); $this->time_start[ $name ] = $mtime[1] + $mtime[0]; return true; } function timer_stop( $name = 1 ) { $mtime = microtime(); $mtime = explode(' ', $mtime); $time_end = $mtime[1] + $mtime[0]; $time_total = $time_end - $this->time_start[ $name ]; return $time_total; } // end timers function trimquotes( $in ) { return( trim( trim( $in ), "'\"" )); } function feedlink(){ return(strtolower(substr($_SERVER['SERVER_PROTOCOL'], 0, strpos($_SERVER['SERVER_PROTOCOL'], '/'))) . '://' . $_SERVER['HTTP_HOST'] . add_query_arg('feed', 'rss', add_query_arg('bsuite_share'))); } // set a cookie function cookie($name, $value = NULL) { if($value === NULL){ if($_GET[$name]) return $_GET[$name]; elseif($_POST[$name]) return $_POST[$name]; elseif($_SESSION[$name]) return $_SESSION[$name]; elseif($_COOKIE[$name]) return $_COOKIE[$name]; else return false; }else{ setcookie($name, $value, time()+60*60*24*30, '/', '.scriblio.net'); return($value); } } // end // get and display rss feeds function get_feed($feed, $count = 15, $template = '
  • %%title%%
    %%content%%
  • ', $return = FALSE){ if(!function_exists('fetch_rss')) require_once (ABSPATH . WPINC . '/rss.php'); $rss = fetch_rss($feed); //print_r($rss); $i = $list = NULL; //print_r($rss); foreach($rss->items as $item){ $i++; if($i > $count) break; $link = $item['link']; $title = $item['title']; if($item['content']['encoded']){ $content = $item['content']['encoded']; }else{ $content = $item['description']; } $list .= str_replace( array( '%%title%%','%%content%%','%%link%%' ), array( $title, $content, $link ), $template ); //echo $template; } if($return) return($list); echo $list; } // end function get_rss // machine tags function machtag_save_post($post_id, $post) { // Passed machine tags overwrite existing if not empty if ( isset( $_REQUEST['bsuite_machine_tags'] )) foreach( $this->machtag_parse_tags( $_REQUEST['bsuite_machine_tags'] ) as $taxonomy => $tags ){ if( 'post_tag' == $taxonomy ){ wp_set_post_tags($post_id, $tags, TRUE); continue; } if(!is_taxonomy( $taxonomy )) register_taxonomy($taxonomy, 'post'); wp_set_object_terms($post_id, $tags, $taxonomy); } } function machtag_parse_tags( $tags_input ) { $tags = $tags_parsed = array(); if( !is_array( $tags_input )){ $tag_lines = explode( "\n", $tags_input ); foreach($tag_lines as $tag_line) $tags_parsed[] = $this->machtag_parse_tag( $tag_line ); }else{ $tags_parsed = $tags_input; } foreach( $tags_parsed as $tag_parsed ) if( !empty( $tag_parsed['taxonomy'] ) && !empty( $tag_parsed['term'] )) $tags[$tag_parsed['taxonomy']][] = $tag_parsed['term']; return( $tags ); } function machtag_parse_tag( $tag ) { $namespace = $taxonomy = $term = FALSE; $taxonomy = 'post_tag'; $temp_a = explode(':', $tag, 2); if($temp_a[1]){ $temp_b = explode('=', $temp_a[1], 2); if($temp_b[1]){ // has namespace, fieldname, & value $namespace = trim( $temp_a[0] ); $taxonomy = trim( $temp_b[0] ); $term = trim( $temp_b[1] ); }else{ // has just fieldname & value $taxonomy = trim( $temp_a[0] ); $term = trim( $temp_b[0] ); } }else{ $temp_b = explode('=', $temp_a[0], 2); if($temp_b[1]){ // has just fieldname & value $taxonomy = trim( $temp_b[0] ); $term = trim( $temp_b[1] ); }else{ // has just value $term = trim( $temp_b[0] ); } } return(array('taxonomy' => $taxonomy, 'term' => $term)); } // add tools to edit screens function edit_current_user_can( $user_caps, $requested_caps, $cap_data ){ // this bit of code taken from Blicki and used under the terms of the GPL // Blicki info: http://wordpress.org/extend/plugins/blicki/ // http://www.blicki.com/ global $post_ID, $page_ID; $requested_cap = $cap_data[0]; $user_id = $cap_data[1]; if ( isset($cap_data[2]) && ( 0 < (int) $cap_data[2] )) $post_id = $cap_data[2]; else if ( isset($post_ID) && ( 0 < (int) $post_ID )) $post_id = $post_ID; else if ( isset($page_ID) && ( 0 < (int) $page_ID )) $post_id = $page_ID; //$current_user = new WP_User($user_id); switch( $requested_cap ){ case 'publish_pages': foreach ($requested_caps as $req_cap) $req_caps[$req_cap] = true; $who_can_edit = get_post_meta($post_id, '_bsuite_who_can_edit', true); if ( empty($who_can_edit) ) $who_can_edit = get_settings('bsuite_who_can_publish'); switch( $who_can_edit ){ case 'anyone': $user_caps = array_merge($user_caps, $req_caps); break; case 'registered_users': if ( is_user_logged_in() ) $user_caps = array_merge($user_caps, $req_caps); break; case 'authors': if ( is_user_logged_in() && isset($user_caps['author'])) $user_caps = array_merge($user_caps, $req_caps); break; default: $caps = map_meta_cap('edit_page', $user_id, $post_id); foreach ($caps as $cap) { if ( empty($user_caps[$cap]) || !$user_caps[$cap] ) return $user_caps; } $user_caps = array_merge($user_caps, $req_caps); } break; case 'edit_page': case 'edit_others_pages': case 'edit_published_pages': foreach ($requested_caps as $req_cap) $req_caps[$req_cap] = true; $who_can_edit = get_post_meta($post_id, '_bsuite_who_can_edit', true); if ( empty($who_can_edit) ) $who_can_edit = get_settings('bsuite_who_can_edit'); if ( 'anyone' == $who_can_edit ) { $user_caps = array_merge($user_caps, $req_caps); } else if ('registered_users' == $who_can_edit ) { if ( is_user_logged_in() ) $user_caps = array_merge($user_caps, $req_caps); } else if ('authors' == $who_can_edit ) { if ( is_user_logged_in() && isset($user_caps['author'])) $user_caps = array_merge($user_caps, $req_caps); } else { $caps = map_meta_cap('edit_page', $user_id, $post_id); foreach ($caps as $cap) { if ( empty($user_caps[$cap]) || !$user_caps[$cap] ) return $user_caps; } $user_caps = array_merge($user_caps, $req_caps); } break; case 'edit_pages': case 'read': foreach ($requested_caps as $req_cap) $req_caps[$req_cap] = true; $who_can_edit = get_option('bsuite_who_can_edit'); if ( 'anyone' == $who_can_edit ) { $user_caps = array_merge($user_caps, $req_caps); } else if ('registered_users' == $who_can_edit ) { if ( is_user_logged_in() ) $user_caps = array_merge($user_caps, $req_caps); } return $user_caps; break; case 'delete_page': case 'delete_pages': foreach ($requested_caps as $req_cap) $req_caps[$req_cap] = true; $who_can_delete = get_option('bsuite_who_can_delete'); if ( 'anyone' == $who_can_delete ) { $user_caps = array_merge($user_caps, $req_caps); } else if ('registered_users' == $who_can_delete ) { if ( is_user_logged_in() ) $user_caps = array_merge($user_caps, $req_caps); } return $user_caps; break; case 'bsuite_change_access': $caps = map_meta_cap('edit_page', $user_id, $post_id); foreach ($caps as $cap) { if ( empty($user_caps[$cap]) || !$user_caps[$cap] ) return $user_caps; } $user_caps['bsuite_change_access'] = true; break; } return $user_caps; } function edit_publish_page( $post_ID ) { if ( !isset($_POST['bsuite_who_can_edit']) ) return $post_ID; $who = $_POST['bsuite_who_can_edit']; if ( ! update_post_meta($post_ID, '_bsuite_who_can_edit', $who)) add_post_meta($post_ID, '_bsuite_who_can_edit', $who, true); return $post_ID; } function edit_page_form() { // must change hooks. example: http://planetozh.com/blog/2008/02/wordpress-snippet-add_meta_box/ $this->edit_insert_perms_form(); $this->edit_insert_tag_form(); $this->edit_insert_category_form(); $this->edit_insert_excerpt_form(); // $this->edit_insert_tools(); $this->edit_insert_machinetag_form(); } function edit_post_form() { // $this->edit_insert_tools(); $this->edit_insert_machinetag_form(); } function edit_comment_form() { // there's no edit_comment_form hook!!! $this->edit_insert_tag_form(); $this->edit_insert_tools(); $this->edit_insert_machinetag_form(); } function edit_insert_perms_form() { global $post_ID; if ( !current_user_can( 'bsuite_change_access' ) ) return; $who = get_post_meta( $post_ID, '_bsuite_who_can_edit', TRUE ); if ( empty($who) ) $who = get_settings( 'bsuite_who_can_edit' ); ?>

    bSuite Tools: Auto Tag Auto Excerpt (about these tools) (NOT WORKING, for now)
    $tag){ if($tag->taxonomy == 'post_tag' || $tag->taxonomy == 'category') continue; $tags_to_edit[] = '
  • :
  • '; } // natcasesort($tags_to_edit); $key++; $tags_to_edit[] = '
  • :
  • '; ?>

    About machine tags

    use them in your template'); ?>

    0, 'name' => 'newcat_parent', 'orderby' => 'name', 'hierarchical' => 1, 'show_option_none' => __('Parent category'), 'tab_index' => 3 ) ); ?>

    strip_tags( str_replace( array( '<','>' ), array( "\n\n<",">\n\n" ), strip_tags( preg_replace( '/\[(.*?)\]/', '', preg_replace( '!(<(?:h[1-6])[^>]*>[^<]*<(?:\/h[1-6])[^>]*>)!', '', $text )), '