product = $product;
$this->setup_hooks();
return $this;
}
/**
* Setup endpoints.
*/
private function setup_hooks() {
add_action( $this->product->get_key() . '_recommend_products', array( $this, 'render_products_box' ), 10, 4 );
add_action( 'admin_head', array( $this, 'enqueue' ) );
}
/**
* Check if we should load the module for this product.
*
* @param Product $product Product data.
*
* @return bool Should we load the module?
*/
public function can_load( $product ) {
return true;
}
/**
* Render products box content.
*
* @param array $plugins_list - list of useful plugins (in slug => nicename format).
* @param array $themes_list - list of useful themes (in slug => nicename format).
* @param array $strings - list of translated strings.
* @param array $preferences - list of preferences.
*/
public function render_products_box( $plugins_list, $themes_list, $strings, $preferences = array() ) {
if ( empty( $plugins_list ) && empty( $themes_list ) ) {
return;
}
if ( ! empty( $plugins_list ) && ! current_user_can( 'install_plugins' ) ) {
return;
}
if ( ! empty( $themes_list ) && ! current_user_can( 'install_themes' ) ) {
return;
}
add_thickbox();
if ( ! empty( $themes_list ) ) {
$list = $this->get_themes( $themes_list, $preferences );
if ( has_action( $this->product->get_key() . '_recommend_products_theme_template' ) ) {
do_action( $this->product->get_key() . '_recommend_products_theme_template', $list, $strings, $preferences );
} else {
echo '
';
foreach ( $list as $theme ) {
echo '
';
echo '
';
echo '
';
echo ' ' . esc_html( $theme->custom_name ) . '';
if ( ! isset( $preferences['description'] ) || ( isset( $preferences['description'] ) && $preferences['description'] ) ) {
echo '' . esc_html( substr( $theme->description, 0, strpos( $theme->description, '.' ) ) ) . '.';
}
echo '
';
echo '';
echo '
';
}
echo '
';
}
}
if ( ! empty( $plugins_list ) ) {
$list = $this->get_plugins( $plugins_list, $preferences );
if ( has_action( $this->product->get_key() . '_recommend_products_plugin_template' ) ) {
do_action( $this->product->get_key() . '_recommend_products_plugin_template', $list, $strings, $preferences );
} else {
echo '';
foreach ( $list as $current_plugin ) {
echo '
';
echo '
';
echo '
';
echo ' ' . esc_html( $current_plugin->custom_name ) . '';
if ( ! isset( $preferences['description'] ) || ( isset( $preferences['description'] ) && $preferences['description'] ) ) {
echo '' . esc_html( substr( $current_plugin->short_description, 0, strpos( $current_plugin->short_description, '.' ) ) ) . '. ';
}
echo '
';
echo ' ';
echo '
';
}
echo '
';
}
}
}
/**
* Collect all the information for the themes list.
*
* @param array $themes_list - list of useful themes (in slug => nicename format).
* @param array $preferences - list of preferences.
*
* @return array
*/
private function get_themes( $themes_list, $preferences ) {
$list = array();
foreach ( $themes_list as $slug => $nicename ) {
$theme = $this->call_theme_api( $slug );
if ( ! $theme ) {
continue;
}
$url = add_query_arg(
array(
'theme' => $theme->slug,
),
network_admin_url( 'theme-install.php' )
);
$name = empty( $nicename ) ? $theme->name : $nicename;
$theme->custom_url = $url;
$theme->custom_name = $name;
$list[] = $theme;
}
return $list;
}
/**
* Call theme api
*
* @param string $slug theme slug.
*
* @return array|mixed|object
*/
private function call_theme_api( $slug ) {
$theme = get_transient( 'ti_theme_info_' . $slug );
if ( false !== $theme ) {
return $theme;
}
$products = $this->safe_get(
'https://api.wordpress.org/themes/info/1.1/?action=query_themes&request[theme]=' . $slug . '&request[per_page]=1'
);
$products = json_decode( wp_remote_retrieve_body( $products ) );
if ( is_object( $products ) ) {
$theme = $products->themes[0];
set_transient( 'ti_theme_info_' . $slug, $theme, 6 * HOUR_IN_SECONDS );
}
return $theme;
}
/**
* Collect all the information for the plugins list.
*
* @param array $plugins_list - list of useful plugins (in slug => nicename format).
* @param array $preferences - list of preferences.
*
* @return array
*/
private function get_plugins( $plugins_list, $preferences ) {
$list = array();
foreach ( $plugins_list as $plugin => $nicename ) {
$current_plugin = $this->call_plugin_api( $plugin );
$name = empty( $nicename ) ? $current_plugin->name : $nicename;
$image = $current_plugin->banners['low'];
if ( isset( $preferences['image'] ) && 'icon' === $preferences['image'] ) {
$image = $current_plugin->icons['1x'];
}
$url = add_query_arg(
array(
'tab' => 'plugin-information',
'plugin' => $current_plugin->slug,
'TB_iframe' => true,
'width' => 800,
'height' => 800,
),
network_admin_url( 'plugin-install.php' )
);
$current_plugin->custom_url = $url;
$current_plugin->custom_name = $name;
$current_plugin->custom_image = $image;
$list[] = $current_plugin;
}
return $list;
}
/**
* Load css and scripts for the plugin recommend page.
*/
public function enqueue() {
$screen = get_current_screen();
if ( ! isset( $screen->id ) ) {
return;
}
if ( false === apply_filters( $this->product->get_key() . '_enqueue_recommend', false, $screen->id ) ) {
return;
}
?>