timesheet source code
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

139 líneas
4.3KB

  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. public function __construct() {
  20. add_action('init', array($this, 'class_loader'));
  21. add_action('wp', array($this, 'check_auth'));
  22. add_action('wp_enqueue_scripts', array($this, 'register_js_css'), 99);
  23. add_filter('show_admin_bar', '__return_false');
  24. add_shortcode( 'ts-sync-users', array($this, 'sync_users'));
  25. }
  26. /**
  27. * Autoload the custom theme classes
  28. */
  29. public function class_loader()
  30. {
  31. // Create a new instance of the autoloader
  32. $loader = new \Psr4AutoloaderClass();
  33. // Register this instance
  34. $loader->register();
  35. // Add our namespace and the folder it maps to
  36. $loader->addNamespace('\XeroPHP', dirname(__FILE__) . '/xero-php-master/src/XeroPHP');
  37. $loader->addNamespace('\Biukop', dirname(__FILE__) . '/' );
  38. $this->xero = new Xero();
  39. }
  40. //
  41. //
  42. ///check auth
  43. public function check_auth(){
  44. global $pagename;
  45. //echo $pagename;
  46. }
  47. ///
  48. // enqueue / register css /js
  49. //
  50. public function register_js_css() {
  51. $this->nonce = wp_create_nonce('acaresydney');
  52. $this->acaresydney_userid = get_query_var( 'acaresydney_userid' ) ;
  53. $this->register_bts_js();
  54. $this->register_timesheet_js_css();
  55. }
  56. private function register_bts_js()
  57. {
  58. wp_enqueue_style( 'bts', plugins_url('css/ts.css', __FILE__));
  59. wp_enqueue_script('bts', plugins_url('js/ts.js', __FILE__), array('jquery', 'jquery-ui-core'));
  60. wp_localize_script( 'bts', 'bts1', array(
  61. 'ajax_url' => admin_url( 'admin-ajax.php' ),
  62. 'nonce' => $this->nonce, // It is common practice to comma after
  63. 'display_name' => wp_get_current_user()->display_name,
  64. 'anonymous' => !is_user_logged_in(),
  65. 'me'=> get_current_user_id(),
  66. 'userid'=> $this->acaresydney_userid,
  67. ) );
  68. }
  69. private function register_timesheet_js_css(){
  70. global $pagename;
  71. if ($pagename != 'time-sheets'){
  72. return;
  73. }
  74. wp_enqueue_style( 'bts_ts', plugins_url('css/bts_timesheet.css', __FILE__));
  75. wp_enqueue_script( 'bts_ts', plugins_url('js/bts_timesheet.js', __FILE__), array( 'jquery' , 'bts' ));
  76. }
  77. //sync users to wordpress system
  78. public function sync_users(){
  79. $this->sync_clients();
  80. $this->sync_employees();
  81. }
  82. private function sync_clients(){
  83. $contacts = $this->xero->getClients($this->clientgroup);
  84. foreach ($contacts as $c){
  85. $this->ensure_contact_exists($c);
  86. }
  87. }
  88. private function sync_employees(){
  89. }
  90. private function ensure_contact_exists($contact){
  91. $login = $contact->getContactID();
  92. $xero_contact = $this->xero->getContact($login);
  93. $args = $this->xero_contact_profile($xero_contact);
  94. $user = get_user_by('login', $login);
  95. if ($user === false){
  96. wp_insert_user($args);
  97. }else{//update user
  98. $args['ID'] = $user->ID;
  99. unset($args['user_pass']); //we don't change password
  100. wp_update_user($args);
  101. }
  102. }
  103. private function xero_contact_profile($c){
  104. $args = [
  105. 'user_login' => $username = $c->getContactID(),
  106. 'user_pass' => md5(uniqid(rand() + time(), true)),
  107. 'display_name' =>$c->getName(),
  108. 'user_email' => $c->getEmailAddress(),
  109. 'first_name' => $c->getFirstName(),
  110. 'last_name' => $c->getLastName(),
  111. 'nickname' => $c->getName(),
  112. 'role' => 'client',
  113. ];
  114. return $args;
  115. }
  116. }
  117. new AcareOffice();