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