PageRenderTime 35ms CodeModel.GetById 26ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/laguna.php

https://github.com/aramboyajyan/laguna
PHP | 256 lines | 119 code | 38 blank | 99 comment | 11 complexity | 6f9af2cedeb24aeb6f0a112aa17dbc4a MD5 | raw file
  1<?php
  2
  3/**
  4 * @file
  5 * Plugin deverlopment framework for custom WordPress plugins.
  6 *
  7 * Plugin and custom plugin framework created by: Topsitemakers.
  8 * http://www.topsitemakers.com/
  9 */
 10
 11/**
 12 * Plugin name: Laguna Framework
 13 * Description: Custom plugin framework that contains many parts WordPress is missing for proper coding of custom plugins. Logging, custom admin pages, several security measures, view rendering, flash messaging and more.
 14 * Author: Topsitemakers
 15 * Author URI: http://www.topsitemakers.com/
 16 * Version: 1.0
 17 */
 18
 19// Sanity check.
 20if (!defined('ABSPATH')) die('Direct access is not allowed.');
 21
 22// Constant variables used in the plugin.
 23require dirname(__FILE__) . '/includes/constants.php';
 24
 25// Helper functions.
 26require dirname(__FILE__) . '/includes/helper.common.php';
 27require dirname(__FILE__) . '/includes/helper.form.php';
 28
 29// Log items table class. Allow overriding from other plugins if necessary.
 30if (!class_exists('Laguna_Logs')) {
 31  require dirname(__FILE__) . '/includes/class.logs.php';
 32}
 33
 34/**
 35 * Main plugin class.
 36 */
 37class Laguna {
 38
 39  // Plugin name; to be used throughout this class has to be the same as the
 40  // plugin folder name.
 41  var $namespace = 'laguna';
 42
 43  /**
 44   * Constructor.
 45   */
 46  function __construct() {
 47    // Actions.
 48    add_action('init', array(&$this, 'init'));
 49    add_action('admin_init', array(&$this, 'admin_init'));
 50    add_action('admin_menu', array(&$this, 'admin_menu'));
 51    add_action('admin_notices', array(&$this, 'admin_notices'));
 52    add_action('admin_bar_menu', array(&$this, 'admin_bar_menu'));
 53    add_action('wp_after_admin_bar_render', array(&$this, 'render_menu'));
 54    
 55    // Actions used for recreating the session. Make sure the callback for
 56    // recreating the session is called last upon login/logout.
 57    add_action('init', array(&$this, 'recreate_session'));
 58    add_action('wp_login', array(&$this, 'recreate_session'), 100);
 59    add_action('wp_logout', array(&$this, 'recreate_session'), 100);
 60
 61    // Registers.
 62    register_activation_hook(__FILE__, array(&$this, 'install'));
 63
 64    // Filters.
 65    // Change message displayed upon unsuccessful user login. This is a
 66    // security measure to prevent potential attackers find out which part of
 67    // the credentials are not correct. The callback function will display a
 68    // configurable generic message to the user.
 69    if (get_option(LAGUNA_SHORTNAME . 'login_errors_enabled')) {
 70      add_filter('login_errors', array(&$this, 'login_errors'));
 71    }
 72    // Remove WordPress version from the HTML output.
 73    add_filter('the_generator', array(&$this, 'the_generator'));
 74  }
 75
 76  /**
 77   * Plugin installation.
 78   */
 79  public function install() {
 80
 81    global $wpdb;
 82    
 83    // Define table names.
 84    $table_name_laguna   = $wpdb->prefix . 'laguna_log';
 85    
 86    // Check if the tables already exist.
 87    if ($wpdb->get_var("SHOW TABLES LIKE '" . $table_name_laguna . "'") != $table_name_laguna) {
 88      // Table SQL
 89      $table_laguna = "CREATE TABLE IF NOT EXISTS `{$table_name_laguna}` (
 90                        `ID` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary key.',
 91                        `ip_address` varchar(100) NOT NULL COMMENT 'IP address of the computer who triggered the log entry.',
 92                        `time` int(11) NOT NULL COMMENT 'UNIX timestamp of when the event was logged.',
 93                        `type` varchar(128) NOT NULL COMMENT 'Type of the logged message.',
 94                        `output` text COMMENT 'Logged output.',
 95                        PRIMARY KEY (`ID`)
 96                      ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='Debug logging for custom plugin development.' AUTO_INCREMENT=1 ;";
 97
 98      // Get the upgrade PHP and create the tables.
 99      require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
100      dbDelta($table_laguna);
101    }
102    
103    /**
104     * Setup default values of the variables.
105     *
106     * The reason we are double checking if the value is set, is to prevent
107     * overwriting the settings when the plugin is already installed and is
108     * just disabled and enabled again.
109     */
110    // Number of rows to be displayed.
111    if (!laguna_option_exists(LAGUNA_SHORTNAME . 'rows_to_display')) {
112      add_option(LAGUNA_SHORTNAME . 'rows_to_display', '15');
113    }
114    // Enable by default the login errors override. If necessary this can be
115    // changed from admin panel.
116    if (!laguna_option_exists(LAGUNA_SHORTNAME . 'login_errors_enabled')) {
117      add_option(LAGUNA_SHORTNAME . 'login_errors_enabled', TRUE);
118    }
119    // Default text displayed on unsuccessful login.
120    if (!laguna_option_exists(LAGUNA_SHORTNAME . 'login_error_text')) {
121      add_option(LAGUNA_SHORTNAME . 'login_error_text', 'Username and/or password is incorrect. Please try again.');
122    }
123    // Default date format.
124    if (!laguna_option_exists(LAGUNA_SHORTNAME . 'date_format')) {
125      add_option(LAGUNA_SHORTNAME . 'date_format', 'F d Y, H:i:s');
126    }
127
128  }
129
130  /**
131   * General init.
132   */
133  public function init() {
134    
135    // Common styles.
136    wp_enqueue_style($this->namespace . '-style-common', plugins_url($this->namespace . '/assets/css/common.css'));
137    // Common scripts.
138    wp_enqueue_script($this->namespace . '-script-common', plugins_url($this->namespace . '/assets/js/common.js'), array('jquery'));
139    
140  }
141
142  /**
143   * Admin init.
144   */
145  public function admin_init() {
146
147    // Admin styles.
148    wp_enqueue_style($this->namespace . '-style-admin', plugins_url($this->namespace . '/assets/css/admin.css'));
149    wp_enqueue_style('thickbox');
150
151    // Admin scripts.
152    wp_enqueue_script($this->namespace . '-script-admin', plugins_url($this->namespace . '/assets/js/admin.js'), array('jquery'));
153    wp_enqueue_script('media-upload');
154
155  }
156
157  /**
158   * Start and/or recreate the session.
159   */
160  public function recreate_session() {
161    // if (!session_id()) {
162      // session_start();
163    // }
164  }
165
166  /**
167   * Define links for administrators.
168   */
169  public function admin_menu() {
170
171    // Main settings page.
172    add_menu_page(__('Developer'), __('Developer'), 'manage_options', $this->namespace . '/admin-pages/view-log.php');
173
174    // Subpages.
175    add_submenu_page($this->namespace . '/admin-pages/view-log.php', __('View log'), __('View log'), 'manage_options', $this->namespace . '/admin-pages/view-log.php');
176    add_submenu_page($this->namespace . '/admin-pages/view-log.php', __('Settings'), __('Settings'), 'manage_options', $this->namespace . '/admin-pages/settings.php');
177
178  }
179
180  /**
181   * Add debugging link to the admin navigation bar.
182   */
183  public function admin_bar_menu() {
184    global $wp_admin_bar;
185    $wp_admin_bar->add_menu(array(
186      'id' => 'laguna',
187      'parent' => 'top-secondary',
188      'title' => __('Logging'),
189    ));
190  }
191
192  /**
193   * Render our menu.
194   */
195  public function render_menu() {
196    global $wpdb;
197    // Get recent logs.
198    $rows_to_display = get_option(LAGUNA_SHORTNAME . 'rows_to_display');
199    $query = $wpdb->prepare("SELECT * FROM {$wpdb->prefix}laguna_log ORDER BY `time` DESC LIMIT 0, %d", array($rows_to_display));
200    $logs = $wpdb->get_results($query);
201    // Format the time in logs.
202    $format = get_option(LAGUNA_SHORTNAME . 'date_format');
203    foreach ($logs as $id => $log) {
204      $logs[$id]->time = date($format, $log->time);
205    }
206    // URL for the "See all entries" link.
207    $all_entries_url = laguna_options_page_path('view-log');
208    // URL for the "Delete all logs" button.
209    $delete_logs_url = $all_entries_url . '&delete=1';
210    // Count total number of log entries in the database.
211    $total_entries_query = $wpdb->prepare("SELECT COUNT(`ID`) FROM {$wpdb->prefix}laguna_log", array());
212    $total_entries   = $wpdb->get_var($total_entries_query);
213    laguna_get_view('navbar.logs', array(
214      'logs' => $logs,
215      'delete_logs_url' => $delete_logs_url,
216      'total_entries' => $total_entries,
217      'all_entries_url' => $all_entries_url,
218    ));
219  }
220
221  /**
222   * Login errors override.
223   */
224  public function login_errors() {
225    $login_error_text = get_option(LAGUNA_SHORTNAME . 'login_error_text');
226    return __($login_error_text);
227  }
228
229  /**
230   * Remove WordPress version from the HTML output.
231   */
232  public function the_generator() {
233    return '';
234  }
235
236  /**
237   * Show flash messages in admin area.
238   */
239  public function admin_notices() {
240    // Check if there are any messages to be displayed.
241    if (isset($_SESSION['laguna_admin_messages']) && is_array($_SESSION['laguna_admin_messages'])) {
242      $messages_group = $_SESSION['laguna_admin_messages'];
243      foreach ($messages_group as $class => $messages) {
244        foreach ($messages as $message) {
245          laguna_get_view('admin.notice', array('class' => $class, 'message' => $message));
246        }
247      }
248      // Remove all messages from session.
249      unset($_SESSION['laguna_admin_messages']);
250    }
251  }
252
253}
254
255// Initiate the plugin.
256new Laguna();