Skip to content

Commit

Permalink
fix: recommended plugins can't be installed [#500]
Browse files Browse the repository at this point in the history
  • Loading branch information
cristian-ungureanu committed Aug 17, 2020
1 parent 75e6e53 commit 559c245
Show file tree
Hide file tree
Showing 5 changed files with 362 additions and 88 deletions.
126 changes: 44 additions & 82 deletions core/app/class-orbit-fox-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,22 @@ public function enqueue_scripts() {
}
if ( in_array( $screen->id, array( 'toplevel_page_obfx_companion' ), true ) ) {
wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . '../assets/js/orbit-fox-admin.js', array( 'jquery' ), $this->version, false );

wp_register_script( 'obfx-plugin-install', plugin_dir_url( __FILE__ ) . '../assets/js/plugin-install.js', array( 'jquery' ), $this->version, true );

wp_localize_script(
'obfx-plugin-install',
'obfxPluginInstall',
array(
'activating' => esc_html__( 'Activating ', 'themeisle-companion' ),
'installing' => esc_html__( 'Installing ... ', 'themeisle-companion' ),
)
);

wp_enqueue_script( 'plugin-install' );
wp_enqueue_script( 'updates' );
wp_enqueue_script( 'obfx-plugin-install' );

}
do_action( 'obfx_admin_enqueue_scripts' );
}
Expand Down Expand Up @@ -259,97 +275,43 @@ public function load_recommended_plugins() {
'wordpress-seo',
];
shuffle( $plugins );
add_thickbox();
echo sprintf( '<div class="obfx-recommended-title-wrapper"><span class="obfx-recommended-title"><span class="dashicons dashicons-megaphone"></span> &nbsp; %s</span><span class="obfx-recommended-disclosure"><i> <span class="dashicons dashicons-editor-help obfx-show-disclosure"></span>Some of these plugins are developed by us and from some others if you use them and upgrade to pro, we might earn a comission.</i></span><div class="clearfix"></div> </div>', 'Orbit Fox recommends' );

$install_instance = new Orbit_Fox_Plugin_Install();

foreach ( $plugins as $plugin ) {
$current_plugin = $this->call_plugin_api( $plugin );
$current_plugin = $install_instance->call_plugin_api( $plugin );
if ( ! isset( $current_plugin->name ) ) {
continue;
}
$image = $current_plugin->icons['1x'];
$name = $current_plugin->name;
$desc = $current_plugin->short_description;
$url = add_query_arg(
array(
'tab' => 'plugin-information',
'plugin' => $plugin,
'TB_iframe' => true,
'width' => 800,
'height' => 800,
),
network_admin_url( 'plugin-install.php' )
);
echo sprintf(
'<div class="tile obfx-recommended ">
<div class="tile-icon">
<div class="obfx-icon-recommended">
<img width="100" src="%s"/>
</div>
</div>
<div class="tile-content">
<p class="tile-title">%s</p>
<p class="tile-subtitle">%s</p>
</div>
<div class="tile-action">
<div class="form-group">
<label class="form-switch activated">
<a class="button button-primary thickbox " href="%s"><span class="dashicons dashicons-download"></span>%s</a>
</label>
</div>
</div>
</div>',
esc_url( $image ),
esc_attr( $name ),
esc_attr( $desc ),
esc_url( $url ),
esc_attr__( 'Install', 'themeisle-companion' )
);
$image = $install_instance->check_for_icon( $current_plugin->icons );
$name = $current_plugin->name;
$desc = $current_plugin->short_description;
$button = $install_instance->get_button_html( $plugin );

echo '<div class="tile obfx-recommended ">';
echo '<div class="tile-icon">';
echo '<div class="obfx-icon-recommended">';
echo '<img width="100" src="' . esc_url( $image ) . '"/>';
echo '</div>';
echo '</div>';
echo '<div class="tile-content">';
echo '<p class="tile-title">' . esc_html( $name ) . '</p>';
echo '<p class="tile-subtitle">' . esc_html( $desc ) . '</p>';
echo '</div>';
echo '<div class="tile-action">';
echo '<div class="form-group">';
echo '<label class="form-switch activated">';
echo wp_kses_post( $button );
echo '</label>';
echo '</div>';
echo '</div>';
echo '</div>';
}

}

/**
* Get info from wporg api.
*
* @param string $slug Plugin slug.
*
* @return array|mixed|object|WP_Error
*/
public function call_plugin_api( $slug ) {
include_once ABSPATH . 'wp-admin/includes/plugin-install.php';

$call_api = get_transient( 'ti_plugin_info_' . $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,
'banners' => true,
),
)
);
set_transient( 'ti_plugin_info_' . $slug, $call_api, 1 * DAY_IN_SECONDS );
}

return $call_api;
}


/**
* This method is called via AJAX and processes the
Expand Down
190 changes: 190 additions & 0 deletions core/app/class-orbit-fox-plugin-install.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<?php
/**
* Class that handles plugins installation.
*/

/**
* Class Orbit_Fox_Plugin_Install
*/
class Orbit_Fox_Plugin_Install {

/**
* Get info from wordpress.org api.
*
* @param string $slug Plugin slug.
*
* @return array|mixed|object|WP_Error
*/
public function call_plugin_api( $slug ) {
include_once ABSPATH . 'wp-admin/includes/plugin-install.php';

$call_api = get_transient( 'ti_plugin_info_' . $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,
'banners' => true,
),
)
);
set_transient( 'ti_plugin_info_' . $slug, $call_api, 1 * DAY_IN_SECONDS );
}

return $call_api;
}

