wpo365-custom-fields.php 4.43 KB
<?php

/**
 *  Plugin Name: WPO365 | CUSTOM USER FIELDS
 *  Plugin URI: https://www.wpo365.com/downloads/wpo365-custom-user-fields/
 *  Description: Synchronize Azure AD user attributes e.g. department, job title etc. to WordPress user profiles (includes the PROFILE+ extension).
 *  Version: 25.1
 *  Author: support@wpo365.com
 *  Author URI: https://www.wpo365.com
 *  License: See license.txt
 */

namespace Wpo;

require __DIR__ . '/vendor/autoload.php';

// Prevent public access to this script
defined('ABSPATH') or die();

if (!class_exists('\Wpo\Custom_Fields')) {

    class Custom_Fields
    {

        public function __construct()
        {
            // Show admin notification when BASIC edition is not installed
            add_action('admin_notices', array($this, 'ensure_wpo365_login'), 10, 0);
            add_action('network_admin_notices', array($this, 'ensure_wpo365_login'), 10, 0);
            add_action('plugins_loaded', array($this, 'ensure_wpo365_login'), 10, 0);
        }

        public function ensure_wpo365_login()
        {
            $version_exists = \class_exists('\Wpo\Core\Version') && isset(\Wpo\Core\Version::$current) && \Wpo\Core\Version::$current >= 20;

            if (current_action() == 'plugins_loaded') {

                if (!$version_exists) {

                    if (false === function_exists('deactivate_plugins')) {
                        require_once ABSPATH . 'wp-admin/includes/plugin.php';
                    }

                    deactivate_plugins(plugin_basename(__FILE__));
                }

                return;
            }

            $plugin_exists = \file_exists(dirname(__DIR__) . '/wpo365-login');

            // Required version installed and activated
            if ($version_exists) {
                return;
            }

            // Required plugin not installed
            if (!$plugin_exists) {
                $install_url = wp_nonce_url(
                    add_query_arg(
                        array(
                            'action' => 'install-plugin',
                            'plugin' => 'wpo365-login',
                            'from'   => 'plugins',
                        ),
                        self_admin_url('update.php')
                    ),
                    'install-plugin_wpo365-login'
                );
                echo '<div class="notice notice-error" style="margin-left: 2px;"><p>'
                    . sprintf(__('The %s plugin requires the latest version of %s to be installed and activated.', 'wpo365-login'), '<strong>WPO365 | CUSTOM USER FIELDS</strong>', '<strong>WPO365 | LOGIN (free)</strong>')
                    . '</p><p>'
                    . '<a class="button button-primary" href="' . esc_url($install_url) . '">' . __('Install plugin', 'wpo365-login') . '</a>.'
                    . '</p></div>';
                return;
            }

            // Required plubin installed but must either be activated or upgraded 
            $activate_url = add_query_arg(
                array(
                    '_wpnonce' => wp_create_nonce('activate-plugin_wpo365-login/wpo365-login.php'),
                    'action'   => 'activate',
                    'plugin'   => 'wpo365-login/wpo365-login.php',
                ),
                network_admin_url('plugins.php')
            );

            if (is_network_admin()) {
                $activate_url = add_query_arg(array('networkwide' => 1), $activate_url);
            }

            $update_url = wp_nonce_url(
                self_admin_url('update.php?action=upgrade-plugin&plugin=') . 'wpo365-login/wpo365-login.php',
                'upgrade-plugin_wpo365-login/wpo365-login.php'
            );

            echo '<div class="notice notice-error" style="margin-left: 2px;"><p>'
                . sprintf(__('The %s plugin requires the latest version of %s to be installed and activated.', 'wpo365-login'), '<strong>WPO365 | CUSTOM USER FIELDS</strong>', '<strong>WPO365 | LOGIN (free)</strong>')
                . '</p><p>'
                . '<a class="button button-primary" href="' . esc_url($activate_url) . '">' . __('Activate plugin', 'wpo365-login') . '</a>&nbsp;'
                . '<a class="button button-primary" href="' . esc_url($update_url) . '">' . __('Update plugin', 'wpo365-login') . '</a>'
                . '</p></div>';
        }
    }
}

$wpo365_custom_fields = new Custom_Fields();