register_section_type( 'Epsilon_Section_Recommended_Actions' );
$this->enqueue();
parent::__construct( $manager, $id, $args );
}
/**
* Enqueue necessary styles and scripts
*/
public function enqueue() {
wp_enqueue_style( 'plugin-install' );
wp_enqueue_script( 'plugin-install' );
wp_enqueue_script( 'updates' );
wp_localize_script( 'updates', '_wpUpdatesItemCounts', array(
'totals' => wp_get_update_data(),
) );
wp_add_inline_script( 'plugin-install', 'var pagenow = "plugin-install";' );
}
/**
* Add custom parameters to pass to the JS via JSON.
*
* @since 1.0.0
* @access public
*/
public function json() {
$json = parent::json();
$json['action_option'] = $this->theme_specific_option;
$json['plugin_option'] = $this->theme_specific_plugin_option;
$json['actions'] = $this->get_actions();
$json['plugins'] = $this->get_plugins();
$count = 0;
foreach ( $this->actions as $action ) {
if ( $action['check'] ) {
continue;
}
$count += 1;
}
$json['total_actions'] = $count;
$json['social_text'] = $this->social_text;
$json['plugin_text'] = $this->plugin_text;
$json['facebook'] = $this->facebook;
$json['facebook_text'] = esc_html__( 'Facebook', 'epsilon-framework' );
$json['twitter'] = $this->twitter;
$json['twitter_text'] = esc_html__( 'Twitter', 'epsilon-framework' );
$json['wp_review'] = $this->wp_review;
$json['wp_review_text'] = esc_html__( 'Review this theme on w.org', 'epsilon-framework' );
if ( $this->wp_review ) {
$json['theme_slug'] = $this->theme_slug;
if ( empty( $this->theme_slug ) ) {
$json['theme_slug'] = get_template();
}
}
return $json;
}
/**
* Outputs the Underscore.js template.
*
* @since 1.0.0
* @access public
* @return void
*/
protected function render_template() {
//@formatter:off
?>
<# if( data.actions.length > 0 ){ #>
{{{ data.title }}}
<# }else{ #>
<# if( data.plugins.length > 0 ){ #>
{{{ data.plugin_text }}}
<# }else{ #>
{{{ data.social_text }}}
<# } #>
<# } #>
<# if( data.actions.length > 0 ){ #>
1 / {{{ data.total_actions }}}
<# } #>
<# if( data.actions.length > 0 ){ #>
<# i = 1 #>
<# for (action in data.actions) { #>
<# if( !data.actions[action].check ){ #>
{{{ data.actions[action].title }}}
{{{ data.actions[action].description }}}
<# if( data.actions[action].plugin_slug ){ #>
<# } #>
<# if( data.actions[action].help ){ #>
{{{ data.actions[action].help }}}
<# } #>
<# } #>
<# i++ #>
<# } #>
<# } #>
<# if( data.plugins.length > 0 ){ #>
<# for (plugin in data.plugins) { #>
<# if( !data.plugins[plugin].check ){ #>
{{{ data.plugins[plugin].title }}}
{{{ data.plugins[plugin].description }}}
<# if( data.plugins[plugin].plugin_slug ){ #>
<# } #>
<# if( data.plugins[plugin].help ){ #>
{{{ data.plugins[plugin].help }}}
<# } #>
<# } #>
<# } #>
<# } #>
class="succes" <# } else { #> class="succes hide" <# } #> >
<# if( data.facebook ){ #>
{{{ data.facebook_text }}}
<# } #>
<# if( data.twitter ){ #>
{{{ data.twitter_text }}}
<# } #>
<# if( data.wp_review ){ #>
{{{ data.wp_review_text }}}
<# } #>
theme_specific_option );
if ( ! $req_actions ) {
$req_actions = array();
foreach ( $this->actions as $k => $v ) {
$req_actions[ $v['id'] ] = true;
}
}
foreach ( $this->actions as $k => $v ) {
if ( $v['check'] ) {
continue;
}
if ( isset( $req_actions[ $v['id'] ] ) && ! $req_actions[ $v['id'] ] ) {
continue;
}
$v['index'] = $k + 1;
if ( ! empty( $v['plugin_slug'] ) ) {
$active = $this->_check_active( $v['plugin_slug'] );
$v['url'] = $this->_create_action_link( $active['needs'], $v['plugin_slug'] );
$v['class'] = '';
$plugin_update = $this->_check_plugin_update( $v['plugin_slug'] );
if ( 'deactivate' == $active['needs'] && ! $plugin_update ) {
$active['needs'] = 'update';
}
if ( 'install' !== $active['needs'] && $active['status'] ) {
$v['class'] = 'active';
}
switch ( $active['needs'] ) {
case 'install':
$v['button_class'] = 'install-now button';
$v['button_label'] = esc_html__( 'Install', 'epsilon-framework' );
break;
case 'activate':
$v['button_class'] = 'activate-now button button-primary';
$v['button_label'] = esc_html__( 'Activate', 'epsilon-framework' );
break;
case 'update':
$v['button_class'] = 'update-now button button-primary';
$v['button_label'] = esc_html__( 'Update', 'epsilon-framework' );
break;
case 'deactivate':
$v['button_class'] = 'deactivate-now button';
$v['button_label'] = esc_html__( 'Deactivate', 'epsilon-framework' );
break;
}
$v['path'] = $active['plugin_path'];
}
$arr[] = $v;
};// End foreach().
return $arr;
}
/**
* @return array
*/
private function get_plugins() {
$arr = array();
$req_plugins = get_option( $this->theme_specific_plugin_option );
if ( ! $req_plugins ) {
$req_plugins = array();
foreach ( $this->plugins as $k => $v ) {
$req_plugins[ $k ] = true;
}
}
foreach ( $this->plugins as $k => $v ) {
$active = $this->_check_active( $k );
if ( 'deactivate' === $active['needs'] ) {
continue;
}
if ( isset( $req_plugins[ $k ] ) && ! $req_plugins[ $k ] ) {
continue;
}
$t = array(
'class' => '',
'id' => $k,
'path' => $active['plugin_path'],
'plugin_slug' => $k,
);
$t['url'] = $this->_create_action_link( $active['needs'], $k );
if ( 'install' !== $active['needs'] && $active['status'] ) {
$t['class'] = 'active';
}
switch ( $active['needs'] ) {
case 'install':
$t['button_class'] = 'install-now button';
$t['button_label'] = esc_html__( 'Install', 'epsilon-framework' );
break;
case 'activate':
$t['button_class'] = 'activate-now button button-primary';
$t['button_label'] = esc_html__( 'Activate', 'epsilon-framework' );
break;
case 'update':
$t['button_class'] = 'update-now button button-primary';
$t['button_label'] = esc_html__( 'Update', 'epsilon-framework' );
break;
case 'deactivate':
$t['button_class'] = 'deactivate-now button';
$t['button_label'] = esc_html__( 'Deactivate', 'epsilon-framework' );
break;
}
$info = $this->_call_plugin_api( $k );
$t['description'] = isset( $info->short_description ) ? $info->short_description : '';
$t['title'] = $t['button_label'] . ': ' . isset( $info->name ) ? $info->name : '';
$arr[] = $t;
}// End foreach().
return $arr;
}
/**
* @param string $plugin_slug
*
* @return array
*/
private function _check_active( $plugin_slug = '' ) {
$plugin_path = $this->_get_plugin_basename_from_slug( $plugin_slug );
if ( file_exists( ABSPATH . 'wp-content/plugins/' . $plugin_path ) ) {
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
$needs = is_plugin_active( $plugin_path ) ? 'deactivate' : 'activate';
return array(
'status' => is_plugin_active( $plugin_path ),
'needs' => $needs,
'plugin_path' => $plugin_path,
);
}
return array(
'status' => false,
'needs' => 'install',
'plugin_path' => false,
);
}
/**
* @param string $need
* @param string $plugin_slug
*
* @return bool|string
*/
private function _create_action_link( $need = '', $plugin_slug = '' ) {
switch ( $need ) {
case 'install':
return wp_nonce_url(
add_query_arg(
array(
'action' => 'install-plugin',
'plugin' => $this->_get_plugin_basename_from_slug( $plugin_slug ),
),
network_admin_url( 'update.php' )
),
'install-plugin_' . $this->_get_plugin_basename_from_slug( $plugin_slug )
);
break;
case 'deactivate':
return add_query_arg(
array(
'action' => 'deactivate',
'plugin' => rawurlencode( $this->_get_plugin_basename_from_slug( $plugin_slug ) ),
'plugin_status' => 'all',
'paged' => '1',
'_wpnonce' => wp_create_nonce( 'deactivate-plugin_' . $this->_get_plugin_basename_from_slug( $plugin_slug ) ),
),
network_admin_url( 'plugins.php' )
);
break;
case 'activate':
return add_query_arg(
array(
'action' => 'activate',
'plugin' => rawurlencode( $this->_get_plugin_basename_from_slug( $plugin_slug ) ),
'plugin_status' => 'all',
'paged' => '1',
'_wpnonce' => wp_create_nonce( 'activate-plugin_' . $this->_get_plugin_basename_from_slug( $plugin_slug ) ),
),
network_admin_url( 'plugins.php' )
);
break;
case 'update':
return wp_nonce_url(
add_query_arg(
array(
'action' => 'upgrade-plugin',
'plugin' => rawurlencode( $this->_get_plugin_basename_from_slug( $plugin_slug ) ),
),
network_admin_url( 'update.php' )
),
'upgrade-plugin_' . $plugin_slug
);
break;
default:
return false;
break;
}// End switch().
}
/**
* @param string $plugin_folder
*
* @return array
*/
private function _get_plugins( $plugin_folder = '' ) {
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
return get_plugins( $plugin_folder );
}
/**
* @param $slug
*
* @return mixed
*/
private function _get_plugin_basename_from_slug( $slug ) {
$keys = array_keys( $this->_get_plugins() );
foreach ( $keys as $key ) {
if ( preg_match( '|^' . $slug . '/|', $key ) ) {
return $key;
}
}
return $slug;
}
/**
* @param $slug
*
* @return bool
*/
private function _check_plugin_update( $slug ) {
$update_plugin_transient = get_site_transient( 'update_plugins' );
if ( isset( $update_plugin_transient->response ) ) {
$plugins = $update_plugin_transient->response;
foreach ( $plugins as $key => $plugin ) {
if ( preg_match( '|^' . $slug . '/|', $key ) ) {
return false;
}
}
}
return true;
}
/**
* @param $slug
*
* @return array|mixed|object|WP_Error
*/
private function _call_plugin_api( $slug ) {
include_once( ABSPATH . 'wp-admin/includes/plugin-install.php' );
$call_api = get_transient( 'epsilon_plugin_information_transient_' . $slug );
if ( false === $call_api ) {
$call_api = plugins_api( 'plugin_information', array(
'slug' => $slug,
'fields' => array(
'downloaded' => false,
'rating' => false,
'description' => false,
'short_description' => true,
'donate_link' => false,
'tags' => false,
'sections' => true,
'homepage' => true,
'added' => false,
'last_updated' => false,
'compatibility' => false,
'tested' => false,
'requires' => false,
'downloadlink' => false,
'icons' => true,
),
) );
set_transient( 'epsilon_plugin_information_transient_' . $slug, $call_api, 30 * MINUTE_IN_SECONDS );
}
return $call_api;
}
}