site_host = domain_mapping_siteurl( get_current_blog_id() );
} else {
$this->site_host = site_url();
}
// Get License key
$this->license_key = trim( get_option( 'modula_pro_license_key' ) );
add_action( 'plugins_loaded', array( $this, 'load_addon_upsells' ), 15 );
}
/**
* Load the upsells class
*/
public function load_addon_upsells() {
// Initialize WPChill upsell class
$args = apply_filters( 'modula_upsells_args', array(
'shop_url' => 'https://wp-modula.com',
'slug' => 'modula',
) );
if ( class_exists( 'WPChill_Upsells' ) ) {
$this->wpchill_upsells = WPChill_Upsells::get_instance( $args );
}
}
private function check_for_addons() {
if ( false !== ( $data = get_transient( 'modula_pro_licensed_extensions' ) ) ) {
$this->addons = is_array( $data ) ? $data : array();
return;
}
// Make sure this matches the exact URL from your site.
$url = apply_filters( 'modula_addon_server_url', MODULA_PRO_STORE_URL . '/wp-json/mt/v1/get-licensed-extensions' );
$url = add_query_arg(
array(
'license' => $this->license_key,
'url' => $this->site_host,
),
$url
);
// Get data from the remote URL.
$response = wp_remote_get( $url );
if ( ! is_wp_error( $response ) ) {
// Decode the data that we got.
$data = json_decode( wp_remote_retrieve_body( $response ), true );
if ( ! empty( $data ) && is_array( $data ) ) {
$this->addons = $data;
// Store the data for a week.
set_transient( 'modula_pro_licensed_extensions', $data, 7 * DAY_IN_SECONDS );
}
}
}
/**
* Sort addons based on packages
*
* @param $addons
*
* @return mixed
* @since 2.5.0
*/
public function sort_addons( $addons ) {
$this->check_for_addons();
if ( $this->addons && $addons ) {
$i = 99;
$j = 1;
foreach ( $addons as $key => $addon ) {
if ( array_key_exists( $addon[ 'slug' ], $this->addons ) ) {
$addons[ $key ][ 'priority' ] = $j;
$j++;
} else {
$addons[ $key ][ 'priority' ] = $i;
$i--;
}
}
}
uasort( $addons, array( 'Modula_Helper', 'sort_data_by_priority' ) );
return $addons;
}
public function addons_scripts( $hook ) {
if ( 'modula-gallery_page_modula-addons' == $hook ) {
wp_enqueue_script( 'modula-pro-addon', MODULA_PRO_URL . 'assets/js/wp-modula-addons.js', array( 'jquery' ), '2.0.0', true );
$args = array(
'install_nonce' => wp_create_nonce( 'modula-pro-install' ),
'connect_error' => esc_html__( 'ERROR: There was an error connecting to the server, Please try again.', 'modula-pro' ),
'installing_text' => esc_html__( 'Installing addon...', 'modula-pro' ),
'activating_text' => esc_html__( 'Activating addon...', 'modula-pro' ),
'deactivating_text' => esc_html__( 'Deactivating addon...', 'modula-pro' ),
'installing_mass_addons' => esc_html__( 'Installing & Activating all addons, please wait..','modula-pro' ),
'deactivating_mass_addons' => esc_html__( 'Deactivating all addons, please wait..','modula-pro' ),
'installing_mass_addons_complete' => esc_html__( 'All addons have been installed & activated.','modula-pro' ),
'deactivating_mass_addons_complete' => esc_html__( 'All addons have been deactivated.','modula-pro' )
);
wp_localize_script( 'modula-pro-addon', 'modulaPRO', $args );
}
}
public function add_license_to_url( $url ) {
return add_query_arg(
array(
'license' => get_option( 'modula_pro_license_key' ),
'url' => site_url(),
),
$url
);
}
public function output_download_link( $link, $addon ) {
if ( empty( $this->addons ) && '' != $this->license_key ) {
$this->check_for_addons();
}
$action = 'install';
if ( ! isset( $addon['slug'] ) ) {
$url = admin_url( 'edit.php?post_type=modula-gallery&page=modula&modula-tab=licenses' );
$link = '' . esc_html__( 'Add license', 'modula-pro' ) . '';
return $link;
}
$licenses_status = get_option('modula_pro_license_status', false);
if ( array_key_exists( $addon['slug'], $this->addons ) && !(!$licenses_status || 'valid' != $licenses_status->license) ) {
$slug = $addon['slug'];
$plugin_path = $slug . '/' . $slug . '.php';
if ( $this->check_plugin_is_installed( $addon['slug'] ) && ! $this->check_plugin_is_active( $plugin_path ) ) {
$action = 'activate';
} elseif ( $this->check_plugin_is_active( $plugin_path ) ) {
$action = 'installed';
}
if ( 'install' != $action ) {
$url = $this->create_plugin_link( $action, $plugin_path );
} else {
$url = $this->addons[ $addon['slug'] ]['download_link'];
}
$settings_url = '';
switch ( $slug ) {
case 'modula-albums':
$settings_url = admin_url( 'edit.php?post_type=modula-gallery&page=modula&modula-tab=standalone' );
break;
case 'modula-speedup':
$settings_url = admin_url( 'edit.php?post_type=modula-gallery&page=modula&modula-tab=compression' );
break;
case 'modula-watermark':
$settings_url = admin_url( 'edit.php?post_type=modula-gallery&page=modula&modula-tab=watermark' );
break;
case 'modula-roles':
$settings_url = admin_url( 'edit.php?post_type=modula-gallery&page=modula&modula-tab=roles' );
break;
}
$attr = '';
if ( 'installed' != $action ) {
$attr = 'disabled="disabled"';
}
$addons_with_settings = array(
'modula-watermark','modula-albums','modula-speedup','modula-roles'
);
$link = '';
$link .= '
';
$link .= '
';
$link .= '
';
$link .= '
';
$link .= '
';
$link .= '
';
$link .= '
';
$link .= '
';
$link .= '
';
$link .= '';
if ( in_array( $slug, $addons_with_settings ) ) {
$link .= '' . esc_html__( 'Settings','modula-pro' ) . '';
}
} elseif ( '' != $this->license_key && !(!$licenses_status || 'valid' != $licenses_status->license) ) { //the user has entered a license key, but this extension requires an upgrade
$url = MODULA_PRO_STORE_UPGRADE_URL . '?utm_source=modula-pro&utm_campaign=upsell&utm_medium=' . $addon['slug'] . '&license=' . $this->license_key;
$link = '' . esc_html__( 'Upgrade', 'modula-pro' ) . '';
} else {
$url = admin_url( 'edit.php?post_type=modula-gallery&page=modula&modula-tab=licenses' );
$link = '' . esc_html__( 'Add license', 'modula-pro' ) . '';
}
return $link;
}
// Function to check if a plugin is active
private function create_plugin_link( $state, $slug ) {
$string = '';
switch ( $state ) {
case 'installed':
$string = add_query_arg(
array(
'action' => 'deactivate',
'plugin' => rawurlencode( $slug ),
'plugin_status' => 'all',
'paged' => '1',
'_wpnonce' => wp_create_nonce( 'deactivate-plugin_' . $slug ),
),
admin_url( 'plugins.php' )
);
break;
case 'activate':
$string = add_query_arg(
array(
'action' => 'activate',
'plugin' => rawurlencode( $slug ),
'plugin_status' => 'all',
'paged' => '1',
'_wpnonce' => wp_create_nonce( 'activate-plugin_' . $slug ),
),
admin_url( 'plugins.php' )
);
break;
default:
$string = '';
break;
}// End switch().
return $string;
}
private function _get_plugins( $plugin_folder = '' ) {
if ( ! empty( $this->plugins ) ) {
return $this->plugins;
}
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$this->plugins = get_plugins( $plugin_folder );
return $this->plugins;
}
private function check_plugin_is_installed( $slug ) {
if ( file_exists( WP_PLUGIN_DIR . '/' . $slug ) ) {
return true;
}
return false;
}
/**
* @return bool
*/
private function check_plugin_is_active( $plugin_path ) {
if ( file_exists( WP_PLUGIN_DIR . '/' . $plugin_path ) ) {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
return is_plugin_active( $plugin_path );
}
}
// Install Addons
public function install_addons() {
// Run a security check first.
check_admin_referer( 'modula-pro-install', 'nonce' );
if ( ! current_user_can( 'install_plugins' ) ) {
echo json_encode( array( 'error' => esc_html__( 'There was an error installing the addon. Please try again.', 'modula-pro' ) ) );
die;
}
if ( ! isset( $_POST['plugin'] ) ) {
echo json_encode( array( 'error' => esc_html__( 'There was an error installing the addon. Please try again.', 'modula-pro' ) ) );
die;
}
$download_url = esc_url( $_POST['plugin'] );
if ( false === strpos( $download_url, MODULA_PRO_STORE_URL ) ) {
echo json_encode( array( 'error' => esc_html__( 'There was an error installing the addon. Please try again.', 'modula-pro' ) ) );
die;
}
global $hook_suffix;
// Set the current screen to avoid undefined notices.
set_current_screen();
// Prepare variables.
$method = '';
$url = add_query_arg(
array(
'page' => 'modula-pro-settings',
),
admin_url( 'admin.php' )
);
$url = esc_url( $url );
// Start output bufferring to catch the filesystem form if credentials are needed.
ob_start();
if ( false === ( $creds = request_filesystem_credentials( $url, $method, false, false, null ) ) ) {
$form = ob_get_clean();
echo json_encode( array( 'form' => $form ) );
die;
}
// If we are not authenticated, make it happen now.
if ( ! WP_Filesystem( $creds ) ) {
ob_start();
request_filesystem_credentials( $url, $method, true, false, null );
$form = ob_get_clean();
echo json_encode( array( 'form' => $form ) );
die;
}
// We do not need any extra credentials if we have gotten this far, so let's install the plugin.
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
require_once MODULA_PRO_PATH . 'includes/admin/class-modula-pro-skin.php';
// Create the plugin upgrader with our custom skin.
$installer = new Plugin_Upgrader( $skin = new Modula_PRO_Skin() );
$installer->install( $download_url );
// Flush the cache and return the newly installed plugin basename.
wp_cache_flush();
if ( $installer->plugin_info() ) {
$plugin_basename = $installer->plugin_info();
echo json_encode(
array(
'plugin' => $plugin_basename,
)
);
die;
}
// Send back a response.
echo json_encode( true );
die;
}
/**
* Activate Modula's Addons
*
* @since 2.5.0
*/
public function activate_addon() {
check_admin_referer( 'modula-pro-install', 'nonce' );
if ( !isset( $_POST[ 'plugin_path' ] ) ) {
echo json_encode( array( 'error' => esc_html__( 'No such addons exists.', 'modula-pro' ) ) );
die;
}
activate_plugin( $_POST[ 'plugin_path' ] );
echo json_encode(
array(
'text' => esc_html__( 'Addon activated', 'modula-pro' )
)
);
die;
}
/**
* Dectivate Modula's Addons
*
* @since 2.5.0
*/
public function deactivate_addon(){
check_admin_referer( 'modula-pro-install', 'nonce' );
if ( !isset( $_POST[ 'plugin_path' ] ) ) {
echo json_encode( array( 'error' => esc_html__( 'No such addons exists.', 'modula-pro' ) ) );
die;
}
deactivate_plugins( $_POST[ 'plugin_path' ] );
echo json_encode(
array(
'text' => esc_html__( 'Addon deactivated.', 'modula-pro' )
)
);
die;
}
/**
* Add extra action links to extensions tab
*
* @since 2.5.0
*/
public function extra_extensions_actions() {
if ( isset( $_GET['extensions'] ) && 'free' == $_GET['extensions'] ) {
return;
}
$quick_action_options = apply_filters(
'modula_extensions_quick_actions',
array(
'modula-install-all-addons' => esc_html__( 'Install all addons', 'modula-pro' ),
'modula-uninstall-all-addons' => esc_html__( 'Deactivate all addons', 'modula-pro' )
) );
$html = '';
$html .= '
';
$html .= '
' . esc_html__( 'Apply', 'modula-pro' ) . '';
$html .= '
';
echo $html;
}
/**
* Install all addons
*
* @since 2.5.0
*/
public function get_all_addons() {
check_admin_referer( 'modula-pro-install', 'nonce' );
if ( empty( $this->addons ) && '' != $this->license_key ) {
$this->check_for_addons();
}
if ( empty( $this->addons ) ) {
echo json_encode( array( 'error' => esc_html__( 'No addons found.', 'modula-pro' ) ) );
wp_die();
}
echo json_encode( $this->addons );
wp_die();
}
/**
* Update addon from extensions tab
*
* @param $addon
* @param $plugin_data
* @Since 2.5.0
*/
public function addon_update( $addon, $plugin_data ){
if ( $plugin_data && $addon['version'] != $plugin_data['Version'] && class_exists( 'Modula_PRO' ) ){
echo '' . esc_html__( 'Update now', 'modula-best-grid-gallery' ) . '';
}
}
/**
* Add notification to install Modula Albums
* @Since 2.5.5
*/
public function modula_albums_notification(){
if ( ! class_exists( 'Modula_Albums' ) && $this->wpchill_upsells && ! $this->wpchill_upsells->is_upgradable_addon( 'modula-albums' ) ){
echo sprintf( esc_html__( 'In order to use Modula Albums addon you need to install it from %shere%s.', 'modula-pro' ), '', '' );
}
}
/**
* Add notification to install Modula SpeedUp
* @Since 2.5.5
*/
public function modula_speedup_notification(){
if ( !class_exists( 'Modula_SpeedUp' ) && $this->wpchill_upsells && ! $this->wpchill_upsells->is_upgradable_addon( 'modula-speedup' ) ){
?>
wpchill_upsells && ! $this->wpchill_upsells->is_upgradable_addon( 'modula-advanced-shortcodes' ) ){
?>
wpchill_upsells && ! $this->wpchill_upsells->is_upgradable_addon( 'modula-watermark' )){
?>
wpchill_upsells && ! $this->wpchill_upsells->is_upgradable_addon( 'modula-roles' )){
?>