Advertisement
srikat

Plugin.php

Jun 6th, 2022
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. <?php
  2.  
  3. namespace BricksExtras;
  4.  
  5. // use Bricks\Capabilities;
  6.  
  7. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  8.  
  9. if ( ! class_exists( 'BricksExtrasUpdater' ) ) {
  10. // load our custom updater.
  11. require_once 'BricksExtrasUpdater.php';
  12. }
  13.  
  14. if ( ! class_exists( 'BricksExtrasLicense' ) ) {
  15. require_once 'BricksExtrasLicense.php';
  16. }
  17.  
  18. if ( ! class_exists( 'SettingsPage' ) ) {
  19. require_once 'SettingsPage.php';
  20. }
  21.  
  22. if ( ! class_exists( 'ChangelogPage' ) ) {
  23. require_once 'ChangelogPage.php';
  24. }
  25.  
  26. class Plugin {
  27.  
  28. const PREFIX = 'bricksextras_';
  29. const TITLE = 'BricksExtras';
  30. const VERSION = '1.0.0';
  31. const STORE_URL = 'https://bricksextras.com';
  32. const ITEM_ID = 367;
  33.  
  34. public function __construct() {
  35. /* Runs when the plugin is activated */
  36. add_action( 'activate_' . BRICKSEXTRAS_BASE, array( __CLASS__, 'activate' ), 10, 2 );
  37.  
  38. /* Fires after WordPress has finished loading but before any headers are sent */
  39. add_action( 'init', array( __CLASS__, 'bricksextras_init' ), 30 );
  40.  
  41. /* EDD licenseing activation form */
  42. BricksExtrasLicense::init( self::PREFIX, self::TITLE, self::STORE_URL, self::ITEM_ID );
  43.  
  44. /* Add admin menu item */
  45. add_action( 'admin_menu', array( __CLASS__, 'admin_menu' ), 11 );
  46.  
  47. /* Add text strings to builder */
  48. add_filter( 'bricks/builder/i18n', [ __CLASS__, 'x_i18n' ]);
  49.  
  50. /* Add settings link to plugin page */
  51. add_filter( 'plugin_action_links_' . BRICKSEXTRAS_BASE, array( __CLASS__, 'settings_link' ) );
  52.  
  53. /* Setup the plugin updater */
  54. add_action( 'admin_init', [ __CLASS__, 'plugin_updater' ], 0 );
  55.  
  56. add_action( 'admin_enqueue_scripts', [ __CLASS__, 'admin_settings_styles'] );
  57.  
  58. add_action( 'wp_enqueue_scripts', [ __CLASS__, 'enqueue_defaults'] , 1 );
  59.  
  60. /* Clean database on uninstall */
  61. register_uninstall_hook( BRICKSEXTRAS_BASE, array( __CLASS__, 'clean_db_on_uninstall' ) );
  62.  
  63. }
  64.  
  65. /* Add text strings to builder */
  66. public static function x_i18n( $i18n ) {
  67.  
  68. // For element category 'extras'
  69. $i18n['extras'] = esc_html__( 'Extras', 'bricks' );
  70.  
  71. return $i18n;
  72.  
  73. }
  74.  
  75. /* Runs when the plugin is activated */
  76. public static function activate( $plugin ) {
  77. if ( ! function_exists( 'bricks_is_builder' ) ) {
  78. die( '<p>\'Bricks builder\' must be installed and activated, in order to activate \'' . self::TITLE . '\'</p>' );
  79. }
  80. }
  81.  
  82. /* Add admin menu item */
  83. public static function admin_menu() {
  84. // Return: Current user has no access to Bricks
  85. if ( \Bricks\Capabilities::current_user_has_no_access() ) {
  86. return;
  87. }
  88.  
  89. global $menu;
  90. $menu_exists = false;
  91.  
  92. foreach ( $menu as $item ) {
  93. if ( array_search( 'bricks', $item ) !== false ) {
  94. $menu_exists = true;
  95. break;
  96. }
  97. }
  98.  
  99. add_submenu_page( 'bricks', self::TITLE, self::TITLE, 'manage_options', self::PREFIX . 'menu', array( __CLASS__, 'menu_item' ) );
  100. }
  101.  
  102. /* Add settings link to plugin page */
  103. public static function settings_link( $links ) {
  104. $url = esc_url(
  105. add_query_arg(
  106. 'page',
  107. self::PREFIX . 'menu',
  108. get_admin_url() . 'admin.php'
  109. )
  110. );
  111.  
  112. // Create the link.
  113. $settings_link = "<a href='$url'>" . __( 'Settings' ) . '</a>';
  114.  
  115. // Adds the link to the beginning of the array.
  116. array_unshift(
  117. $links,
  118. $settings_link
  119. );
  120.  
  121. return $links;
  122. }
  123.  
  124. /* Setup the plugin updater */
  125. public static function plugin_updater() {
  126. // retrieve our license key from the DB.
  127. $license_key = trim( get_option( self::PREFIX . 'license_key' ) );
  128.  
  129. // setup the updater.
  130. $edd_updater = new BricksExtrasUpdater(
  131. self::STORE_URL,
  132. __FILE__,
  133. array(
  134. 'version' => self::VERSION, // current version number
  135. 'license' => $license_key, // license key (used get_option above to retrieve from DB)
  136. 'item_id' => self::ITEM_ID, // ID of the product
  137. 'item_name' => self::TITLE,
  138. 'author' => 'BricksExtras', // author of this plugin
  139. 'url' => home_url(),
  140. 'beta' => false,
  141. )
  142. );
  143. }
  144.  
  145. /* Clean database on uninstall */
  146. public static function clean_db_on_uninstall() {
  147. foreach ( wp_load_alloptions() as $option => $value ) {
  148. if ( strpos( $option, 'bricksextras_' ) === 0 ) {
  149. delete_option( $option );
  150. }
  151. }
  152. }
  153.  
  154. /* Settings page styles */
  155. public static function admin_settings_styles($hook_suffix) {
  156. wp_enqueue_style( 'x_admin_css', BRICKSEXTRAS_URL . 'includes/css/admin.css', false, '1.0.0' );
  157. }
  158.  
  159. public static function enqueue_defaults() {
  160. wp_enqueue_script( 'x-frontend', BRICKSEXTRAS_URL . 'components/assets/js/frontend.js', '', '1.0.0', true );
  161. }
  162.  
  163. /* Settings page tabs */
  164. public static function menu_item() {
  165. $tab = isset( $_GET['tab'] ) ? sanitize_text_field( $_GET['tab'] ) : false;
  166. ?>
  167. <div class="wrap bricksextras-settings">
  168. <h1>BricksExtras Settings</h1>
  169. <br>
  170. <h2 class="nav-tab-wrapper">
  171. <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>
  172. <a href="?page=<?php echo self::PREFIX . 'menu'; ?>&amp;tab=license" class="nav-tab<?php echo $tab == 'license' ? ' nav-tab-active' : ''; ?>">License</a>
  173. <a href="?page=<?php echo self::PREFIX . 'menu'; ?>&amp;tab=changelog" class="nav-tab<?php echo $tab == 'changelog' ? ' nav-tab-active' : ''; ?>">Changelog</a>
  174. </h2>
  175. <div class="bricks-admin-wrapper">
  176. <?php
  177. if ( $tab === 'license' ) {
  178. BricksExtrasLicense::license_page();
  179. } elseif ( 'changelog' === $tab ) {
  180. ChangelogPage::init();
  181. } else {
  182. SettingsPage::init( self::PREFIX, self::TITLE, self::VERSION );
  183. }
  184. ?>
  185. </div></div>
  186. <?php
  187. }
  188.  
  189. /* Fires after WordPress has finished loading but before any headers are sent */
  190. public static function bricksextras_init() {
  191.  
  192. if ( ! class_exists( 'BricksExtrasMain' ) ) {
  193. require_once 'BricksExtrasMain.php';
  194. }
  195.  
  196. $BricksExtras = new BricksExtrasMain( self::PREFIX );
  197.  
  198. }
  199.  
  200.  
  201.  
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement