/wp-content/plugins/activetables/admin/class-activetables-admin.php

https://gitlab.com/ogar.vasily/activetables · PHP · 203 lines · 122 code · 30 blank · 51 comment · 10 complexity · 2322a15c15bea663902a599506e8792f MD5 · raw file

  1. <?php
  2. /**
  3. * The admin-specific functionality of the plugin.
  4. *
  5. * @link http://somecompany.com
  6. * @since 1.0.0
  7. *
  8. * @package Activetables
  9. * @subpackage Activetables/admin
  10. */
  11. /**
  12. * The admin-specific functionality of the plugin.
  13. *
  14. * Defines the plugin name, version, and two examples hooks for how to
  15. * enqueue the admin-specific stylesheet and JavaScript.
  16. *
  17. * @package Activetables
  18. * @subpackage Activetables/admin
  19. * @author Vasily Ogar <ogar.vasily@gmail.com>
  20. */
  21. class Activetables_Admin
  22. {
  23. private $translation_array;
  24. function __construct()
  25. {
  26. $this->translation_array = array(
  27. 'media_upload_page_title' => __('Choose file', 'activetables'),
  28. 'media_upload_btn_title' => __('Choose file', 'activetables'),
  29. 'media_upload_alert' => __('Please select a valid file format CSV, XML, XLS, XLSX or JSON', 'activetables'),
  30. );
  31. }
  32. public static function get_table_data($id)
  33. {
  34. global $wpdb;
  35. do_action('activetables_action_before_get_table_data', $id);
  36. $query = $wpdb->prepare("SELECT * FROM " . WPAT_TBL_SETS . " WHERE id=%d", $id);
  37. $data = $wpdb->get_row($query);
  38. $data = apply_filters('activetables_filter_table_data', $data, $id);
  39. return $data;
  40. }
  41. /**
  42. * Register the stylesheets for the admin area.
  43. *
  44. * @since 1.0.0
  45. */
  46. public function enqueue_styles()
  47. {
  48. wp_enqueue_style(WPAT_PLUGIN_NAME . '-bootstrap', WPAT_PLUGIN_URL . 'libs/bootstrap/bootstrap.min.css', array(), WPAT_PLUGIN_VERSION, 'all');
  49. wp_enqueue_style(WPAT_PLUGIN_NAME . '-font-awesome', WPAT_PLUGIN_URL . 'admin/css/font-awesome.min.css', array(), WPAT_PLUGIN_VERSION, 'all');
  50. wp_enqueue_style(WPAT_PLUGIN_NAME, WPAT_PLUGIN_URL . 'admin/css/activetables-admin.css', array(), WPAT_PLUGIN_VERSION, 'all');
  51. }
  52. /**
  53. * Register the JavaScript for the admin area.
  54. *
  55. * @since 1.0.0
  56. */
  57. public function enqueue_scripts()
  58. {
  59. wp_enqueue_script(WPAT_PLUGIN_NAME . '-angular', WPAT_PLUGIN_URL . 'libs/angular/angular.min.js', array('jquery'), WPAT_PLUGIN_VERSION, true);
  60. // wp_enqueue_script(WPAT_PLUGIN_NAME . '-bootstrap', WPAT_PLUGIN_URL . 'libs/bootstrap/bootstrap.min.js', array('jquery'), WPAT_PLUGIN_VERSION, true);
  61. wp_enqueue_script(WPAT_PLUGIN_NAME . '-admin', WPAT_PLUGIN_URL . 'admin/js/activetables-admin.js', array('activetables-angular'), WPAT_PLUGIN_VERSION, true);
  62. wp_localize_script(WPAT_PLUGIN_NAME . '-admin', 'admin', $this->translation_array);
  63. }
  64. /**
  65. * Generate menu items
  66. */
  67. public function admin_menu()
  68. {
  69. add_menu_page('ActiveTables', 'ActiveTables', 'manage_options', 'activetables', array($this, 'all_tables_page'), 'none');
  70. add_submenu_page('activetables', 'Add a new table', 'Add new table', 'manage_options', 'activetables-add-table', array($this, 'add_table_page'));
  71. add_submenu_page('activetables', 'ActiveTables settings', 'Settings', 'manage_options', 'activetables-settings', array($this, 'settings_page'));
  72. }
  73. /**
  74. * Output all existing tables
  75. */
  76. function all_tables_page()
  77. {
  78. global $wpdb;
  79. if (!current_user_can('manage_options')) {
  80. wp_die(__('You do not have sufficient permissions to access this page.', 'activetables'));
  81. }
  82. $action = isset($_GET['action']) ? filter_var($_GET['action'], FILTER_SANITIZE_STRING) : '';
  83. if ($action == 'edit') {
  84. ob_start();
  85. include_once(WPAT_PLUGIN_DIR . 'admin/partials/edit-table.tpl.php');
  86. $page_content = ob_get_contents();
  87. ob_end_clean();
  88. $edit_table = apply_filters('activetables_filter_edit_table', $page_content);
  89. echo $edit_table;
  90. } else {
  91. if ($action == 'delete') {
  92. $table_id = $_GET['table_id'];
  93. if (!is_array($table_id)) {
  94. $wpdb->delete(WPAT_TBL_SETS, array('ID' => $table_id), array('%d'));
  95. $wpdb->delete(WPAT_TBL_COLS, array('table_id' => $table_id), array('%d'));
  96. } else {
  97. foreach ($table_id as $id) {
  98. $wpdb->delete(WPAT_TBL_SETS, array('ID' => $id), array('%d'));
  99. $wpdb->delete(WPAT_TBL_COLS, array('table_id' => $id), array('%d'));
  100. }
  101. }
  102. }
  103. ob_start();
  104. include_once(WPAT_PLUGIN_DIR . 'admin/partials/all-tables.tpl.php');
  105. $page_content = ob_get_contents();
  106. ob_end_clean();
  107. $all_tables = apply_filters('activetables_filter_all_tables', $page_content);
  108. echo $all_tables;
  109. }
  110. do_action('activetables_action_all_tables');
  111. }
  112. /**
  113. * Add new table
  114. */
  115. public function add_table_page()
  116. {
  117. if (!current_user_can('manage_options')) {
  118. wp_die(__('You do not have sufficient permissions to access this page.', 'activetables'));
  119. }
  120. ob_start();
  121. include_once(WPAT_PLUGIN_DIR . 'admin/partials/edit-table.tpl.php');
  122. $page_content = ob_get_contents();
  123. ob_end_clean();
  124. $add_table = apply_filters('activetables_filter_add_table', $page_content);
  125. echo $add_table;
  126. do_action('activetables_action_add_table');
  127. }
  128. /**
  129. * Settings page
  130. */
  131. public function settings_page()
  132. {
  133. if (!current_user_can('manage_options')) {
  134. wp_die(__('You do not have sufficient permissions to access this page.', 'activetables'));
  135. }
  136. ob_start();
  137. include_once(WPAT_PLUGIN_DIR . 'admin/partials/settings.tpl.php');
  138. $page_content = ob_get_contents();
  139. ob_end_clean();
  140. $settings_page = apply_filters('activetables_filter_settings_page', $page_content);
  141. echo $settings_page;
  142. do_action('activetables_action_settings_page');
  143. }
  144. /**
  145. * Add option to Screen options menu
  146. */
  147. public function add_options()
  148. {
  149. $option = 'per_page';
  150. $args = array(
  151. 'label' => 'Tables per page',
  152. 'default' => 10,
  153. 'option' => 'activetables_per_page'
  154. );
  155. add_screen_option($option, $args);
  156. }
  157. /**
  158. * "Screen Options" slide-in where the user can adjust the columns to be shown and the number of rows to be displayed
  159. */
  160. public function set_option($status, $option, $value)
  161. {
  162. return $value;
  163. }
  164. /**
  165. * Extend wordpress mime types
  166. */
  167. public function extend_mime_types($mime_types)
  168. {
  169. $mime_types['xml'] = 'application/xml';
  170. $mime_types['json'] = 'application/json';
  171. return $mime_types;
  172. }
  173. }