=') ){ //2.3 introduces print_scripts- so we'll use that instead. add_action('admin_print_scripts-ddeviantart/dda-options.php','dda_admin_head'); } else { //If the correct page, we'll want to add a head entry. if( 'options-general.php' == $pagenow && isset( $_GET['page'] ) && 'ddeviantart/dda-options.php' == $_GET['page']) dda_admin_head(); } //Due to the register_activation_hook being brocken on windows, if we're running on windows we re-create the rules every time the admin section is hit. if( false !== strpos(__FILE__,'\\') ) dda_flush_rules(); } function dda_admin_head(){ wp_enqueue_script('jquery'); } /*** Widget support ***/ add_action('widgets_init', 'dda_widget_init'); function dda_widget_init(){ if ( ! function_exists('register_sidebar_widget') ) return; $widgets = get_option('dda_widgets'); foreach((array)$widgets as $widget){ register_sidebar_widget($widget['name'], 'dda_widget', $widget); } } function dda_widget($args,$widget){ if( ! isset($widget['feed']) ) return; extract($args,EXTR_SKIP); echo $before_widget; echo $before_title . $widget['name'] . $after_title; dda_display_widget($widget); echo $after_widget; } function dda_display_widget($widget){ $items = dda_get_feed_items($widget); if( $widget['random'] == '1' ) shuffle($items); foreach($items as $item){ //If no Custom HTML specified, use default HTML. if( empty($widget['html']) ){ echo ''; if( !empty($item['imagelink']) ){ echo ''.attribute_escape($item['title']).''; } else { echo $item['title']; } echo '
'; } else { $find = array('%id%','%link%','%category%','%image%','%title%','%author%'); $replace = array($item['guid'], $item['link'], $item['category'], $item['imagelink'], $item['title'], $item['author']); echo str_replace($find, $replace, stripslashes($widget['html'])); }//end if() }//end foreach() } /*** General Functions ***/ function dda_get_feed_items($widget){ $option = 'dda_'.md5($widget['feed']).$widget['numbertoshow']; $items = get_option($option); //TODO: implement a cleaner to clean old options. if( $items ) return $items; include_once('inc/dCustom.php'); $rss = fetch_rss_timeout($widget['feed'],get_option('dda_timeout')); $items = dda_process_feed_items($rss->items, $widget); update_option($option, $items); return $items; } function dda_process_feed_items($items, $widget){ for($i=0; $i <= count($items); $i++){ $result = dda_deviation_url($items[$i]['guid'] ,$widget['size']); if( ! result ){ $items[$i] = null; unset($items[$i]); continue; } else { //Set the URL $items[$i]['imagelink'] = $result; //Determine the authors name from the image url: if( preg_match('#_by_(.*).jpg$#',$result,$mat) ){ $items[$i]['author'] = $mat[1]; } } //If we have enough images, Return the total currently parsed. if( $i >= $widget['numbertoshow'] ) return array_slice($items,0,$i); }//end for return $items; } function dda_deviation_url($id,$size=150){ $urls = get_option('dda_imageaddresses'); //Poor mans caching, Not using WP Object cache due to never being enabled. if ( isset($urls[ $id . '-' . $size ]) ) return $urls[ $id . '-' . $size ]; include_once( ABSPATH . 'wp-includes/class-snoopy.php' ); $snoopy = new Snoopy(); $snoopy->agent = $_SERVER['HTTP_USER_AGENT']; //Needs to be a valid Browser, DA block non-browsers $snoopy->maxredirs = 0; //Dont follow redirects $snoopy->_httpmethod = "HEAD"; //Dont want the image, just the headers //This URL is used by the Flash-embedable link. $snoopy->fetch('http://www.deviantart.com/global/getthumb.php?size=' . $size . '&id=' . $id ); //If not found if( trim($snoopy->headers[0]) != 'HTTP/1.1 302 Found') return false; //Locate the redirection header, this is the location of the image to display foreach($snoopy->headers as $head){ if( substr($head,0,8) == 'Location' ){ $url = trim(substr($head,9)); break; } } $urls[ $id . '-' . $size ] = $url; update_option('dda_imageaddresses',$urls); return $url; } /*** Inline thumbnail support ***/ if( ! get_option('dda_disableinlinethumbnails') ) add_filter('the_content', 'dda_inline'); function dda_inline($post){ $post = preg_replace_callback('#:thumb(\d+):((\d+)?:)?((\w+):)?#i','dda_inline_callback',$post); return $post; } function dda_inline_callback($matches){ //Defaults $class = get_option('dda_inlineclass'); $size = get_option('dda_inlinesize'); $id = $matches[1]; if( isset($matches[3]) && !empty($matches[3]) ) $size = $matches[3]; if( isset($matches[5]) && !empty($matches[5]) ) $class = $matches[5]; if( '300' == $size ) $size .= 'W'; $img = dda_deviation_url($id, $size); if( get_option('dda_inlinelink') ){ return ""; } return ""; return $ret; } /*** Portfolio stuff ***/ add_action('template_redirect','dda_template'); function dda_template($arg){ global $wp_query; if( !isset($wp_query->query_vars['ddeviantart']) ) return $arg; // we're wanting to display a ddeviantart page, We dont do this here, we'll override the template for this page instead add_filter('home_template','dda_template_override'); return $arg; } function dda_template_override($template){ //Override the template for the current page and replace it with our own. return dirname(__FILE__).'/dda_portfolio.php'; } add_action('get_pages','dda_portfolio_pages'); function dda_portfolio_pages($pages){ if( ! get_option('dda_portfolio_pages') ) return $pages; $url = get_bloginfo('siteurl'); if( substr($url,-1) != '/' ) $url .= '/'; global $wp_rewrite; if( ! $wp_rewite || ! $wp_rewrite->using_permalinks() ) $url .= '?ddeviantart='; $portfolios = get_option('dda_portfolios'); foreach($portfolios as $id=>$portfolio){ $page = new stdClass; $page->post_title = $portfolio['name']; $page->post_name = $portfolio['slug']; $page->guid = $url . $portfolio['slug']; /*$page->post_category = 0; $page->post_parent = 0; $page->post_type = 'page'; $page->post_status = 'publish';*/ $page->ID = $id == 0 ? -1 : -($id)-1; /* we use ID's starting at -1, shouldnt conflict with anything. */ $pages[] = $page; } return $pages; } /*** URL REWRITE STUFF ***/ //If the rewrite rules are regenerated, Add our pretty permalink stuff, redirect it to the correct queryvar add_action('generate_rewrite_rules', 'dda_add_rewrite_rules'); function dda_add_rewrite_rules( $wp_rewrite ) { $pages = get_option('dda_portfolios'); if( empty($pages) ) return; $pageRegex = ''; foreach((array)$pages as $page){ if( '' != $pageRegex) $pageRegex .= '|'.$page['slug']; else $pageRegex = $page['slug']; } $new_rules = array( "($pageRegex)" => 'index.php?ddeviantart=' . $wp_rewrite->preg_index(1) ); $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; } //Add a Query Var, This allows us to access the query var via $wp_query add_filter('query_vars', 'dda_queryvars' ); function dda_queryvars( $qvars ){ $qvars[] = 'ddeviantart'; return $qvars; } //Add a Activation hook for the current page: // NOTE: Broken on win32, see http://trac.wordpress.org/ticket/3002 register_activation_hook(__FILE__,'dda_flush_rules'); function dda_flush_rules(){ //Flush the rewrite rules so that the new rules from this plugin get added. global $wp_rewrite; $wp_rewrite->flush_rules(); } ?>