system_check()) { echo __('Author avatars: system check failed.', 'author-avatars'); } elseif(!$this->install_check()) { echo __('Author avatars: install check failed.', 'author-avatars'); } elseif(!$this->update_check()) { echo __('Author avatars: update check failed.', 'author-avatars'); } else { $this->init_settings(); $this->register_resources(); $this->init_widgets(); $this->init_shortcodes(); $this->init_controlpanels(); $this->init_AuthorAvatarsImageSpriting(); } } /** * Check we got everything we need to use the plugin */ function system_check() { if (!defined('AUTHOR_AVATARS_VERSION')) die('Author Avatars: constant AUTHOR_AVATARS_VERSION is not defined.'); if (!defined('AUTHOR_AVATARS_VERSION_HISTORY')) die('Author Avatars: constant AUTHOR_AVATARS_VERSION_HISTORY is not defined.'); return true; } /** * Include settings class */ function init_settings() { // include global helper functions file. require_once('helper.functions.php'); // include settings file require_once('AuthorAvatarsSettings.class.php'); // load translation domain on init action add_action('init', array($this, 'load_translation_domain')); } /** * Loads the translation domain for ngettext calls * * @see http://codex.wordpress.org/User:Nbachiyski/I18n_for_WordPress_Developers#I18n_for_theme_and_plugin_developers */ function load_translation_domain() { // load translation file $plugin_dir = basename(dirname(dirname(__FILE__))); load_plugin_textdomain( 'author-avatars', false, $plugin_dir . '/translations' ); } /** * Registers all stylesheets and scripts */ function register_resources() { $aa_ver = AUTHOR_AVATARS_VERSION; $plugin_dir = basename(dirname(dirname(__FILE__))); // make sure styles are written on wp_head action add_action('wp_head', 'wp_print_styles'); // styles wp_register_style('author-avatars-widget', WP_PLUGIN_URL . '/'. $plugin_dir .'/css/widget.css', array(), $aa_ver); wp_register_style('author-avatars-shortcode', WP_PLUGIN_URL . '/'. $plugin_dir .'/css/shortcode.css', array(), $aa_ver); wp_register_style('admin-form', WP_PLUGIN_URL . '/'. $plugin_dir .'/css/admin-form.css', array(), $aa_ver); // scripts wp_register_script('jquery-ui-resizable', WP_PLUGIN_URL . '/'. $plugin_dir .'/js/jquery-ui.resizable.js', array('jquery-ui-core'), '1.5.3'); wp_register_script('author-avatars-form', WP_PLUGIN_URL . '/'. $plugin_dir .'/js/form.js', array('jquery-ui-resizable'), $aa_ver); wp_register_script('author-avatars-widget-admin', WP_PLUGIN_URL . '/'. $plugin_dir .'/js/widget.admin.js', array('author-avatars-form'), $aa_ver); wp_register_script('tinymce-popup', '/wp-includes/js/tinymce/tiny_mce_popup.js', array(), function_exists('mce_version') ? mce_version() : false); wp_register_script('author-avatars-tinymce-popup', WP_PLUGIN_URL .'/'. $plugin_dir .'/js/tinymce.popup.js', array('author-avatars-form', 'jquery-ui-tabs'), $aa_ver); } /** * Init author avatar widget */ function init_widgets() { // include necessary file(s). require_once('AuthorAvatarsWidget.class.php'); // Create an object for the widget. Registering is done in the object's constructor $author_avatars_multiwidget = new AuthorAvatarsWidget(); } /** * Init author avatars shortcodes */ function init_shortcodes() { // include necessary file(s). require_once('AuthorAvatarsShortcode.class.php'); require_once('ShowAvatarShortcode.class.php'); require_once('AuthorAvatarsEditorButton.class.php'); // Create objects of the shortcode classes. Registering is done in the objects' constructors $author_avatars = new AuthorAvatarsShortcode(); $show_avatar = new ShowAvatarShortcode(); $editor_button = new AuthorAvatarsEditorButton(); } /** * Init control panels */ function init_controlpanels() { // include necessary file(s). require_once('AuthorAvatarsSitewideAdminPanel.class.php'); $wpmu_settings = new AuthorAvatarsSitewideAdminPanel(); } /** * Init AuthorAvatarsImageSpriting */ function init_AuthorAvatarsImageSpriting() { // first task do we have a table to store Image metadate in global $wpdb; $table_name = $wpdb->prefix . "AuthorAvatars"; if(strcmp ($wpdb->get_var("show tables like '$table_name'"),$table_name) < 0) { $sql = "CREATE TABLE " . $table_name . " ( id mediumint(9) NOT NULL AUTO_INCREMENT, url VARCHAR(255) NOT NULL, size mediumint(9) NOT NULL, offset mediumint(9) NOT NULL, INDEX (url), UNIQUE KEY id (id) );"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } // include necessary file(s). require_once('AuthorAvatarsImageSpriting.class.php'); $AuthorAvatarsImageSpriting = new AuthorAvatarsImageSpriting(); } /** * Number of the currently installed version of the plugin. * @access private */ var $__version_installed = null; /** * returns the version number of the currently installed plugin. */ function get_installed_version($reset = false) { if ($this->__version_installed == null || $reset) { $this->__version_installed = get_option('author_avatars_version'); } return $this->__version_installed; } /** * updates the number of the currently installed version. */ function set_installed_version($value) { $oldversion = $this->get_installed_version(); if (empty($oldversion)) { add_option('author_avatars_version', $value); } else { update_option('author_avatars_version', $value); } $this->__version_installed = $value; } /** * Check if author avatars is installed and install it if necessary * @return false if an error occured, true otherwise */ function install_check() { $version = $this->get_installed_version(true); // Version not empty -> plugin already installed if (!empty($version)) return true; // Version empty: this means we are either on version 0.1 (which didn't have this option) or on a fresh install. else { // check if the 0.1 version is installed if (get_option('widget_blogauthors')) { // set installed version to 0.1 $this->set_installed_version('0.1'); return true; } // else it's probably a new/fresh install else { if ($this->install()) { $this->set_installed_version(AUTHOR_AVATARS_VERSION); return true; } else { return false; // install failed. } } } } /** * install the plugin * @return true if install was successful, false otherwise */ function install() { // nothing to install return true; } /** * Check if there's any need to do updates and start updates if necessary * @return false if an error occured, true otherwise */ function update_check() { if ($this->get_installed_version() != AUTHOR_AVATARS_VERSION) $this->do_updates(); return true; } /** * tries to do all updates until we're up to date */ function do_updates() { $step_count = 0; $max_number_updates = 25; while ($this->get_installed_version() != AUTHOR_AVATARS_VERSION) { if ($step_count >= $max_number_updates) { break; die('Author Avatars: more than 25 update steps.. something might be wrong...'); // FIXME: change error handling!? } $this->do_update_step(); $step_count++; } } /** * Do one version update, for example from version 0.1 to version 0.2, and updates the version number in the end. */ function do_update_step() { $version_history = unserialize(AUTHOR_AVATARS_VERSION_HISTORY); foreach ($version_history as $i => $version) { // for the current version, if there is a next version if ($version == $this->get_installed_version() && ($i+1) < count($version_history)) { $new_version = $version_history[$i+1]; $fn = 'update__'. ereg_replace("[^0-9]","",$version) .'_'. ereg_replace("[^0-9]","",$new_version); if (method_exists($this, $fn) && !$this->{$fn}()) { die('Author Avatars: error trying to update version '. $version .' to '. $new_version .'. '); // FIXME: change error handling!? } else { $this->set_installed_version($new_version); } break; } } } /** * Do update step 0.1 to 0.2 */ function update__01_02() { // update database: convert old widgets to new ones using the "MultiWidget" class. $old_widget = get_option('widget_blogauthors'); $new_widget = $old_widget; foreach ($new_widget as $id => $widget) { $new_widget[$id]['__multiwidget'] = $id; $new_widget[$id]['title'] = __('Blog Authors', 'author-avatars'); } delete_option('widget_blogauthors'); add_option('multiwidget_author_avatars', $new_widget); // update sidebar option $sidebars = get_option('sidebars_widgets'); foreach ($sidebars as $i => $sidebar) { if(is_array($sidebar)) { foreach ($sidebar as $k => $widget) { $sidebars[$i][$k] = str_replace('blogauthors-', 'author_avatars-', $widget); } } } update_option('sidebars_widgets', $sidebars); // return true if update successful return true; } /** * Do update step 0.6.2 to 0.7 */ function update__062_07() { $widgets = (array) get_option('multiwidget_author_avatars'); foreach ($widgets as $id => $widget) { if (!empty($widget['display']) && is_array($widget['display'])) { $key = array_search('link_to_authorpage', $widget['display']); if ($key !== false) { unset($widgets[$id]['display'][$key]); $widgets[$id]['display']['user_link'] = 'authorpage'; } } } update_option('multiwidget_author_avatars', $widgets); return true; } } ?>