srikat icon

Plugin.php

srikat | PRO | 06/06/22 08:37:09 PM UTC | 0 ⭐ | 374 👁️ | Never ⏰ | []
text |

5.93 KB

|

None

|

0 👍

/

0 👎

<?php
 namespace BricksExtras;
 // use Bricks\Capabilities;
 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
 if ( ! class_exists( 'BricksExtrasUpdater' ) ) {
	// load our custom updater.
	require_once 'BricksExtrasUpdater.php';
}
 if ( ! class_exists( 'BricksExtrasLicense' ) ) {
	require_once 'BricksExtrasLicense.php';
}
 if ( ! class_exists( 'SettingsPage' ) ) {
	require_once 'SettingsPage.php';
}
 if ( ! class_exists( 'ChangelogPage' ) ) {
	require_once 'ChangelogPage.php';
}
 class Plugin {
 	const PREFIX    = 'bricksextras_';
	const TITLE     = 'BricksExtras';
	const VERSION   = '1.0.0';
	const STORE_URL = 'https://bricksextras.com';
	const ITEM_ID   = 367;
 	public function __construct() {
		/* Runs when the plugin is activated */
		add_action( 'activate_' . BRICKSEXTRAS_BASE, array( __CLASS__, 'activate' ), 10, 2 );
 		/* Fires after WordPress has finished loading but before any headers are sent */
		add_action( 'init', array( __CLASS__, 'bricksextras_init' ), 30 );
 		/* EDD licenseing activation form */
		BricksExtrasLicense::init( self::PREFIX, self::TITLE, self::STORE_URL, self::ITEM_ID );
 		/* Add admin menu item */
		add_action( 'admin_menu', array( __CLASS__, 'admin_menu' ), 11 );
 		/* Add text strings to builder */
		add_filter( 'bricks/builder/i18n', [ __CLASS__, 'x_i18n' ]);
 		/* Add settings link to plugin page */
		add_filter( 'plugin_action_links_' . BRICKSEXTRAS_BASE, array( __CLASS__, 'settings_link' ) );
 		/* Setup the plugin updater */
		add_action( 'admin_init', [ __CLASS__, 'plugin_updater' ], 0 );
 		add_action( 'admin_enqueue_scripts', [ __CLASS__, 'admin_settings_styles'] );
 		add_action( 'wp_enqueue_scripts', [ __CLASS__, 'enqueue_defaults'] , 1 );
 		/* Clean database on uninstall */
		register_uninstall_hook( BRICKSEXTRAS_BASE, array( __CLASS__, 'clean_db_on_uninstall' ) );
 	}
 	/* Add text strings to builder */
	public static function x_i18n( $i18n ) {
 		// For element category 'extras'
		$i18n['extras'] = esc_html__( 'Extras', 'bricks' );
 		return $i18n;
 	}
 	/* Runs when the plugin is activated */
	public static function activate( $plugin ) {
		if ( ! function_exists( 'bricks_is_builder' ) ) {
			die( '<p>\'Bricks builder\' must be installed and activated, in order to activate \'' . self::TITLE . '\'</p>' );
		}
	}
 	/* Add admin menu item */
	public static function admin_menu() {
		// Return: Current user has no access to Bricks
		if ( \Bricks\Capabilities::current_user_has_no_access() ) {
			return;
		}
 		global $menu;
		$menu_exists = false;
 		foreach ( $menu as $item ) {
			if ( array_search( 'bricks', $item ) !== false ) {
				$menu_exists = true;
				break;
			}
		}
 		add_submenu_page( 'bricks', self::TITLE, self::TITLE, 'manage_options', self::PREFIX . 'menu', array( __CLASS__, 'menu_item' ) );
	}
 	/* Add settings link to plugin page */
	public static function settings_link( $links ) {
		$url = esc_url(
			add_query_arg(
				'page',
				self::PREFIX . 'menu',
				get_admin_url() . 'admin.php'
			)
		);
 		// Create the link.
		$settings_link = "<a href='$url'>" . __( 'Settings' ) . '</a>';
 		// Adds the link to the beginning of the array.
		array_unshift(
			$links,
			$settings_link
		);
 		return $links;
	}
 	/* Setup the plugin updater */
	public static function plugin_updater() {
		// retrieve our license key from the DB.
		$license_key = trim( get_option( self::PREFIX . 'license_key' ) );
 		// setup the updater.
		$edd_updater = new BricksExtrasUpdater(
			self::STORE_URL,
			__FILE__,
			array(
				'version'   => self::VERSION, // current version number
				'license'   => $license_key, // license key (used get_option above to retrieve from DB)
				'item_id'   => self::ITEM_ID, // ID of the product
				'item_name' => self::TITLE,
				'author'    => 'BricksExtras', // author of this plugin
				'url'       => home_url(),
				'beta'      => false,
			)
		);
	}
 	/* Clean database on uninstall */
	public static function clean_db_on_uninstall() {
		foreach ( wp_load_alloptions() as $option => $value ) {
			if ( strpos( $option, 'bricksextras_' ) === 0 ) {
				delete_option( $option );
			}
		}
	}
 	/* Settings page styles */
	public static function admin_settings_styles($hook_suffix) {
		wp_enqueue_style( 'x_admin_css', BRICKSEXTRAS_URL . 'includes/css/admin.css', false, '1.0.0' );
	}
 	public static function enqueue_defaults() {
		wp_enqueue_script( 'x-frontend', BRICKSEXTRAS_URL . 'components/assets/js/frontend.js', '', '1.0.0', true );
	}
 	/* Settings page tabs */
	public static function menu_item() {
		$tab = isset( $_GET['tab'] ) ? sanitize_text_field( $_GET['tab'] ) : false;
		?>
		<div class="wrap bricksextras-settings">
			<h1>BricksExtras Settings</h1>
			<br>
			<h2 class="nav-tab-wrapper">
				<a href="?page=<?php echo self::PREFIX . 'menu'; ?>&amp;tab=settings" class="nav-tab<?php echo ( $tab === false || $tab == 'settings' ) ? ' nav-tab-active' : ''; ?>">Elements</a>
				<a href="?page=<?php echo self::PREFIX . 'menu'; ?>&amp;tab=license" class="nav-tab<?php echo $tab == 'license' ? ' nav-tab-active' : ''; ?>">License</a>
				<a href="?page=<?php echo self::PREFIX . 'menu'; ?>&amp;tab=changelog" class="nav-tab<?php echo $tab == 'changelog' ? ' nav-tab-active' : ''; ?>">Changelog</a>
			</h2>
			<div class="bricks-admin-wrapper">
			<?php
			if ( $tab === 'license' ) {
				BricksExtrasLicense::license_page();
			} elseif ( 'changelog' === $tab ) {
				ChangelogPage::init();
			} else {
				SettingsPage::init( self::PREFIX, self::TITLE, self::VERSION );
			}
			?>
		</div></div>
		<?php
	}
 	/* Fires after WordPress has finished loading but before any headers are sent */
	public static function bricksextras_init() {
 		if ( ! class_exists( 'BricksExtrasMain' ) ) {
			require_once 'BricksExtrasMain.php';
		}
 		$BricksExtras = new BricksExtrasMain( self::PREFIX );
 	}
   }

Comments