Options page Author: Dion Hulse Author URI: http://dd32.id.au/ Plugin URI: http://dd32.id.au/wordpress-plugins/ddeviantart/ */ global $dda_version; $dda_version = "0"; add_action('init', 'dda_init'); function dda_init(){ global $dda_version; include_once 'functions.php'; include_once 'class.deviantart.php'; include_once 'class.ddacache.php'; if( false === get_option('dda_version') || get_option('dda_version') != $dda_version){ dda_update10(); update_option('dda_version', $dda_version); } /*** Inline thumbnail support ***/ if( ! get_option('dda_disableinlinethumbnails') ) add_filter('the_content', 'dda_inline'); if( get_option('dda_portfolio_pages') ) add_action('get_pages', 'dda_portfolio_pages'); } add_action('admin_menu', 'dda_admin_init'); /** * Admin init. Add admin hooks and menu items. * * @param void * @return void */ function dda_admin_init(){ add_options_page('dDeviantArt', 'DeviantArt', 'administrator', 'ddeviantart/options.php'); add_action('admin_print_scripts-ddeviantart/options.php', 'dda_admin_head'); } function dda_update10(){ //Clear cache first dda_delete_cache(false); //Widgets. $widgets = get_option('dda_widgets'); $new_widgets = array(); foreach($widgets as $key => $widget){ if( isset($widget['slug']) ){ $new_widgets[ $widget['slug'] ] = $widget; continue; //Allready converted... } $new_widget = array(); $new_widget['name'] = $widget['name']; $new_widget['slug'] = sanitize_title($widget['name']); $new_widget['order'] = str_replace('-', '', $widget['order']); $new_widget['scraps'] = (bool)$widget['includescraps']; $new_widget['number'] = (int)$widget['numbertoshow']; $new_widget['size'] = (int)$widget['size']; $new_widget['type'] = $widget['type']; if( 'query' == $new_widget['type'] ){ $new_widget['user'] = ('by' == $widget['who']); $new_widget['fav'] = ('favby' == $widget['who']); $new_widget['search'] = ('search' == $widget['who']); if( $new_widget['user'] ) $new_widget['term'] = $widget['byname']; if( $new_widget['fav'] ) $new_widget['term'] = $widget['favbyname']; if( $new_widget['search'] ) $new_widget['term'] = $widget['search']; } else if( 'rss' == $new_widget['type'] ){ $new_widget['user'] = false; $new_widget['fav'] = false; $new_widget['user'] = false; $new_widget['feed'] = $widget['feed']; } if( $widget['html'] != dda_default_widget_html() ) $new_widget['html'] = $widget['html']; $new_widgets[ $new_widget['slug'] ] = $new_widget; } if( $widgets != $new_widgets ) update_option('dda_widgets', $new_widgets); } function dda_default_widget_html(){ return ' %title% by %symbol%%author%
'; } /** * Admin Head, addd jQuery, Deregister prototype as its unneeded on our pages. * * @param void * @return void */ function dda_admin_head(){ wp_enqueue_script('jquery'); } /*** Widget support ***/ add_action('widgets_init', 'dda_widget_init'); /** * Widget Init. Register all sidebar widgets * * @param void * @return void */ function dda_widget_init(){ if ( ! function_exists('register_sidebar_widget') ) return; $widget_ops = array('classname' => 'dda_widget', 'description' => __('Allows you to insert Deviations into your Blog posts')); if( ! $widgets = get_option('dda_widgets') ) $widgets = array(); foreach($widgets as $widget) wp_register_sidebar_widget( $widget['slug'], $widget['name'], 'dda_widget', $widget_ops + array( 'widget' => $widget ) ); } /** * Display a Sidebar widget * * @param array $args mixed Options for display * @param array $widget widget options * @return void */ function dda_widget($args,$slug = ''){ extract($args,EXTR_SKIP); $widgets = get_option('dda_widgets'); $widget = & $widgets[ $widget_id ]; echo $before_widget; echo $before_title . $widget['name'] . $after_title; dda_display_widget($widget); echo $after_widget; } /** * Display a widget * * @param array $widget widget options * @return void */ function dda_display_widget($widget){ $items = DeviantArt::getDeviations($widget); if( ! $items ) return; if( $widget['random'] ) shuffle($items); if( $widget['number'] > 0 && count($items) > $widget['number'] ) $items = array_slice($items, 0, $widget['number']); $html = isset($widget['html']) && !empty($widget['html']) ? $widget['html'] : dda_default_widget_html(); foreach($items as $item){ $item_html = $html; preg_match_all('|(%.+?%)|', $html, $matches); $matches = array_unique($matches[0]); foreach( (array)$matches as $match){ switch($match){ case '%link%': $replace = 'http://www.deviantart.com/deviation/' . $item->id . '/'; break; case '%title%': $replace = $item->title; break; case '%category%': $replace = $item->category; break; case '%symbol%': $replace = $item->userSymbol; break; case '%author%': $replace = $item->user; break; case '%time%': $replace = $item->submitted; break; case '%id%': $replace = $item->id; break; case '%image%': $replace = $item->getURL(); break; case '%image150%': $replace = $item->getURL(100); break; case '%image150%': $replace = $item->getURL(150); break; case '%image300%': $replace = $item->getURL(300); break; case '%imagefull%': $replace = $item->getURL('full'); break; default: $replace = $match; } $item_html = str_replace($match, $replace, $item_html); } echo $item_html; } } /** * Display a widget via PHP Embed * * @param string $name the name of the Widget to embed, Defaults to first widget * @return void */ function dda_widget_embed($name = false){ $widgets = get_option('dda_widgets'); if( $name ) $name = sanitize_title_with_dashes($name); //Generate Slug foreach((array)$widgets as $widget){ if( ! $name || sanitize_title_with_dashes($widget['name']) == $name){ dda_display_widget($widget); break; } } } /*** General Functions ***/ /** * Deletes any Cached Feed items (option name = dda_ * * @global $wpdb Wordpress DataBase object * @param boolean $uninstall Set to true to remove all plugin settings. * @return void */ function dda_delete_cache($uninstall = false){ global $wpdb; $noncache = array( 'dda_disableinlinethumbnails', 'dda_hotlink', 'dda_inlineclass', 'dda_inlineize', 'dda_inlinelink', 'dda_inlinesize', 'dda_inlinethumbnails', 'dda_portfolios', 'dda_portfolio_pages', 'dda_timeout', 'dda_widgets'); //We dont want to delete user settings. $sql = "SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE 'dda_%'"; if( ! $uninstall ) $sql .= " AND option_name NOT IN ('" . implode("','",$noncache) . "')"; $items = $wpdb->get_col( $sql ); foreach( (array) $items as $item) delete_option($item); DDA_Cache::cleanup(); if( ! $uninstall ) add_option('dda_imageaddresses', array() ); return true; } /*** Inline thumbnail support ***/ /** * Replaces the :thumb555555: codes in posts/pages to a Deviation URL * * @param string $post the content of the post * @return string $post */ function dda_inline($post){ $post = preg_replace_callback('#:thumb(\d+):((\d+)?:)?((\w+):)?#i', 'dda_inline_callback', $post); return $post; } /** * Replaces the :thumb555555: codes in posts/pages to a Deviation URL, Does the hard lifting * * @param mixed $matches array of matched thumburls and extra options * @return string $ret the HTML to replace with */ 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]; $imgurl = DeviantArt::getImageURL($id, $size); //$imgurl = dda_deviation_url($id, $size); $img = ""; if( get_option('dda_inlinelink') ){ if( ! $imgurl ) return sprintf("%s $id",__('View Deviation')); return "$img"; } if( $imgurl ) return $img; else return ''; } /*** Portfolio stuff ***/ add_action('template_redirect','dda_template',9); /** * Redirects the template if the user has landed on a DA portfolio page * * @param string $arg the current template * @return string $arg the current template */ function dda_template($arg){ global $wp_query; if( !isset($wp_query->query_vars['ddeviantart']) ) return $arg; $wp_query->is_404 = false; // 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; } /** * Redirects the template to dda custom template * * @param string $template the current template (unused) * @return string dda custom template */ function dda_template_override($template){ //Override the template for the current page and replace it with our own. return dirname(__FILE__).'/dda_portfolio.php'; } /** * Inserts the dda portfolio pages into the page listing * * @param array $pages The pages objects * @return array $pages */ function dda_portfolio_pages($pages){ $portfolios = get_option('dda_portfolios'); if( empty($portfolios) || ! is_array($portfolios) ) return $pages; $url = get_bloginfo('siteurl'); $url = trailingslashit($url); foreach( (array) $portfolios as $id=>$portfolio){ $page = new stdClass; $page->post_title = $portfolio['name']; $page->post_name = $portfolio['slug']; //$page->guid = $url . $portfolio['slug'] . ($permalinks ? '/' : ''); $page->ID = ( $id == 0 ) ? -1 : -($id+1); /* we use ID's starting at -1, shouldnt conflict with anything. */ $pages[] = $page; } return $pages; } /** * Modifies the page link to ?ddeviantart for the negitive numbers * * @param string $link the current link * @param int $id the id of the current link * @return string $link the new link */ add_filter('_get_page_link', 'dda_portfolio_pages_link',10,2); function dda_portfolio_pages_link($link, $id=1){ global $wp_rewrite; if($id > 0) return $link; $portfolios = get_option('dda_portfolios'); $portfolio = $portfolios[ -($id)-1 ]; if( ! empty($portfolio) ){ if( ! $wp_rewrite || ! $wp_rewrite->using_permalinks() ) $link = preg_replace('!\?p\w*=-\d+!', '?ddeviantart=' . $portfolio['slug'], $link); else $link .= $portfolio['slug'] . '/'; } return $link; } /*** 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'); /** * Adds rewrite rules for pretty permalinks * * @param object $wp_rewrite the Rewrite object * @return void */ function dda_add_rewrite_rules( $wp_rewrite ) { $pages = get_option('dda_portfolios'); if( empty($pages) ) return; $pageRegex = ''; foreach((array)$pages as $page){ if( empty($pageRegex) ) $pageRegex = $page['slug']; else $pageRegex .= '|' . $page['slug']; } $new_rules = array( "($pageRegex)" => 'index.php?ddeviantart=' . $wp_rewrite->preg_index(1) ); $wp_rewrite->rules = $wp_rewrite->rules + $new_rules; } //Add a Query Var, This allows us to access the query var via $wp_query add_filter('query_vars', 'dda_queryvars' ); /** * Adds the query vars pretty permalinks * * @param object $qvars the current Query vars * @return void */ function dda_queryvars( $qvars ){ $qvars[] = 'ddeviantart'; return $qvars; } register_activation_hook(__FILE__,'dda_flush_rules'); /** * Flushes the rewrite rules causing them to be recreated on a plugin activation. * * @param void * @return void */ 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(); dda_delete_cache(); } ?>