Guest User

Untitled

a guest
Nov 19th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.08 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Code It Wise Test
  4. Plugin URI:
  5. description: This is my test plugin for a certain website.
  6. Version: 0.1
  7. Author: Me
  8. Author URI:
  9. License: GPL2
  10. */
  11.  
  12. require(__DIR__ . 'codeitwise-admin-page.php');
  13. class CodeItWise {
  14.  
  15. function __construct() {
  16. add_action( 'admin_menu', array( 'CodeItWise', 'codeitwise__adminMenuActions' ) );
  17. }
  18.  
  19. public static function codeitwise__adminMenuActions() {
  20. add_menu_page( 'Code It Wise - Codeable Test', 'Code It Wise', 'manage_options', 'ciw-admin-page.php', 'CodeItWise__Admin::renderAdminPage', 'dashicons-admin-generic', 6 );
  21. }
  22. }
  23.  
  24. $ciw = new CodeItWise();
  25.  
  26. ?>
  27.  
  28. <?php
  29. class CodeItWise__Admin {
  30.  
  31. function __construct() {
  32. add_action( 'admin_enqueue_scripts', 'CodeItWise__Admin::renderScripts' );
  33. add_action( 'wp_ajax_get_users', 'CodeItWise__Admin::getUsers' );
  34. }
  35.  
  36. public static function renderAdminPage ()
  37. {?>
  38. <div class="wrap">
  39. <h2>Code It Wise - Codeable Test</h2>
  40. </div>
  41. <?php
  42. global $wp_roles;
  43. $all_roles = $wp_roles->get_names();
  44. ?>
  45. <div class="tablenav top">
  46. <div class="alignleft">
  47. <label class="screen-reader-text">Filter role</label>
  48. <select name="role" js-filter-role>
  49. <option value="">Roles</option>
  50. <?php
  51. foreach($all_roles as $role)
  52. {
  53. echo '<option value="' . $role. '">' . $role . '</option>';
  54. }
  55. ?>
  56. </select>
  57. </div>
  58. </div>
  59. <div class="wrap" js-users-container>
  60. <!-- the table will be loaded in here -->
  61. </div>
  62. <div class="wrap" js-pagination-container>
  63.  
  64. </div>
  65. <?php
  66. include(__DIR__ . "handlebar-templates.html");
  67. }
  68.  
  69. public static function renderScripts() {
  70. wp_enqueue_script('codeitwise__handlebars', plugin_dir_url(__FILE__) . '/scripts/handlebars-v4.0.10.min.js');
  71. wp_enqueue_script('codeitwise__core', plugin_dir_url(__FILE__) . '/scripts/codeitwise.js');
  72. }
  73.  
  74. public static function getUsers() {
  75.  
  76. $role = $_POST['role'];
  77. $page = $_POST['page'];
  78. $orderby = $_POST['orderby'];
  79. $order = $_POST['order'];
  80.  
  81. if(!isset($page)) {
  82. $page = 1;
  83. }
  84.  
  85. $page_size = 10;
  86. $columns = array(
  87. 'display_name',
  88. 'user_login'
  89. );
  90. $meta_key = array(
  91.  
  92. );
  93.  
  94. $args = array(
  95. 'role' => $role,
  96. 'offset' => (($page - 1) * $page_size),
  97. 'number' => $page_size,
  98. 'fields' => $columns,
  99. 'orderby' => $orderby,
  100. 'order' => $order
  101. );
  102.  
  103. $counted = count_users();
  104. $count = ($role === "" ? $counted["total_users"] : $counted["avail_roles"][strtolower($role)]);
  105. if($count === null) $count = 0;
  106. $response= array(
  107. 'users' => get_users($args),
  108. 'total_count' => $count,
  109. 'role' => $role,
  110. 'page' => $page,
  111. 'page_size' => $page_size,
  112. 'orderby' => $orderby,
  113. 'order' => $order
  114. );
  115. wp_send_json_success( $response );
  116.  
  117. }
  118. }
  119.  
  120. $ciw_admin = new CodeItWise__Admin();
  121. ?>
  122.  
  123. (function () {
  124. const captains = console;
  125. const $ = jQuery;
  126. const defaultOrder = "display_name";
  127.  
  128. let role = "";
  129. let column = defaultOrder;
  130. let direction = "DESC";
  131.  
  132. Handlebars.registerHelper('if_even', function (index) {
  133. if ((index % 2) == 0) {
  134. return "";
  135. } else {
  136. return new Handlebars.SafeString("class='alternate'");
  137. }
  138. });
  139. Handlebars.registerHelper("inc", function (value, by, options) {
  140. return parseInt(value) + by;
  141. });
  142. Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {
  143.  
  144. switch (operator) {
  145. case '==':
  146. return (v1 == v2) ? options.fn(this) : options.inverse(this);
  147. case '===':
  148. return (v1 === v2) ? options.fn(this) : options.inverse(this);
  149. case '!=':
  150. return (v1 != v2) ? options.fn(this) : options.inverse(this);
  151. case '!==':
  152. return (v1 !== v2) ? options.fn(this) : options.inverse(this);
  153. case '<':
  154. return (v1 < v2) ? options.fn(this) : options.inverse(this);
  155. case '<=':
  156. return (v1 <= v2) ? options.fn(this) : options.inverse(this);
  157. case '>':
  158. return (v1 > v2) ? options.fn(this) : options.inverse(this);
  159. case '>=':
  160. return (v1 >= v2) ? options.fn(this) : options.inverse(this);
  161. case '&&':
  162. return (v1 && v2) ? options.fn(this) : options.inverse(this);
  163. case '||':
  164. return (v1 || v2) ? options.fn(this) : options.inverse(this);
  165. default:
  166. return options.inverse(this);
  167. }
  168. });
  169. Handlebars.registerHelper('colOrder', function (orderby, order, column, options) {
  170. if(orderby === column) {
  171. if(order == "desc")
  172. {
  173. return "asc";
  174. }
  175. }
  176. return "desc";
  177. });
  178.  
  179. $(function () {
  180.  
  181. BuildTable(role, 1, defaultOrder, "ASC");
  182. $("[js-filter-role]").on("change", function () {
  183. role = $(this).val();
  184. BuildTable(role, 1, defaultOrder, "ASC");
  185. });
  186.  
  187. $('body').on('click', '[data-trigger-page]', function (e) {
  188. e.preventDefault();
  189. BuildTable(role, $(this).data("trigger-page"), column, direction);
  190. });
  191.  
  192. $('body').on('click', '[js-sortable]', function(e) {
  193. e.preventDefault();
  194.  
  195. column = $(this).data("column");
  196. direction = $(this).data("direction");
  197. BuildTable(role, 1, column, direction);
  198.  
  199. });
  200.  
  201. });
  202.  
  203. function BuildTable(role, page, orderby, order) {
  204. let data = { action: 'get_users', page, role, orderby, order };
  205. let url = '/wp-admin/admin-ajax.php';
  206.  
  207. $.post(url, data, 'json')
  208. .done(function (response) {
  209. let source = $("[js-users]").html();
  210. let template = Handlebars.compile(source);
  211. let html = template({ users: response.data.users, order: response.data.order, orderby: response.data.orderby });
  212. let container = $("[js-users-container]");
  213.  
  214. container.empty();
  215. container.append(html);
  216. if(response.data.total_count > 0)
  217. {
  218. source = $("[js-pagination]").html();
  219. template = Handlebars.compile(source);
  220. html = template({ total_count: response.data.total_count, current_page: page, page_count: Math.ceil(response.data.total_count / response.data.page_size) });
  221. container = $("[js-pagination-container]");
  222. container.empty();
  223. container.append(html);
  224. }
  225. else {
  226. $("[js-pagination-container]").empty();
  227. }
  228.  
  229. })
  230. .fail(function (xhr, status, error) {
  231. // error handling
  232. alert(error);
  233. });
  234. }
  235.  
  236.  
  237. })();
  238.  
  239. <script type="text/x-handlebars-template" js-users>
  240.  
  241. <table class="wp-list-table widefat fixed" cellspacing="0">
  242. <thead>
  243. <tr>
  244. <th class="manage-column sortable {{colOrder orderby order 'display_name'}}" scope="col" js-sortable data-column="display_name" data-direction="{{colOrder orderby order 'display_name'}}">
  245. <a href="#">
  246. <span>Name</span>
  247. <span class="sorting-indicator"></span>
  248. </a>
  249. </th>
  250. <th class="manage-column sortable {{colOrder orderby order 'username'}}" scope="col" js-sortable data-column="username" data-direction="{{colOrder orderby order 'username'}}">
  251. <a href="#">
  252. <span>Username</span>
  253. <span class="sorting-indicator"></span>
  254. </a>
  255. </th>
  256. </tr>
  257. </thead>
  258. {{#each users }}
  259. <tr {{if_even @index}}>
  260. <td>
  261. {{display_name}}
  262. </td>
  263. <td>
  264. {{user_login}}
  265. </td>
  266. </tr>
  267. {{else}}
  268. <tr>
  269. <td colspan="2">
  270. No users to display.
  271. </td>
  272. </tr>
  273. {{/each}}
  274. </table>
  275. </script>
  276.  
  277. <script type="text/x-handlebars-template" js-pagination>
  278. <div class="tablenav top">
  279. <div class="tablenav-pages">
  280. <span class="displaying-num">{{total_count}} items</span>
  281. <span class="pagination-links">
  282. {{#ifCond current_page '===' 1}}
  283. <span class="tablenav-pages-navspan" aria-hidden="true">«</span>
  284. <span class="tablenav-pages-navspan" aria-hidden="true">‹</span>
  285. {{/ifCond}}
  286. {{#ifCond current_page '>' 1}}
  287. <a class="prev-page" data-trigger-page="1" href="#"><span class="screen-reader-text">Next page</span><span aria-hidden="true">«</span></a>
  288. <a class="first-page" data-trigger-page="{{inc current_page -1}}" href="#"><span class="screen-reader-text">Last page</span><span aria-hidden="true">‹</span></a>
  289. {{/ifCond}}
  290.  
  291. <span class="current-page">{{current_page}}</span> of <span class="page-count">{{page_count}}</span>
  292.  
  293. {{#ifCond current_page '==' page_count}}
  294. <span class="tablenav-pages-navspan" aria-hidden="true">›</span>
  295. <span class="tablenav-pages-navspan" aria-hidden="true">»</span>
  296. {{/ifCond}}
  297. {{#ifCond current_page '<' page_count}}
  298. <a class="next-page" data-trigger-page="{{inc current_page 1}}" href="#"><span class="screen-reader-text">Next page</span><span aria-hidden="true">›</span></a>
  299. <a class="last-page" data-trigger-page="{{page_count}}" href="#"><span class="screen-reader-text">Last page</span><span aria-hidden="true">»</span></a>
  300. {{/ifCond}}
  301. </span>
  302. </div>
  303. </div>
  304. </script>
Add Comment
Please, Sign In to add comment