timesheet source code
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

191 行
6.0KB

  1. <?php
  2. /**
  3. * Plugin Name: Acare Advanced Office
  4. * Plugin URI: http://biukop.com.au/acaresydney/timesheets
  5. * Description: Advanced Office system, timesheet, Payroll for AcareSydney
  6. * Version: 2.1
  7. * Author: Biukop Intelligence
  8. * Author URI: http://biukop.com.au/
  9. */
  10. namespace Biukop;
  11. require_once(dirname(__FILE__) . '/autoload.php');
  12. require_once (ABSPATH . 'wp-includes/pluggable.php');
  13. class AcareOffice{
  14. private $nonce; //for ajax verification
  15. private $pages = array('time-sheets', 'user-list');
  16. private $acaresydney_userid = 0;
  17. private $clientgroup="48646f3d-cf5e-4fea-8c8b-5812bd540e1b";
  18. private $xero ;
  19. private $cli = false;
  20. public function __construct() {
  21. add_action('init', array($this, 'class_loader'));
  22. add_action('wp', array($this, 'check_auth'));
  23. add_action('wp_enqueue_scripts', array($this, 'register_js_css'), 99);
  24. add_filter('show_admin_bar', '__return_false');
  25. add_shortcode( 'ts-sync-users', array($this, 'sync_users'));
  26. }
  27. /**
  28. * Autoload the custom theme classes
  29. */
  30. public function class_loader()
  31. {
  32. // Create a new instance of the autoloader
  33. $loader = new \Psr4AutoloaderClass();
  34. // Register this instance
  35. $loader->register();
  36. // Add our namespace and the folder it maps to
  37. $loader->addNamespace('\XeroPHP', dirname(__FILE__) . '/xero-php-master/src/XeroPHP');
  38. $loader->addNamespace('\Biukop', dirname(__FILE__) . '/' );
  39. $this->xero = new Xero();
  40. }
  41. //
  42. //
  43. ///check auth
  44. public function check_auth(){
  45. global $pagename;
  46. //echo $pagename;
  47. }
  48. ///
  49. // enqueue / register css /js
  50. //
  51. public function register_js_css() {
  52. $this->nonce = wp_create_nonce('acaresydney');
  53. $this->acaresydney_userid = get_query_var( 'acaresydney_userid' ) ;
  54. $this->register_bts_js();
  55. $this->register_timesheet_js_css();
  56. }
  57. private function register_bts_js()
  58. {
  59. wp_enqueue_style( 'bts', plugins_url('css/ts.css', __FILE__));
  60. wp_enqueue_script('bts', plugins_url('js/ts.js', __FILE__), array('jquery', 'jquery-ui-core'));
  61. wp_localize_script( 'bts', 'bts1', array(
  62. 'ajax_url' => admin_url( 'admin-ajax.php' ),
  63. 'nonce' => $this->nonce, // It is common practice to comma after
  64. 'display_name' => wp_get_current_user()->display_name,
  65. 'anonymous' => !is_user_logged_in(),
  66. 'me'=> get_current_user_id(),
  67. 'userid'=> $this->acaresydney_userid,
  68. ) );
  69. }
  70. private function register_timesheet_js_css(){
  71. global $pagename;
  72. if ($pagename != 'time-sheets'){
  73. return;
  74. }
  75. wp_enqueue_style( 'bts_ts', plugins_url('css/bts_timesheet.css', __FILE__));
  76. wp_enqueue_script( 'bts_ts', plugins_url('js/bts_timesheet.js', __FILE__), array( 'jquery' , 'bts' ));
  77. }
  78. public function sync_user_cli($args = array(), $assoc_args = array()){
  79. $this->cli = true;
  80. echo "running sync user from command line;";
  81. $this->sync_users();
  82. return;
  83. }
  84. //sync users to wordpress system
  85. public function sync_users(){
  86. set_time_limit(600); // executing time longer than expected 10 minutes
  87. //$this->sync_clients();
  88. $this->sync_employees();
  89. }
  90. private function sync_clients(){
  91. $contacts = $this->xero->getClients($this->clientgroup);
  92. foreach ($contacts as $c){
  93. $this->ensure_contact_exists($c);
  94. }
  95. }
  96. private function sync_employees(){
  97. $employees = $this->xero->getEmployees();
  98. foreach ( $employees as $e){
  99. if ($this->cli){
  100. echo "sync " . $e->getFirstName() ."\n";
  101. }
  102. $this->ensure_staff_exists($e);
  103. }
  104. }
  105. private function ensure_contact_exists($contact){
  106. $login = $contact->getContactID();
  107. $xero_contact = $this->xero->getContact($login);
  108. $args = $this->xero_contact_profile($xero_contact);
  109. $user = get_user_by('login', $login);
  110. if ($user === false){
  111. wp_insert_user($args);
  112. }else{//update user
  113. $args['ID'] = $user->ID;
  114. unset($args['user_pass']); //we don't change password
  115. wp_update_user($args);
  116. }
  117. }
  118. private function xero_contact_profile($c){
  119. $args = [
  120. 'user_login' => $username = $c->getContactID(),
  121. 'user_pass' => md5(uniqid(rand() + time(), true)),
  122. 'display_name' =>$c->getName(),
  123. 'user_email' => $c->getEmailAddress(),
  124. 'first_name' => $c->getFirstName(),
  125. 'last_name' => $c->getLastName(),
  126. 'nickname' => $c->getName(),
  127. 'role' => 'client',
  128. ];
  129. return $args;
  130. }
  131. private function ensure_staff_exists($employee)
  132. {
  133. $login = $employee->getEmployeeID();
  134. $user = get_user_by('login', $login);
  135. if ($user === false){
  136. $xero_employee = $this->xero->getEmployee($login);
  137. $args = $this->xero_employee_profile($xero_employee);
  138. wp_insert_user($args);
  139. }else{
  140. return;
  141. $args['ID'] = $user->ID;
  142. unset($args['user_pass']);
  143. wp_update_user($args);
  144. }
  145. }
  146. private function xero_employee_profile($e){
  147. $args = [
  148. 'user_login' => $username = $e->getEmployeeID(),
  149. 'user_pass' => md5(uniqid(rand() + time(), true)),
  150. 'display_name' =>$e->getFirstName() . " " . $e->getLastName(),
  151. 'user_email' => $e->getEmail(),
  152. 'first_name' => $e->getFirstName(),
  153. 'last_name' => $e->getLastName(),
  154. 'nickname' => $e->getFirstName(),
  155. 'role' => 'staff',
  156. ];
  157. return $args;
  158. }
  159. }
  160. $bb = new AcareOffice();
  161. if ( defined( 'WP_CLI' ) && WP_CLI ) {
  162. \WP_CLI::add_command( 'sync_users', array($bb, 'sync_user_cli'));
  163. }