Created
September 11, 2023 16:37
-
-
Save bogdan-mainwp/cdbed4d9786b17aa6525de94464a061a to your computer and use it in GitHub Desktop.
Basic MainWP one-page extension demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: MainWP Hello World Extension | |
* Plugin URI: https://yourwebsiteurl.com | |
* Description: MainWP Hello World Extension is used for the demo. | |
* Version: 1.0 | |
* Author: Your Name | |
* Author URI: https://yourwebsiteurl.com | |
*/ | |
class MainWP_HelloWorld_Extension_Activator { | |
protected $mainwpMainActivated = false; | |
protected $childEnabled = false; | |
protected $childKey = false; | |
protected $childFile; | |
protected $plugin_handle = 'mainwp-hello-world-extension'; | |
protected $product_id = 'MainWP MainWP Hello World Extension'; | |
protected $software_version = '1.0'; | |
public function __construct() { | |
$this->childFile = __FILE__; | |
add_filter( 'mainwp_getextensions', array( &$this, 'get_this_extension' ) ); | |
$this->mainwpMainActivated = apply_filters( 'mainwp_activated_check', false ); | |
if ( $this->mainwpMainActivated !== false ) { | |
$this->activate_this_plugin(); | |
} else { | |
add_action( 'mainwp_activated', array( &$this, 'activate_this_plugin' ) ); | |
} | |
add_action( 'admin_notices', array( &$this, 'mainwp_error_notice' ) ); | |
} | |
/** | |
* Add your extension to MainWP via the 'mainwp_getextensions' filter. | |
* | |
* @param array $params Array containing the extensions info. | |
* | |
* @return array $params Updated array containing the extensions info. | |
*/ | |
public function get_this_extension( $params ) { | |
$params[] = array( | |
'plugin' => __FILE__, | |
'api' => $this->plugin_handle, | |
'mainwp' => false, | |
'callback' => array( &$this, 'settings' ), | |
'apiManager' => false, | |
); | |
return $params; | |
} | |
/** | |
* Displays the extension page with an adequate header and footer. | |
*/ | |
public function settings() { | |
do_action( 'mainwp_pageheader_extensions', __FILE__ ); | |
?> | |
<div class="ui segment"> | |
<h1><?php esc_html_e( 'Hello World!', 'mainwp' ); ?></h1> | |
</div> | |
<?php | |
do_action( 'mainwp_pagefooter_extensions', __FILE__ ); | |
} | |
/** | |
* Activate the extension API license and initiate the extension. | |
*/ | |
public function activate_this_plugin() { | |
$this->mainwpMainActivated = apply_filters( 'mainwp_activated_check', $this->mainwpMainActivated ); | |
$this->childEnabled = apply_filters( 'mainwp_extension_enabled_check', __FILE__ ); | |
$this->childKey = $this->childEnabled['key']; | |
} | |
/** | |
* Get the extension key. | |
* | |
* @return string | |
*/ | |
public function get_child_key() { | |
return $this->childKey; | |
} | |
/** | |
* Get the extension file. | |
* | |
* @return string | |
*/ | |
public function get_child_file() { | |
return $this->childFile; | |
} | |
/** | |
* Render the warning notice if the MainWP Dashboard plugin is not activated. | |
*/ | |
public function mainwp_error_notice() { | |
global $current_screen; | |
if ( $current_screen->parent_base == 'plugins' && $this->mainwpMainActivated == false ) { | |
echo '<div class="error"><p>MainWP Hello World Extension ' . __( 'requires <a href="https://mainwp.com/" target="_blank">MainWP Dashboard Plugin</a> to be activated in order to work. Please install and activate <a href="https://mainwp.com/" target="_blank">MainWP Dashboard Plugin</a> first.', 'mainwp' ) . '</p></div>'; | |
} | |
} | |
} | |
global $mainWPHelloWorldExtensionActivator; | |
$mainWPHelloWorldExtensionActivator = new MainWP_HelloWorld_Extension_Activator(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment