AzureActiveDirectory_Plugin.php 12 KB
<?php


include_once('AzureActiveDirectory_LifeCycle.php');

class AzureActiveDirectory_Plugin extends AzureActiveDirectory_LifeCycle
{

    /**
     * See: http://plugin.michael-simpson.com/?page_id=31
     *
     * @return array of option meta data.
     */
    public function getOptionMetaData()
    {
        //  http://plugin.michael-simpson.com/?page_id=31
        return array(
            //'_version' => array('Installed Version'), // Leave this one commented-out. Uncomment to test upgrades.
            'appTenantDomainName' => array(__('Tenant Domain Name', 'appTenantDomainName')), // 90002 invalid_request
            'appPrincipalId'      => array(__('Principal Id', 'appPrincipalId')),// 70001 unauthorized_client
            'appObjectId'         => array(__('Object Id', 'appObjectId')), //
            'password'            => array(__('Password', 'password')), // 50012 invalid_client


            // 'AmAwesome' => array(__('I like this awesome plugin', 'my-awesome-plugin'), 'false', 'true'),
            //'CanDoSomething' => array(__('Which user role can do something', 'my-awesome-plugin'),
            //                             'Administrator', 'Editor', 'Author', 'Contributor', 'Subscriber', 'Anyone')
        );
    }

    //    protected function getOptionValueI18nString($optionValue) {
    //        $i18nValue = parent::getOptionValueI18nString($optionValue);
    //        return $i18nValue;
    //    }

    protected function initOptions()
    {
        $options = $this->getOptionMetaData();

        if (!empty($options)) {
            foreach ($options as $key => $arr) {
                if (is_array($arr) && count($arr > 1)) {
                    $this->addOption($key, $arr[1]);
                }
            }
        }
    }

    public function getPluginDisplayName()
    {
        return 'Azure Active Directory';
    }

    protected function getMainPluginFileName()
    {
        return 'azure-active-directory.php';
    }

    /**
     * See: http://plugin.michael-simpson.com/?page_id=101
     * Called by install() to create any database tables if needed.
     * Best Practice:
     * (1) Prefix all table names with $wpdb->prefix
     * (2) make table names lower case only
     *
     * @return void
     */
    protected function installDatabaseTables()
    {
        //        global $wpdb;
        //        $tableName = $this->prefixTableName('mytable');
        //        $wpdb->query("CREATE TABLE IF NOT EXISTS `$tableName` (
        //            `id` INTEGER NOT NULL");
    }

    /**
     * See: http://plugin.michael-simpson.com/?page_id=101
     * Drop plugin-created tables on uninstall.
     *
     * @return void
     */
    protected function unInstallDatabaseTables()
    {
        //        global $wpdb;
        //        $tableName = $this->prefixTableName('mytable');
        //        $wpdb->query("DROP TABLE IF EXISTS `$tableName`");
    }


    /**
     * Perform actions when upgrading from version X to version Y
     * See: http://plugin.michael-simpson.com/?page_id=35
     *
     * @return void
     */
    public function upgrade()
    {
    }

    public function addActionsAndFilters()
    {

        // Add options administration page
        // http://plugin.michael-simpson.com/?page_id=47
        add_action('admin_menu', array(&$this, 'addSettingsSubMenuPage'));
        add_action('wp_ajax_get_meta_keys', [$this, 'get_meta_keys']);
        add_action('wp_ajax_get_template_data', [$this, 'get_template_data']);
        add_action('wp_ajax_get_settings_data', [$this, 'get_settings_data']);
        add_action('wp_ajax_get_query_data', [$this, 'get_query_data']);
        include_once('AzureActiveDirectory_Directory_ShortCode.php');
        $sc = new AzureActiveDirectory_Directory_ShortCode();
        $sc->register('user-directory');
        //include_once('AzureActiveDirectory_Directory_ShortCode_Search.php');
        //$sc = new AzureActiveDirectory_Directory_ShortCode_Search();
        // $sc->register('user-directory-search');


        // Example adding a script & style just for the options administration page
        // http://plugin.michael-simpson.com/?page_id=47
        //        if (strpos($_SERVER['REQUEST_URI'], $this->getSettingsSlug()) !== false) {
        //            wp_enqueue_script('my-script', plugins_url('/js/my-script.js', __FILE__));
        //            wp_enqueue_style('my-style', plugins_url('/css/my-style.css', __FILE__));
        //        }


        // Add Actions & Filters
        // http://plugin.michael-simpson.com/?page_id=37


        // Adding scripts & styles to all pages
        // Examples:
       if(isset($_GET['page'])){
        if($_GET['page'] == 'AzureActiveDirectory_PluginSettings') {
            wp_enqueue_script(
                array(
                    'jquery',
                    'jquery-ui-core',
                    'jquery-ui-tabs',
                    'jquery-ui-sortable',
                    'wp-color-picker',
                    'thickbox',
                    'media-upload',
                    'acf-input',
                    'acf-datepicker',
                    'jquery-ui-draggable',
                    'jquery-ui-droppable',
                    'jquery-ui-widget	'
                )
            );
            wp_enqueue_script('jquery-ui-sortable', 'https://code.jquery.com/ui/1.12.1/jquery-ui.js');
            wp_enqueue_style('style', 'https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css');
            wp_enqueue_style('my-style', plugins_url('/assests/css/styles.css', __FILE__));
            wp_enqueue_style('qb-style', plugins_url('/assests/css/QueryBuilder.css', __FILE__));

            wp_enqueue_style('bt-style', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css');
            wp_enqueue_style(
                'bt-theme-style',
                'https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css'
            );
            wp_enqueue_script('bt-script', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js');
            wp_enqueue_script('qb-script', 'https://boiteaimages.com/demo/querybuilder/query-builder.js');
            function enqueue_my_scripts()
            {
                wp_enqueue_script('my-script', plugins_url('/assests/js/script.js', __FILE__));
                wp_enqueue_script('my-q-script', plugins_url('/assests/js/queryBuilder.js', __FILE__));
            }

            add_action('admin_enqueue_scripts', 'enqueue_my_scripts');
        }
	   }
        // Register short codes
        // http://plugin.michael-simpson.com/?page_id=39


        // Register AJAX hooks
        // http://plugin.michael-simpson.com/?page_id=41

    }

    public function get_query_data()
    {
        $directory_shortcode_query = get_option("AzureActiveDirectory_Plugin_directory_shortcode_query");
        $results                   = $directory_shortcode_query[$_POST['name']][0];

        wp_send_json(unserialize($results));
    }


    public function get_meta_keys()
    {
        // Get the meta keys from the settings
        $users      = GraphServiceAccessHelper::getFeed('users');
        $user_array = get_object_vars($users[0]);
        // unset($user_array['odata.type'],$user_array['objectType'],$user_array['objectId'],$user_array['accountEnabled'],$user_array['assignedLicenses'],$user_array['assignedPlans'],$user_array['provisioningErrors']);
        //    $meta_keys = $user_array;
        foreach ($meta_keys as $key => $value) {
            $results[] = array(
                'id'        => $key,
                'label'     => $key,
                'type'      => 'string',
                'operators' => array('equal', 'begins_with')
            );
        }

        wp_send_json($results);
    }

    public function get_template_data()
    {
        $directory_shortcode_template = get_option("AzureActiveDirectory_Plugin_directory_shortcode_template");
        wp_send_json($directory_shortcode_template[$_POST['name']][0]);


    }

    public function get_settings_data()
    {


        $row_count = 0;
        session_start();
        $dropped = "";
        $dragg   = "";
        $users   = GraphServiceAccessHelper::getFeed('users');


        $user_array = get_object_vars($users[0]);

        $directory_shortcode_settings = "";

        if (isset($_POST['name'])) {
            $directory_shortcode_settings = get_option("AzureActiveDirectory_Plugin_directory_shortcode_settings");

            $directory_shortcode_settings      = $directory_shortcode_settings[$_POST['name']];
            $string                            = get_option("AzureActiveDirectory_Plugin_directory_shortcode_template");
            $string                            = $string[$directory_shortcode_settings['dirtype']][0];
            $directory_shortcode_card_settings = $this->getContents($string, '{{', '}}');
        } else {

            $string                            = get_option("AzureActiveDirectory_Plugin_directory_shortcode_template");
            $string                            = $string[$_POST['dirtype']][0];
            $directory_shortcode_card_settings = $this->getContents($string, '{{', '}}');

        }
        //unset($user_array['odata.type'],$user_array['objectType'],$user_array['objectId'],$user_array['accountEnabled'],$user_array['assignedLicenses'],$user_array['assignedPlans'],$user_array['provisioningErrors']);

        foreach ($user_array as $key => $value) {

            if (!$this->in_array_r($key, $directory_shortcode_settings)) {
                $dragg .= '<li class="dragg">';
                $dragg .= '<a href="PleaseEnableJavascript.html" onclick="return false;" title="add' . $key . 'to list" class="adbutton ui-btn  ">';
                $dragg .= '<input type="checkbox" class="myCheckbox" style="display:none;" id="' . $key . '"  name="directory_shortcode_settings[]" value="' . $key . '" />' . $key . '</a> </li>';
            }
        };


        foreach ($directory_shortcode_card_settings as $key) {
            $dropped .= '<div id="' . $key . '" class="droppable ui-widget-header"> <p>' . $key . '</p>';
            if ($directory_shortcode_settings[0][$key] != "") {
                foreach ($directory_shortcode_settings[0][$key] as $optkey) {
                    $dropped .= '<li class="dragg dropped">';
                    $dropped .= '<a href="PleaseEnableJavascript.html" onclick="return false;" title="remove ' . $optkey . ' to list" class="adbutton ui-btn ui-icon-delete ui-btn-icon-left">';
                    $dropped .= '<input type="checkbox" class="myCheckbox" style="display:none;" id="' . $optkey . '"  name="directory_shortcode_settings[' . $key . '][]" value="' . $optkey . '" checked="checked" />' . $optkey . '</a> </li>';
                }
            }
            $dropped .= '</div>';

        }


        echo json_encode(
            array("dragg" => $dragg, "dropped" => $dropped, "dirtype" => $directory_shortcode_settings['dirtype'])
        );
        die();


    }

    function getContents($str, $startDelimiter, $endDelimiter)
    {
        $contents             = array();
        $startDelimiterLength = strlen($startDelimiter);
        $endDelimiterLength   = strlen($endDelimiter);
        $startFrom            = $contentStart = $contentEnd = 0;
        while (false !== ($contentStart = strpos($str, $startDelimiter, $startFrom))) {
            $contentStart += $startDelimiterLength;
            $contentEnd = strpos($str, $endDelimiter, $contentStart);
            if (false === $contentEnd) {
                break;
            }
            $contents[] = substr($str, $contentStart, $contentEnd - $contentStart);
            $startFrom  = $contentEnd + $endDelimiterLength;
        }

        return $contents;
    }


    function in_array_r($needle, $haystack, $strict = false)
    {
        foreach ($haystack as $item) {
            if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && $this->in_array_r(
                        $needle,
                        $item,
                        $strict
                    ))
            ) {
                return true;
            }
        }

        return false;
    }

}