|
- <?php
- /**
- * Plugin Name: Acare Advanced Office
- * Plugin URI: http://biukop.com.au/acaresydney/timesheets
- * Description: Advanced Office system, timesheet, Payroll for AcareSydney
- * Version: 2.1
- * Author: Biukop Intelligence
- * Author URI: http://biukop.com.au/
- */
-
- namespace Biukop;
- require_once(dirname(__FILE__) . '/autoload.php');
- require_once (ABSPATH . 'wp-includes/pluggable.php');
-
-
- class AcareOffice{
-
- private $nonce; //for ajax verification
- private $pages = array('time-sheets', 'user-list');
- private $acaresydney_userid = 0;
- private $clientgroup="48646f3d-cf5e-4fea-8c8b-5812bd540e1b";
- private $xero ;
-
- public function __construct() {
- add_action('init', array($this, 'class_loader'));
- add_action('wp', array($this, 'check_auth'));
- add_action('wp_enqueue_scripts', array($this, 'register_js_css'), 99);
-
- add_filter('show_admin_bar', '__return_false');
-
- add_shortcode( 'ts-sync-users', array($this, 'sync_users'));
- }
-
- /**
- * Autoload the custom theme classes
- */
- public function class_loader()
- {
- // Create a new instance of the autoloader
- $loader = new \Psr4AutoloaderClass();
-
- // Register this instance
- $loader->register();
-
- // Add our namespace and the folder it maps to
- $loader->addNamespace('\XeroPHP', dirname(__FILE__) . '/xero-php-master/src/XeroPHP');
- $loader->addNamespace('\Biukop', dirname(__FILE__) . '/' );
-
- $this->xero = new Xero();
- }
-
-
-
- //
- //
- ///check auth
- public function check_auth(){
- global $pagename;
- //echo $pagename;
- }
-
- ///
- // enqueue / register css /js
- //
- public function register_js_css() {
- $this->nonce = wp_create_nonce('acaresydney');
- $this->acaresydney_userid = get_query_var( 'acaresydney_userid' ) ;
- $this->register_bts_js();
- $this->register_timesheet_js_css();
- }
- private function register_bts_js()
- {
- wp_enqueue_style( 'bts', plugins_url('css/ts.css', __FILE__));
- wp_enqueue_script('bts', plugins_url('js/ts.js', __FILE__), array('jquery', 'jquery-ui-core'));
- wp_localize_script( 'bts', 'bts1', array(
- 'ajax_url' => admin_url( 'admin-ajax.php' ),
- 'nonce' => $this->nonce, // It is common practice to comma after
- 'display_name' => wp_get_current_user()->display_name,
- 'anonymous' => !is_user_logged_in(),
- 'me'=> get_current_user_id(),
- 'userid'=> $this->acaresydney_userid,
- ) );
- }
-
- private function register_timesheet_js_css(){
- global $pagename;
- if ($pagename != 'time-sheets'){
- return;
- }
- wp_enqueue_style( 'bts_ts', plugins_url('css/bts_timesheet.css', __FILE__));
- wp_enqueue_script( 'bts_ts', plugins_url('js/bts_timesheet.js', __FILE__), array( 'jquery' , 'bts' ));
- }
-
- //sync users to wordpress system
- public function sync_users(){
- $this->sync_clients();
- $this->sync_employees();
- }
- private function sync_clients(){
- $contacts = $this->xero->getClients($this->clientgroup);
- foreach ($contacts as $c){
- $this->ensure_contact_exists($c);
- }
- }
-
- private function sync_employees(){
-
- }
-
- private function ensure_contact_exists($contact){
- $login = $contact->getContactID();
- $xero_contact = $this->xero->getContact($login);
- $args = $this->xero_contact_profile($xero_contact);
- $user = get_user_by('login', $login);
- if ($user === false){
- wp_insert_user($args);
- }else{//update user
- $args['ID'] = $user->ID;
- unset($args['user_pass']); //we don't change password
- wp_update_user($args);
- }
- }
-
- private function xero_contact_profile($c){
- $args = [
- 'user_login' => $username = $c->getContactID(),
- 'user_pass' => md5(uniqid(rand() + time(), true)),
- 'display_name' =>$c->getName(),
- 'user_email' => $c->getEmailAddress(),
- 'first_name' => $c->getFirstName(),
- 'last_name' => $c->getLastName(),
- 'nickname' => $c->getName(),
- 'role' => 'client',
- ];
- return $args;
- }
- }
-
- new AcareOffice();
|