0, 'logging_enabled' => 0, ); private $app_title = ''; private $plugin_description = ''; private $plugin_uploads_path = null; // E.g. /wp-content/uploads/PLUGIN_ID_STR/ private $plugin_uploads_url = null; // E.g. http://yourdomain/wp-content/uploads/PLUGIN_ID_STR/ private $plugin_uploads_dir = null; // E.g. DOC_ROOT/wp-content/uploads/PLUGIN_ID_STR/ private $download_key = null; // the param that will hold the download hash private $web_trigger_key = null; // the param will trigger something to happen. (e.g. PayPal IPN, test check etc.) // can't be instantiated; just using get_instance private function __construct() { } /** * handles the singleton */ function get_instance() { if (is_null(self::$instance)) { global $wpdb; $cls = __CLASS__; $inst = new $cls; $site_url = get_settings('siteurl'); $site_url = rtrim($site_url, '/') . '/'; // e.g. http://domain.com/blog/ $inst->site_url = $site_url; $inst->plugin_dir_name = basename(dirname(__FILE__)); // e.g. wp-command-center; this can change e.g. a 123 can be appended if such folder exist $inst->plugin_data_dir = dirname(__FILE__) . '/data'; $inst->plugin_url = $site_url . 'wp-content/plugins/' . $inst->plugin_dir_name . '/'; $inst->plugin_settings_key = $inst->plugin_id_str . '_settings'; $inst->plugin_support_link .= '&css_file=' . urlencode(get_bloginfo('stylesheet_url')); $inst->plugin_admin_url_prefix = $site_url . 'wp-admin/admin.php?page=' . $inst->plugin_dir_name; $opts = $inst->get_options(); if (!$inst->log_enabled && !empty($opts['logging_enabled'])) { $inst->log_enabled = $opts['logging_enabled']; } // the log file be: log.1dd9091e045b9374dfb6b042990d65cc.2012-01-05.log if ($inst->log_enabled) { $inst->log_file = $inst->plugin_data_dir . '/log.' . md5($site_url . $inst->plugin_dir_name) . '.' . date('Y-m-d') . '.log'; } add_action('plugins_loaded', array($inst, 'init'), 100); define('ORBISIUS_WP_ADMIN_UI_SIMPLIFICATOR_BASE_DIR', dirname(__FILE__)); // e.g. // htdocs/wordpress/wp-content/plugins/wp-command-center define('ORBISIUS_WP_ADMIN_UI_SIMPLIFICATOR_DIR_NAME', $inst->plugin_dir_name); self::$instance = $inst; } return self::$instance; } public function __clone() { trigger_error('Clone is not allowed.', E_USER_ERROR); } public function __wakeup() { trigger_error('Unserializing is not allowed.', E_USER_ERROR); } /** * Logs whatever is passed IF logs are enabled. */ function log($msg = '') { if ($this->log_enabled) { $msg = '[' . date('r') . '] ' . '[' . $_SERVER['REMOTE_ADDR'] . '] ' . $msg . "\n"; error_log($msg, 3, $this->log_file); } } /** * :) * @see http://codex.wordpress.org/Function_Reference/wp_enqueue_script */ function load_scripts() { $scripts = array( ); wp_enqueue_script('jquery'); foreach ($scripts as $js_file) { $js_file_fmt = preg_replace('#\W#si', '_', $js_file); wp_enqueue_script( $this->plugin_id_str . '_js_' . $js_file_fmt, $this->plugin_url . 'js/' . $js_file ); } $css_styles = array( 'main.css', ); foreach ($css_styles as $css_file) { $css_file_fmt = preg_replace('#\W#si', '_', $css_file); wp_register_style($this->plugin_id_str . '_css_' . $css_file_fmt, $this->plugin_url . 'css/' . $css_file); wp_enqueue_style($this->plugin_id_str . '_css_' . $css_file_fmt); } } /** * handles the init */ function init() { global $wpdb; if (is_admin()) { // Administration menus add_action('admin_menu', array($this, 'administration_menu')); add_action('admin_init', array($this, 'register_settings')); add_action('admin_notices', array($this, 'notices')); // Hide the menu only for other users/admins if ($this->canSimplify()) { add_action('admin_menu', array($this, 'remove_menus'), 9999); add_action('admin_bar_menu', array($this, 'remove_nav_bar_items'), 9999); add_action('wp_dashboard_setup', array($this, 'remove_dashboard_widgets'), 9999); } else { add_action('admin_bar_menu', array($this, 'add_switch'), 9999); } wp_register_style($this->plugin_dir_name, $this->plugin_url . 'css/main.css', false, filemtime(ORBISIUS_WP_ADMIN_UI_SIMPLIFICATOR_BASE_DIR . '/css/main.css')); wp_enqueue_style($this->plugin_dir_name); /*wp_enqueue_script( $this->plugin_id_str . '_main_js', $this->plugin_url . 'js/main.js' );*/ } if (!is_feed()) { add_action('wp_head', array($this, 'add_plugin_credits'), 1); // be the first in the header add_action('wp_footer', array($this, 'add_plugin_credits'), 1000); // be the last in the footer } add_action('wp_enqueue_scripts', array($this, 'load_scripts')); // Add hook for admin
add_action('admin_head', array($this, 'my_custom_js')); // Add hook for front-end // add_action('wp_head', array($this, 'my_custom_js')); } /** * @see http://stackoverflow.com/questions/5849057/adding-script-to-wordpress-in-head-elemen */ function my_custom_js() { $plugin_url = $this->plugin_url; echo <<< JS_EOF JS_EOF; } /** * a check wheather should be simplify or not. */ function canSimplify() { $status = false; $opts = $this->get_options(); $current_user = $this->get_user(); // Hide the menu only for other users/admins if (!empty($opts['skip_simplification_for_id']) && $current_user->ID != $opts['skip_simplification_for_id']) { $status = true; } return $status; } /** * @see http://codex.wordpress.org/Dashboard_Widgets_API */ function dashboard_welcome_widget() { echo "Welcome to the dashboard."; } /** * @see http://www.catswhocode.com/blog/10-wordpress-dashboard-hacks * @global type $wp_meta_boxes */ function remove_dashboard_widgets() { // Globalize the metaboxes array, this holds all the widgets for wp-admin global $wp_meta_boxes; // Remove the incomming links widget unset($wp_meta_boxes['dashboard']['normal']); unset($wp_meta_boxes['dashboard']['side']); wp_add_dashboard_widget($this->plugin_id_str . '_dashboard_widget', 'Welcome', array($this, 'dashboard_welcome_widget')); /*unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']); // Remove right now unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']); unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']); unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);*/ } /** * TODO: allow switching to default UI on/off */ function add_switch($wp_admin_bar) { $opts = $this->get_options(); // if the main admin has allowed the other users to swtich put this in the admin if (0&&!empty($opts['allow_switch_to_default_ui_on_off'])) { $args = array( 'id' => $this->plugin_id_str . '_switch_to_default_ui_on_off', // id of the existing child node (New > Post) 'title' => 'Switch to Simple', // alter the title of existing node 'parent' => false, // 'top-secondary' // set parent to false to make it a top level (parent) node 'href' => '', 'meta' => array('class' => 'ab-top-secondary'), ); $wp_admin_bar->add_node($args); } } /** * This removes the clutter from the admin area BUT doesn't stop the access to the access to those menus. * @see http://www.wprecipes.com/how-to-remove-menus-in-wordpress-dashboard * @see http://hungred.com/how-to/remove-wordpress-admin-menu-affecting-wordpress-core-system/ * @see admin pos http://blog.rutwick.com/add-items-anywhere-to-the-wp-3-3-admin-bar */ function remove_nav_bar_items($wp_admin_bar) { $wp_admin_bar->remove_node('wp-logo'); $wp_admin_bar->remove_menu('new-content'); $wp_admin_bar->remove_menu('updates'); $wp_admin_bar->remove_menu('comments'); $wp_admin_bar->remove_menu('site-name'); $wp_admin_bar->remove_node('top-secondary'); $current_user = wp_get_current_user(); // http://codex.wordpress.org/Function_Reference/wp_get_current_user // Logout $args = array( 'id' => 'logout', // id of the existing child node (New > Post) 'title' => 'Logout ' . $current_user->user_email, // alter the title of existing node // $current_user->ID 'parent' => false, // 'top-secondary' // set parent to false to make it a top level (parent) node 'meta' => array('class' => 'ab-top-secondary'), ); $wp_admin_bar->add_node($args); $args = array( 'id' => 'new-post', // id of the existing child node (New > Post) 'title' => 'Add New Post', // alter the title of existing node 'parent' => false, // set parent to false to make it a top level (parent) node ); $wp_admin_bar->add_node($args); $args = array( 'id' => 'new-page', // id of the existing child node (New > Post) 'title' => 'Add New Page', // alter the title of existing node 'parent' => false // set parent to false to make it a top level (parent) node ); $wp_admin_bar->add_node($args); // add a parent item $args = array( 'id' => 'orb_my_account', 'title' => 'My Account', 'meta' => array('class' => 'ab-top-secondary'), ); $wp_admin_bar->add_node($args); // add a child item to a our parent item $args = array('id' => 'edit-profile', 'title' => 'Edit Profile', 'parent' => 'orb_my_account'); $wp_admin_bar->add_node($args); // add another child item to a our parent item (not to our first group) /*$args = array('id' => 'orb_my_account_sub_change_password', 'title' => 'Change Password', 'parent' => 'orb_my_account', 'href' => '?cmd=upgrade', ); $wp_admin_bar->add_node($args); // add another child item to a our parent item (not to our first group) $args = array( 'id' => 'orb_my_account_sub_affiliates', 'title' => 'Affiliates ($)', 'parent' => 'orb_my_account', 'href' => '?cmd=upgrade', ); $wp_admin_bar->add_node($args); // add a group node with a class "first-toolbar-group" $args = array( 'id' => 'first_group', 'parent' => 'orb_my_account', 'meta' => array('class' => 'first-toolbar-group'), ); $wp_admin_bar->add_group($args); // add an item to a our group item $args = array( 'id' => 'first_grouped_node', 'title' => 'Addons', 'parent' => 'first_group', 'href' => '?cmd=addons', ); $wp_admin_bar->add_node($args); */ // View Site $args = array( 'id' => 'view-site', // id of the existing child node (New > Post) 'title' => 'View Your Site', // alter the title of existing node 'parent' => false, // 'top-secondary' // set parent to false to make it a top level (parent) node 'meta' => array('class' => 'ab-top-secondary', 'target' => '_blank'), ); $wp_admin_bar->add_node($args); } /** * This removes the clutter from the admin area BUT doesn't stop the access to the access to those menus. * @see http://www.wprecipes.com/how-to-remove-menus-in-wordpress-dashboard * @see http://hungred.com/how-to/remove-wordpress-admin-menu-affecting-wordpress-core-system/ */ function remove_menus() { /*remove_menu_page('edit.php'); remove_menu_page('link-manager.php'); remove_menu_page('themes.php'); remove_menu_page('tools.php'); remove_menu_page('upload.php'); remove_menu_page('edit-comments.php'); remove_menu_page('plugins.php'); remove_submenu_page( 'options-general.php', 'options-media.php' ); remove_menu_page('link-manager.php'); //remove_menu_page('themes.php'); remove_submenu_page( 'themes.php', 'themes.php' ); remove_submenu_page( 'themes.php', 'theme-editor.php' ); remove_submenu_page( 'themes.php', 'themes.php?page=custom-background' ); remove_submenu_page( 'widgets.php', 'theme-editor.php' ); remove_menu_page('tools.php'); remove_menu_page('upload.php'); remove_menu_page('edit-comments.php'); remove_menu_page('plugins.php'); remove_menu_page('admin.php?page=w3tc_general'); remove_menu_page('admin.php?page=better_wp_security'); remove_menu_page('admin.php?page=wpcf7'); remove_submenu_page( 'index.php', 'update-core.php' ); remove_submenu_page( 'options-general.php', 'options-discussion.php' ); remove_submenu_page( 'options-general.php', 'options-writing.php' ); remove_submenu_page( 'options-general.php', 'options-reading.php' ); remove_submenu_page( 'options-general.php', 'options-permalink.php' ); remove_submenu_page( 'options-general.php', 'options-media.php' );*/ remove_submenu_page( 'index.php', 'update-core.php' ); global $menu; // __('Posts'), __('Dashboard'), $restricted = array(__('Links'), __('Appearance'), __('Users'), __('Settings'), __('Tools'), __('Comments'), __('Plugins'), ); end ($menu); while (prev($menu)){ $value = explode(' ', $menu[key($menu)][0]); if (in_array($value[0] != NULL ? $value[0] : "" , $restricted)) { unset($menu[key($menu)]); } } } /** * returns the obj of currently logged in user. * @global type $current_user * @return type */ function get_user() { wp_get_current_user(); global $current_user; $current_user->ID; return $current_user; } /** * Handles the plugin activation. creates db tables and uploads dir with an htaccess file */ function on_activate() { $opts = array(); $current_user = $this->get_user(); $id = $current_user->ID; $opts['status'] = 1; // auto enable $opts['skip_simplification_for_id'] = $id; // the plugin will skip this user $this->set_options($opts); } /** * Handles the plugin deactivation. */ function on_deactivate() { $opts = array(); $opts['status'] = 0; $this->set_options($opts); } /** * Handles the plugin uninstallation. */ function on_uninstall() { delete_option($this->plugin_settings_key); } /** * Allows access to some private vars * @param str $var */ public function get($var) { if (isset($this->$var) /* && (strpos($var, 'plugin') !== false) */) { return $this->$var; } } /** * gets current options and return the default ones if not exist * @param void * @return array */ function get_options() { $opts = get_option($this->plugin_settings_key); $opts = empty($opts) ? array() : (array) $opts; // if we've introduced a new default key/value it'll show up. $opts = array_merge($this->plugin_default_opts, $opts); if (empty($opts['purchase_thanks'])) { $opts['purchase_thanks'] = $this->plugin_default_opts['purchase_thanks']; } return $opts; } /** * Updates options but it merges them unless $override is set to 1 * that way we could just update one variable of the settings. */ function set_options($opts = array(), $override = 0) { if (!$override) { $old_opts = $this->get_options(); $opts = array_merge($old_opts, $opts); } update_option($this->plugin_settings_key, $opts); return $opts; } /** * This is what the plugin admins will see when they click on the main menu. * @var string */ private $plugin_landing_tab = '/menu.dashboard.php'; /** * Adds the settings in the admin menu */ public function administration_menu() { // Settings > FirstInsLeadForm // add_options_page(__($this->plugin_name, "ORBISIUS_WP_ADMIN_UI_SIMPLIFICATOR"), __($this->plugin_name, "ORBISIUS_WP_ADMIN_UI_SIMPLIFICATOR"), 'manage_options', $this->plugin_dir_name . '/menu.settings.php'); if (!$this->canSimplify()) { // if this is the admin user show the plugin section add_menu_page(__($this->plugin_name, $this->plugin_dir_name), __($this->plugin_name, $this->plugin_dir_name), 'manage_options', $this->plugin_dir_name . '/menu.dashboard.php', null, $this->plugin_url . '/images/cup.png'); add_submenu_page($this->plugin_dir_name . '/' . $this->plugin_landing_tab, __('Dashboard', $this->plugin_dir_name), __('Dashboard', $this->plugin_dir_name), 'manage_options', $this->plugin_dir_name . '/menu.dashboard.php'); add_submenu_page($this->plugin_dir_name . '/' . $this->plugin_landing_tab, __('Settings', $this->plugin_dir_name), __('Settings', $this->plugin_dir_name), 'manage_options', $this->plugin_dir_name . '/menu.settings.php'); add_submenu_page($this->plugin_dir_name . '/' . $this->plugin_landing_tab, __('Help', $this->plugin_dir_name), __('Help', $this->plugin_dir_name), 'manage_options', $this->plugin_dir_name . '/menu.support.php'); add_submenu_page($this->plugin_dir_name . '/' . $this->plugin_landing_tab, __('Contact', $this->plugin_dir_name), __('Contact', $this->plugin_dir_name), 'manage_options', $this->plugin_dir_name . '/menu.contact.php'); add_submenu_page($this->plugin_dir_name . '/' . $this->plugin_landing_tab, __('About', $this->plugin_dir_name), __('About', $this->plugin_dir_name), 'manage_options', $this->plugin_dir_name . '/menu.about.php'); // when plugins are show add a settings link near my plugin for a quick access to the settings page. add_filter('plugin_action_links', array($this, 'add_plugin_settings_link'), 10, 2); } } /** * Allows access to some private vars * @param str $var */ public function generate_newsletter_box($params = array()) { $file = ORBISIUS_WP_ADMIN_UI_SIMPLIFICATOR_BASE_DIR . '/zzz_newsletter_box.html'; $buffer = Orbisius_WP_Admin_UI_SimplificatorUtil::read($file); wp_get_current_user(); global $current_user; $user_email = $current_user->user_email; $replace_vars = array( '%%PLUGIN_URL%%' => $this->get('plugin_url'), '%%USER_EMAIL%%' => $user_email, '%%PLUGIN_ID_STR%%' => $this->get('plugin_id_str'), '%%admin_sidebar%%' => $this->get('plugin_id_str'), ); if (!empty($params['form_only'])) { $replace_vars['NEWSLETTER_QR_EXTRA_CLASS'] = "app_hide"; } else { $replace_vars['NEWSLETTER_QR_EXTRA_CLASS'] = ""; } if (!empty($params['src2'])) { $replace_vars['SRC2'] = $params['src2']; } elseif (!empty($params['SRC2'])) { $replace_vars['SRC2'] = $params['SRC2']; } $buffer = Orbisius_WP_Admin_UI_SimplificatorUtil::replace_vars($buffer, $replace_vars); return $buffer; } /** * Allows access to some private vars * @param str $var */ public function generate_donate_box() { $msg = ''; $file = ORBISIUS_WP_ADMIN_UI_SIMPLIFICATOR_BASE_DIR . '/zzz_donate_box.html'; if (!empty($_REQUEST['error'])) { $msg = $this->message('There was a problem with the payment.'); } if (!empty($_REQUEST['ok'])) { $msg = $this->message('Thank you so much!', 1); } $return_url = Orbisius_WP_Admin_UI_SimplificatorUtil::add_url_params($this->get('plugin_business_status_url'), array( 'r' => $this->get('plugin_admin_url_prefix') . '/menu.dashboard.php&ok=1', // paypal de/escapes 'status' => 1, )); $cancel_url = Orbisius_WP_Admin_UI_SimplificatorUtil::add_url_params($this->get('plugin_business_status_url'), array( 'r' => $this->get('plugin_admin_url_prefix') . '/menu.dashboard.php&error=1', // 'status' => 0, )); $replace_vars = array( '%%MSG%%' => $msg, '%%AMOUNT%%' => '9.99', '%%BUSINESS_EMAIL%%' => $this->plugin_business_email, '%%ITEM_NAME%%' => $this->plugin_name . ' Donation', '%%ITEM_NAME_REGULARLY%%' => $this->plugin_name . ' Donation (regularly)', '%%PLUGIN_URL%%' => $this->get('plugin_url'), '%%CUSTOM%%' => http_build_query(array('site_url' => $this->site_url, 'product_name' => $this->plugin_id_str)), '%%NOTIFY_URL%%' => $this->get('plugin_business_ipn'), '%%RETURN_URL%%' => $return_url, '%%CANCEL_URL%%' => $cancel_url, ); // Let's switch the Sandbox settings. if ($this->plugin_business_sandbox) { $replace_vars['paypal.com'] = 'sandbox.paypal.com'; $replace_vars['%%BUSINESS_EMAIL%%'] = $this->plugin_business_email_sandbox; } $buffer = Orbisius_WP_Admin_UI_SimplificatorUtil::read($file); $buffer = str_replace(array_keys($replace_vars), array_values($replace_vars), $buffer); return $buffer; } /** * Outputs some options info. No save for now. */ function options() { $orb_wp_simple_ui_obj = Orbisius_WP_Admin_UI_Simplificator::get_instance(); $opts = get_option('settings'); include_once(ORBISIUS_WP_ADMIN_UI_SIMPLIFICATOR_BASE_DIR . '/menu.settings.php'); } /** * Sets the setting variables */ function register_settings() { // whitelist options register_setting($this->plugin_dir_name, $this->plugin_settings_key); } // Add the ? settings link in Plugins page very good function add_plugin_settings_link($links, $file) { if ($file == plugin_basename(__FILE__)) { //$prefix = 'options-general.php?page=' . dirname(plugin_basename(__FILE__)) . '/'; $prefix = $this->plugin_admin_url_prefix . '/'; $dashboard_link = "" . __("Dashboard", $this->plugin_dir_name) . ''; $settings_link = "" . __("Settings", $this->plugin_dir_name) . ''; array_unshift($links, $settings_link); array_unshift($links, $dashboard_link); } return $links; } /** * adds some HTML comments in the page so people would know that this plugin powers their site. */ function add_plugin_credits() { //printf("\n" . '' . PHP_EOL); printf(PHP_EOL . '' . PHP_EOL . PHP_EOL); } /** * Checks if WP simpple shopping cart is installed. */ function notices() { $opts = $this->get_options(); return ; if (empty($opts['status'])) { echo $this->message($this->plugin_name . " is currently disabled. Please, enable it from " . " {$this->plugin_name} > Settings"); } } /** * Outputs a message (adds some paragraphs) */ function message($msg, $status = 0) { $id = $this->plugin_id_str; $cls = empty($status) ? 'error fade' : 'success update'; // update is the WP class for success ?!? $str = <<$msg
MSG_EOF; return $str; } /** * a simple status message, no formatting except color */ function msg($msg, $status = 0, $use_inline_css = 0) { $inline_css = ''; $id = $this->plugin_id_str; $cls = empty($status) ? 'app_error' : 'app_success'; if ($use_inline_css) { $inline_css = empty($status) ? 'background-color:red;' : 'background-color:green;'; $inline_css .= 'text-align:center;margin-left: auto; margin-right:auto; padding-bottom:10px;color:white;'; } $str = <<