product = $product_object; parent::__construct( $product_object ); } /** * Registers the hooks */ public function setup_hooks_child() { $this->setup_vars(); add_action( 'wp_dashboard_setup', array( &$this, 'add_widget' ) ); add_action( 'wp_network_dashboard_setup', array( &$this, 'add_widget' ) ); add_filter( 'themeisle_sdk_recommend_plugin_or_theme', array( &$this, 'recommend_plugin_or_theme' ) ); } /** * Setup class variables */ function setup_vars() { $this->dashboard_name = apply_filters( 'themeisle_sdk_dashboard_widget_name', 'WordPress Guides/Tutorials' ); $this->feeds = apply_filters( 'themeisle_sdk_dashboard_widget_feeds', array( 'https://themeisle.com/blog/feed', ) ); } /** * Add widget to the dashboard * * @return string|void */ function add_widget() { global $wp_meta_boxes; if ( isset( $wp_meta_boxes['dashboard']['normal']['core']['themeisle'] ) ) { return; } wp_add_dashboard_widget( 'themeisle', $this->dashboard_name, array( &$this, 'render_dashboard_widget', ) ); } /** * Setup feed items. */ private function setup_feeds() { $items_normalized = array(); if ( false === ( $items_normalized = get_transient( 'themeisle_sdk_feed_items' ) ) ) { // Load SimplePie Instance $feed = fetch_feed( $this->feeds ); // TODO report error when is an error loading the feed if ( is_wp_error( $feed ) ) { return; } $items = $feed->get_items( 0, 5 ); foreach ( (array) $items as $item ) { $items_normalized[] = array( 'title' => $item->get_title(), 'date' => $item->get_date( 'U' ), 'link' => $item->get_permalink(), ); } set_transient( 'themeisle_sdk_feed_items', $items_normalized, 48 * HOUR_IN_SECONDS ); } $this->items = $items_normalized; } /** * Render widget content */ function render_dashboard_widget() { $this->setup_feeds(); if ( empty( $this->items ) || ! is_array( $this->items ) ) { return; } ?> $recommend['slug'], ), network_admin_url( 'theme-install.php' ) ); if ( 'plugin' === $type ) { $url = add_query_arg( array( 'tab' => 'plugin-information', 'plugin' => $recommend['slug'], ), network_admin_url( 'plugin-install.php' ) ); } ?> exists(); } else { $all_plugins = array_keys( get_plugins() ); foreach ( $all_plugins as $slug ) { if ( strpos( $slug, $val['slug'] ) !== false ) { return false; } } return true; } } /** * Fetch themes from wporg api. * * @param string $author The author name. * * @return array The list of themes. */ function get_themes_from_wporg( $author ) { $products = wp_remote_get( 'https://api.wordpress.org/themes/info/1.1/?action=query_themes&request[author]=' . $author . '&request[per_page]=30&request[fields][active_installs]=true' ); $products = json_decode( wp_remote_retrieve_body( $products ) ); if ( is_object( $products ) ) { $products = isset( $products->themes ) ? $products->themes : array(); } else { $products = array(); } return $products; } /** * Fetch plugin from wporg api. * * @param string $author The author slug. * * @return array The list of plugins for the selected author. */ function get_plugins_from_wporg( $author ) { $products = wp_remote_get( 'https://api.wordpress.org/plugins/info/1.1/?action=query_plugins&request[author]=' . $author . '&request[author]=codeinwp&request[per_page]=20&request[fields][active_installs]=true' ); $products = json_decode( wp_remote_retrieve_body( $products ) ); if ( is_object( $products ) ) { $products = isset( $products->plugins ) ? $products->plugins : array(); } else { $products = array(); } return $products; } /** * Fetch products from the recomended section. * * @return array|mixed The list of products to use in recomended section. */ function get_product_from_api() { if ( false === ( $products = get_transient( 'themeisle_sdk_products' ) ) ) { $products = array(); $themeisle_themes = $this->get_themes_from_wporg( 'themeisle' ); $codeinwp_themes = $this->get_themes_from_wporg( 'codeinwp' ); $themeisle_plugins = $this->get_plugins_from_wporg( 'themeisle' ); $codeinwp_plugins = $this->get_plugins_from_wporg( 'codeinwp' ); $all_themes = array_merge( $themeisle_themes, $codeinwp_themes ); foreach ( $all_themes as $theme ) { if ( $theme->active_installs < 4999 ) { continue; } $products[] = array( 'name' => $theme->name, 'type' => 'theme', 'slug' => $theme->slug, 'installs' => $theme->active_installs, ); } $all_plugins = array_merge( $themeisle_plugins, $codeinwp_plugins ); foreach ( $all_plugins as $plugin ) { if ( $plugin->active_installs < 5999 ) { continue; } $products[] = array( 'name' => $plugin->name, 'type' => 'plugin', 'slug' => $plugin->slug, 'installs' => $plugin->active_installs, ); } set_transient( 'themeisle_sdk_products', $products, 6 * HOUR_IN_SECONDS ); } return $products; } /** * Contact the API and fetch the recommended plugins/themes */ function recommend_plugin_or_theme() { $products = $this->get_product_from_api(); if ( ! is_array( $products ) ) { $products = array(); } $products = array_filter( $products, array( $this, 'remove_current_products' ) ); $products = array_merge( $products ); if ( count( $products ) > 1 ) { shuffle( $products ); $products = array_slice( $products, 0, 1 ); } $to_recommend = isset( $products[0] ) ? $products[0] : $products; return $to_recommend; } } endif;