PageRenderTime 305ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/cleverness-to-do-list-loader.class.php

https://github.com/cleverness/cleverness-to-do-list-plugin
PHP | 311 lines | 202 code | 32 blank | 77 comment | 18 complexity | 2c2957acba6c03771d86bf35046d2f19 MD5 | raw file
  1. <?php
  2. /**
  3. * Cleverness To-Do List Plugin Loader
  4. *
  5. * Loads the plugin
  6. * @author C.M. Kendrick <cindy@cleverness.org>
  7. * @package cleverness-to-do-list
  8. * @version 3.2.1
  9. */
  10. /**
  11. * Loader class
  12. * @package cleverness-to-do-list
  13. * @subpackage includes
  14. */
  15. class CTDL_Loader {
  16. public static $settings;
  17. public static $dashboard_settings;
  18. public static function init() {
  19. global $ClevernessToDoList, $CTDL_Frontend_Checklist, $CTDL_Frontend_Admin, $CTDL_templates, $CTDL_Dashboard_Widget;
  20. if ( is_admin() ) self::check_for_upgrade();
  21. $general_options = ( get_option( 'CTDL_general' ) ? get_option( 'CTDL_general' ) : array() );
  22. $advanced_options = ( get_option( 'CTDL_advanced' ) ? get_option( 'CTDL_advanced' ) : array() );
  23. $permissions_options = ( get_option( 'CTDL_permissions' ) ? get_option( 'CTDL_permissions' ) : array() );
  24. self::$settings = array_merge( $general_options, $advanced_options, $permissions_options );
  25. self::$dashboard_settings = get_option( 'CTDL_dashboard_settings' );
  26. self::include_files();
  27. if ( !post_type_exists( 'todo' ) ) self::setup_custom_post_type();
  28. if ( !taxonomy_exists( 'todocategories' ) ) self::create_taxonomies();
  29. self::call_wp_hooks();
  30. $ClevernessToDoList = new ClevernessToDoList();
  31. $CTDL_templates = new CTDL_Template_Loader;
  32. if ( is_admin() ) {
  33. new CTDL_Settings();
  34. $CTDL_Dashboard_Widget = new CTDL_Dashboard_Widget();
  35. }
  36. $CTDL_Frontend_Admin = new CTDL_Frontend_Admin;
  37. $CTDL_Frontend_Checklist = new CTDL_Frontend_Checklist;
  38. new CTDL_Frontend_List;
  39. }
  40. /**
  41. * Check to see if plugin has an upgrade
  42. * @static
  43. * @since 3.1
  44. */
  45. public static function check_for_upgrade() {
  46. global $wp_version;
  47. $exit_msg = esc_html__( 'To-Do List requires WordPress 3.8 or newer. <a href="http://codex.wordpress.org/Upgrading_WordPress">Please update.</a>', 'cleverness-to-do-list' );
  48. if ( version_compare( $wp_version, '3.8', '<' ) ) {
  49. exit( $exit_msg );
  50. }
  51. cleverness_todo_activation();
  52. }
  53. /**
  54. * Set up custom post types for to-do items
  55. * @static
  56. * @since 3.0
  57. */
  58. public static function setup_custom_post_type() {
  59. register_post_type( 'todo',
  60. array(
  61. 'labels' => array(
  62. 'name' => __( 'To-Do', 'cleverness-to-do-list' ),
  63. 'singular_name' => __( 'To-Do', 'cleverness-to-do-list' )
  64. ),
  65. 'public' => false,
  66. 'publicly_queryable' => false,
  67. 'exclude_from_search' => true,
  68. 'show_ui' => false,
  69. 'hierarchical' => false,
  70. 'has_archive' => false,
  71. 'rewrite' => false,
  72. 'query_var' => false,
  73. 'can_export' => true,
  74. 'show_in_nav_menus' => false,
  75. )
  76. );
  77. }
  78. /**
  79. * Setup categories
  80. * @static
  81. * @since 3.0
  82. */
  83. public static function create_taxonomies() {
  84. $labels = array(
  85. 'name' => _x( 'Categories', 'taxonomy general name', 'cleverness-to-do-list' ),
  86. 'singular_name' => _x( 'Category', 'taxonomy singular name', 'cleverness-to-do-list' ),
  87. );
  88. register_taxonomy( 'todocategories', array( 'todo' ), array(
  89. 'hierarchical' => true,
  90. 'labels' => $labels,
  91. 'show_ui' => false,
  92. 'query_var' => false,
  93. 'rewrite' => false,
  94. ) );
  95. }
  96. /**
  97. * Calls the plugin files for inclusion
  98. * @static
  99. */
  100. private static function include_files() {
  101. include_once CTDL_PLUGIN_DIR.'includes/cleverness-to-do-list-library.class.php';
  102. include_once CTDL_PLUGIN_DIR.'includes/cleverness-to-do-list.class.php';
  103. if ( !class_exists( 'Gamajo_Template_Loader' ) ) {
  104. include_once CTDL_PLUGIN_DIR . 'includes/cleverness-to-do-list-template.class.php';
  105. }
  106. include_once CTDL_PLUGIN_DIR . 'includes/cleverness-to-do-list-template-loader.class.php';
  107. include_once CTDL_PLUGIN_DIR . 'includes/cleverness-to-do-list-template-functions.class.php';
  108. if ( self::$settings['categories'] == 1 ) include_once CTDL_PLUGIN_DIR.'includes/cleverness-to-do-list-categories.class.php';
  109. if ( is_admin() ) {
  110. include_once CTDL_PLUGIN_DIR.'includes/cleverness-to-do-list-settings.class.php';
  111. include_once CTDL_PLUGIN_DIR.'includes/cleverness-to-do-list-help.class.php';
  112. include_once CTDL_PLUGIN_DIR.'includes/cleverness-to-do-list-dashboard-widget.class.php';
  113. }
  114. include_once CTDL_PLUGIN_DIR.'includes/cleverness-to-do-list-frontend.class.php';
  115. }
  116. /**
  117. * Adds actions to WordPress hooks
  118. * @static
  119. */
  120. private static function call_wp_hooks() {
  121. if ( self::$settings['admin_bar'] == 1 ) add_action( 'admin_bar_menu', array( 'CTDL_Lib', 'add_to_toolbar' ), 999 );
  122. if ( is_admin() ) {
  123. add_action( 'admin_init', array( __CLASS__, 'admin_init' ) );
  124. add_action( 'admin_menu', array( __CLASS__, 'create_admin_menu' ) );
  125. add_filter( 'plugin_action_links', array( 'CTDL_Lib', 'add_settings_link' ), 10, 2 );
  126. if ( self::$settings['categories'] == 1 ) add_action( 'admin_init', array( 'CTDL_Categories', 'initialize_categories' ) );
  127. }
  128. add_action( 'split_shared_term', array( 'CTDL_Lib', 'split_shared_term' ), 10, 4 );
  129. add_action( 'wp_ajax_cleverness_add_todo', array( 'CTDL_Lib', 'add_todo_callback' ) );
  130. add_action( 'wp_ajax_cleverness_delete_todo', array( 'CTDL_Lib', 'delete_todo_callback' ) );
  131. add_action( 'wp_ajax_cleverness_todo_complete', array( 'CTDL_Lib', 'complete_todo_callback' ) );
  132. add_action( 'wp_ajax_cleverness_frontend_display_todos', array( 'CTDL_Lib', 'frontend_display_todos_callback' ) );
  133. }
  134. /**
  135. * Adds the Main plugin page and the categories page to the WordPress backend menu
  136. * Also adds the Help tab to those pages
  137. * @static
  138. */
  139. public static function create_admin_menu() {
  140. global $cleverness_todo_page, $cleverness_todo_cat_page;
  141. $cleverness_todo_page = add_menu_page( apply_filters( 'ctdl_todo_list', __( 'To-Do List', 'cleverness-to-do-list' ) ), apply_filters( 'ctdl_todo_list', __( 'To-Do List', 'cleverness-to-do-list' ) ),
  142. self::$settings['view_capability'], 'cleverness-to-do-list', array( __CLASS__, 'plugin_page' ), apply_filters( 'ctdl_icon', 'dashicons-yes' ) );
  143. if ( self::$settings['categories'] == 1 ) {
  144. $cleverness_todo_cat_page = add_submenu_page( 'cleverness-to-do-list', apply_filters( 'ctdl_categories_title', __( 'To-Do List Categories', 'cleverness-to-do-list' ) ),
  145. apply_filters( 'ctdl_categories', __( 'Categories', 'cleverness-to-do-list' ) ),
  146. self::$settings['add_cat_capability'], 'cleverness-to-do-list-cats', array( 'CTDL_Categories', 'create_category_page' ) );
  147. add_action( "load-$cleverness_todo_cat_page", array( 'CTDL_Help', 'cleverness_todo_help_tab' ) );
  148. }
  149. add_action( "load-$cleverness_todo_page", array( 'CTDL_Help', 'cleverness_todo_help_tab' ) );
  150. }
  151. /**
  152. * Displays the Main To-Do List page
  153. * @static
  154. */
  155. public static function plugin_page() {
  156. global $ClevernessToDoList;
  157. $ClevernessToDoList->display();
  158. echo $ClevernessToDoList->list;
  159. }
  160. /**
  161. * Adds the CSS and JS to the backend pages
  162. * @static
  163. */
  164. public static function admin_init() {
  165. global $cleverness_todo_page, $cleverness_todo_settings_page, $cleverness_todo_cat_page;
  166. add_action( 'admin_print_styles-'.$cleverness_todo_page, array( __CLASS__, 'add_admin_css' ) );
  167. add_action( 'admin_print_scripts-'.$cleverness_todo_page, array( __CLASS__, 'add_admin_js' ) );
  168. add_action( 'admin_print_styles-'.$cleverness_todo_cat_page, array( __CLASS__, 'add_admin_css' ) );
  169. add_action( 'admin_print_scripts-'.$cleverness_todo_cat_page, array( __CLASS__, 'add_admin_js' ) );
  170. add_action( 'admin_print_scripts-'.$cleverness_todo_settings_page, array( __CLASS__, 'add_admin_js' ) );
  171. }
  172. /**
  173. * Loads the CSS file for the WP backend
  174. * @static
  175. */
  176. public static function add_admin_css() {
  177. $cleverness_style_url = CTDL_PLUGIN_URL.'/css/cleverness-to-do-list-admin.css';
  178. $cleverness_style_file = CTDL_PLUGIN_DIR.'/css/cleverness-to-do-list-admin.css';
  179. if ( file_exists( $cleverness_style_file ) ) {
  180. wp_register_style( 'cleverness_todo_style_sheet', $cleverness_style_url, array(), CTDL_PLUGIN_VERSION );
  181. wp_enqueue_style( 'cleverness_todo_style_sheet' );
  182. wp_enqueue_style( 'jquery.ui.theme', CTDL_PLUGIN_URL.'/css/jquery-ui-fresh.css', array(), CTDL_PLUGIN_VERSION );
  183. wp_enqueue_style( 'cleverness_todo_select_css', CTDL_PLUGIN_URL.'/css/cleverness-to-do-list-select2.css', array(), CTDL_PLUGIN_VERSION );
  184. }
  185. }
  186. /**
  187. * Loads and localizes JS files for the WP backend
  188. * @static
  189. */
  190. public static function add_admin_js() {
  191. wp_register_script( 'cleverness_todo_js', CTDL_PLUGIN_URL.'/js/cleverness-to-do-list-admin.js', '', CTDL_PLUGIN_VERSION, true );
  192. wp_register_script( 'cleverness_metadata_js', CTDL_PLUGIN_URL.'/js/jquery.metadata.js', '', CTDL_PLUGIN_VERSION, true );
  193. wp_register_script( 'cleverness_tablesorter_js', CTDL_PLUGIN_URL.'/js/jquery.tablesorter.min.js', '', CTDL_PLUGIN_VERSION, true );
  194. wp_register_script( 'cleverness_todo_select2_js', CTDL_PLUGIN_URL.'/js/select2.min.js', '', CTDL_PLUGIN_VERSION, true );
  195. wp_enqueue_script( 'cleverness_todo_js' );
  196. wp_enqueue_script( 'cleverness_metadata_js' );
  197. wp_enqueue_script( 'cleverness_tablesorter_js' );
  198. wp_enqueue_script( 'jquery-color' );
  199. wp_enqueue_script( 'jquery-ui-datepicker' );
  200. wp_enqueue_script( 'jquery-ui-core' );
  201. wp_enqueue_script( 'jquery-ui-slider' );
  202. wp_enqueue_script( 'jquery-ui-widget' );
  203. wp_enqueue_script( 'jquery-ui-mouse' );
  204. wp_enqueue_script( 'jquery-ui-resizable' );
  205. wp_enqueue_script( 'cleverness_todo_select2_js' );
  206. wp_localize_script( 'cleverness_todo_js', 'ctdl', CTDL_Loader::get_js_vars() );
  207. }
  208. /**
  209. * Localize JS variables
  210. * @static
  211. * @return array
  212. */
  213. public static function get_js_vars() {
  214. global $CTDL_Frontend_Admin;
  215. $array = array(
  216. 'INSERT_MSG' => __( 'New To-Do Added', 'cleverness-to-do-list' ),
  217. 'SUCCESS_MSG' => __( 'To-Do Deleted.', 'cleverness-to-do-list' ),
  218. 'ERROR_MSG' => __( 'There was a problem performing that action.', 'cleverness-to-do-list' ),
  219. 'PERMISSION_MSG' => __( 'You do not have sufficient privileges to do that.', 'cleverness-to-do-list' ),
  220. 'CONFIRMATION_MSG' => __( "You are about to permanently delete the selected item. \n 'Cancel' to stop, 'OK' to delete.", 'cleverness-to-do-list' ),
  221. 'CONFIRMATION_ALL_MSG' => __( "You are about to permanently delete all completed items. \n 'Cancel' to stop, 'OK' to delete.", 'cleverness-to-do-list' ),
  222. 'CONFIRMATION_DELETE_ALL_MSG' => __( "You are about to permanently delete all to-do items. \n 'Cancel' to stop, 'OK' to delete.", 'cleverness-to-do-list' ),
  223. 'CONFIRMATION_DEL_TABLES_MSG' => __( "You are about to permanently delete database tables. This cannot be undone. \n 'Cancel' to stop, 'OK' to delete.", 'cleverness-to-do-list' ),
  224. 'SELECT_USER' => __( 'Select a User', 'cleverness-to-do-list' ),
  225. 'NONCE' => wp_create_nonce( 'ctdl-todo' ),
  226. 'AJAX_URL' => admin_url( 'admin-ajax.php' ),
  227. );
  228. if ( isset( $CTDL_Frontend_Admin->atts ) ) {
  229. $array['TODOADMIN_ATTS'] = $CTDL_Frontend_Admin->atts;
  230. }
  231. return $array;
  232. }
  233. /**
  234. * Register the scripts for the To-Do List admin frontend
  235. */
  236. public static function frontend_admin_register_scripts() {
  237. wp_register_script( 'cleverness_metadata_js', CTDL_PLUGIN_URL . '/js/jquery.metadata.js', '', CTDL_PLUGIN_VERSION, true );
  238. wp_register_script( 'cleverness_tablesorter_js', CTDL_PLUGIN_URL . '/js/jquery.tablesorter.min.js', '', CTDL_PLUGIN_VERSION, true );
  239. wp_register_script( 'cleverness_todo_select2_js', CTDL_PLUGIN_URL . '/js/select2.min.js', '', CTDL_PLUGIN_VERSION, true );
  240. wp_register_script( 'cleverness_todo_frontend_admin_js', CTDL_PLUGIN_URL . '/js/cleverness-to-do-list-frontend-admin.js', array( 'jquery' ), CTDL_PLUGIN_VERSION, true );
  241. }
  242. /**
  243. * Enqueue the scripts for the To-Do List admin frontend
  244. */
  245. public static function frontend_admin_enqueue_scripts() {
  246. wp_enqueue_script( 'jquery' );
  247. wp_enqueue_script( 'jquery-ui-datepicker' );
  248. wp_enqueue_script( 'jquery-color' );
  249. wp_enqueue_script( 'jquery-ui-slider' );
  250. wp_enqueue_script( 'jquery-ui-widget' );
  251. wp_enqueue_script( 'jquery-ui-mouse' );
  252. wp_enqueue_script( 'cleverness_metadata_js' );
  253. wp_enqueue_script( 'cleverness_tablesorter_js' );
  254. wp_enqueue_script( 'cleverness_todo_select2_js' );
  255. wp_enqueue_script( 'cleverness_todo_frontend_admin_js' );
  256. wp_localize_script( 'cleverness_todo_frontend_admin_js', 'ctdl', CTDL_Loader::get_js_vars() );
  257. }
  258. /**
  259. * Register the scripts for the To-Do List frontend checklist
  260. */
  261. public static function frontend_checklist_register_scripts() {
  262. wp_register_script( 'cleverness_todo_checklist_complete_js', CTDL_PLUGIN_URL . '/js/cleverness-to-do-list-frontend.js', array( 'jquery' ), CTDL_PLUGIN_VERSION, true );
  263. }
  264. /**
  265. * Enqueue and Localize JavaScript for the To-Do List frontend checklist
  266. */
  267. public static function frontend_checklist_enqueue_scripts() {
  268. wp_enqueue_script( 'jquery' );
  269. wp_enqueue_script( 'cleverness_todo_checklist_complete_js' );
  270. wp_localize_script( 'cleverness_todo_checklist_complete_js', 'ctdl', CTDL_Loader::get_js_vars() );
  271. }
  272. /**
  273. * Register and enqueue the css for the frontend
  274. */
  275. public static function frontend_css() {
  276. wp_enqueue_style( 'cleverness_todo_list_frontend', CTDL_PLUGIN_URL . '/css/cleverness-to-do-list-frontend.css', array(), CTDL_PLUGIN_VERSION );
  277. wp_enqueue_style( 'jquery.ui.theme', CTDL_PLUGIN_URL . '/css/jquery-ui-fresh.css', array(), CTDL_PLUGIN_VERSION );
  278. }
  279. }