/**
* Get plugin icon.
*
* @param array $arr Icon sizes.
*
* @return string
*/
public function check_for_icon( $arr ) {
if ( ! empty( $arr['svg'] ) ) {
$plugin_icon_url = $arr['svg'];
} elseif ( ! empty( $arr['2x'] ) ) {
$plugin_icon_url = $arr['2x'];
} elseif ( ! empty( $arr['1x'] ) ) {
$plugin_icon_url = $arr['1x'];
} else {
$plugin_icon_url = $arr['default'];
}

return $plugin_icon_url;
}

/**
* Check plugin state.
*
* @param string $slug plugin slug.
*
* @return bool
*/
public function check_plugin_state( $slug ) {

$plugin_link_suffix = self::get_plugin_path( $slug );

if ( file_exists( ABSPATH . 'wp-content/plugins/' . $plugin_link_suffix ) ) {
return is_plugin_active( $plugin_link_suffix ) ? 'deactivate' : 'activate';
}

return 'install';
}


/**
* Get plugin path based on plugin slug.
*
* @param string $slug Plugin slug.
*
* @return string
*/
public static function get_plugin_path( $slug ) {

switch ( $slug ) {
case 'wpforms-lite':
return $slug . '/wpforms.php';
break;
case 'translatepress-multilingual':
return $slug . '/index.php';
break;
case 'feedzy-rss-feeds':
return $slug . '/feedzy-rss-feed.php';
break;
case 'wordpress-seo':
return $slug . '/wp-seo.php';
default:
return $slug . '/' . $slug . '.php';
}
}

/**
* Generate action button html.
*
* @param string $slug plugin slug.
* @param array $settings button settings.
*
* @return string
*/
public function get_button_html( $slug, $settings = array() ) {
$button = '';
$redirect = '';
if ( ! empty( $settings ) && array_key_exists( 'redirect', $settings ) ) {
$redirect = $settings['redirect'];
}
$state = $this->check_plugin_state( $slug );
if ( empty( $slug ) ) {
return '';
}

$additional = '';

if ( $state === 'deactivate' ) {
$additional = ' action_button active';
}

$button .= '<div class=" plugin-card-' . esc_attr( $slug ) . esc_attr( $additional ) . '" style="padding: 8px 0 5px;">';

$plugin_link_suffix = self::get_plugin_path( $slug );

$nonce = add_query_arg(
array(
'action' => 'activate',
'plugin' => rawurlencode( $plugin_link_suffix ),
'plugin_status' => 'all',
'paged' => '1',
'_wpnonce' => wp_create_nonce( 'activate-plugin_' . $plugin_link_suffix ),
),
esc_url( network_admin_url( 'plugins.php' ) )
);
switch ( $state ) {
case 'install':
$button .= '<a data-redirect="' . esc_url( $redirect ) . '" data-slug="' . esc_attr( $slug ) . '" class="install-now obfx-install-plugin button button-primary" href="' . esc_url( $nonce ) . '" data-name="' . esc_attr( $slug ) . '" aria-label="Install ' . esc_attr( $slug ) . '"><span class="dashicons dashicons-download"></span>' . __( 'Install and activate', 'themeisle-companion' ) . '</a>';
break;

case 'activate':
$button .= '<a data-redirect="' . esc_url( $redirect ) . '" data-slug="' . esc_attr( $slug ) . '" class="activate-now button button-primary" href="' . esc_url( $nonce ) . '" aria-label="Activate ' . esc_attr( $slug ) . '">' . esc_html__( 'Activate', 'themeisle-companion' ) . '</a>';
break;

case 'deactivate':
$nonce = add_query_arg(
array(
'action' => 'deactivate',
'plugin' => rawurlencode( $plugin_link_suffix ),
'plugin_status' => 'all',
'paged' => '1',
'_wpnonce' => wp_create_nonce( 'deactivate-plugin_' . $plugin_link_suffix ),
),
esc_url( network_admin_url( 'plugins.php' ) )
);

$button .= '<a data-redirect="' . esc_url( $redirect ) . '" data-slug="' . esc_attr( $slug ) . '" class="deactivate-now button button-primary" href="' . esc_url( $nonce ) . '" data-name="' . esc_attr( $slug ) . '" aria-label="Deactivate ' . esc_attr( $slug ) . '">' . esc_html__( 'Deactivate', 'themeisle-companion' ) . '</a>';
break;

default:
break;
}// End switch().
$button .= '</div>';

return $button;
}
}
27 changes: 26 additions & 1 deletion core/assets/css/orbit-fox-admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -3878,9 +3878,12 @@ ul.obfx-menu-tabs{
line-height: 15px;
box-shadow: none;
text-shadow: none;
display: inline;
}

.obfx-recommended .button-primary:hover {
.obfx-recommended .button-primary:hover,
.obfx-recommended .button-primary:active,
.obfx-recommended .button-primary:focus {
background: #fff;
border-color: #5764c6;
color: #5764c6;
Expand All @@ -3889,6 +3892,28 @@ ul.obfx-menu-tabs{
transition: 0.3s;
}

.obfx-recommended .button-primary.updating-message:before {
color: #fff;
margin: 0;
vertical-align: middle;
margin-right: 5px;
padding: 0;
}

.obfx-recommended .button-primary span.dashicons {
vertical-align: middle;
margin-right: 5px;
}

.obfx-recommended .button-primary:hover span.dashicons,
.obfx-recommended .button-primary.updating-message:hover:before,
.obfx-recommended .button-primary:active span.dashicons,
.obfx-recommended .button-primary.updating-message:active:before,
.obfx-recommended .button-primary:focus span.dashicons,
.obfx-recommended .button-primary.updating-message:focus:before{
color: #5764c6;
}

.wp-core-ui .button-primary:hover {
text-shadow: none;
}
Expand Down
Loading

0 comments on commit 559c245

Please sign in to comment.