+<?php\r
+/*\r
+Plugin Name: danixland WPOrg Stats\r
+Plugin URI: https://danix.xyz/?p=3745\r
+Description: A simple plugin that shows useful stats about your plugins hosted on WordPress.org\r
+Version: 0.1\r
+Author: Danilo 'danix' Macri\r
+Author URI: https://danix.xyz\r
+Text Domain: dnxwporg\r
+\r
+This program is distributed in the hope that it will be useful,\r
+but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\r
+*/\r
+\r
+\r
+/**\r
+ * Add plugin i18n domain: dnxwporg\r
+ * @since 0.1\r
+ */\r
+load_plugin_textdomain('dnxwporg', plugins_url() . '/danixland-wporg-stats/i18n/', 'danixland-wporg-stats/i18n/');\r
+\r
+/**\r
+ * Function that installs our widget options\r
+ * @since 0.1\r
+ */\r
+function dnxwporg_options_set() {\r
+ update_option( 'dnxwporg_author', 'danixland', '', 'yes' );\r
+ update_option( 'dnxwporg_plugins_stats', '', '', 'yes' );\r
+ update_option( 'dnxwporg_description', true, '', 'yes' );\r
+ update_option( 'dnxwporg_desc_content', '', '', 'yes' );\r
+ update_option( 'dnxwporg_linkto', true, '', 'yes' );\r
+ update_option( 'dnxwporg_use_icons', true, '', 'yes' );\r
+}\r
+\r
+/**\r
+ * Function that deletes our widget options\r
+ * @since 0.1\r
+ */\r
+function dnxwporg_options_unset() {\r
+ delete_option( 'dnxwporg_author' );\r
+ delete_option( 'dnxwporg_plugins_stats' );\r
+ delete_option( 'dnxwporg_description' );\r
+ delete_option( 'dnxwporg_desc_content' );\r
+ delete_option( 'dnxwporg_linkto' );\r
+ delete_option( 'dnxwporg_use_icons' );\r
+}\r
+\r
+/**\r
+ * Add function on plugin activation that'll set our plugin options\r
+ * @since 0.1\r
+ */\r
+register_activation_hook( __FILE__, 'dnxwporg_options_set' );\r
+\r
+/**\r
+ * Add function on plugin deactivation that'll unset our plugin options\r
+ * @since 0.3\r
+ */\r
+register_deactivation_hook( __FILE__, 'dnxwporg_options_unset' );\r
+\r
+function dnxwporg_load_js($hook) {\r
+ // Load only on ?page=widgets.php\r
+ if($hook != 'widgets.php') {\r
+ return;\r
+ }\r
+ wp_enqueue_script( 'dnxwporg-utils-js', plugins_url('js/dnxwporg-utils.js', __FILE__), array('jquery'), false, true );\r
+}\r
+add_action( 'admin_enqueue_scripts', 'dnxwporg_load_js' );\r
+\r
+\r
+/**\r
+ * Add function to retrieve the stats from api.wordpress.org\r
+ * @param $author - the plugin author nickname\r
+ * @return array - either the array with the info about the plugins or an empty array in case of failure\r
+ * @since 0.1\r
+ */\r
+function dnxwporg_get_plugins_stats( $author ) {\r
+ $url = 'https://api.wordpress.org/plugins/info/1.0/';\r
+ $fields = array(\r
+ 'downloaded' => true,\r
+ 'rating' => false,\r
+ 'description' => false,\r
+ 'short_description' => false,\r
+ 'donate_link' => false,\r
+ 'tags' => false,\r
+ 'sections' => false,\r
+ 'homepage' => true,\r
+ 'added' => false,\r
+ 'last_updated' => false,\r
+ 'compatibility' => false,\r
+ 'tested' => false,\r
+ 'requires' => false,\r
+ 'downloadlink' => false,\r
+ );\r
+ $args = (object) array( 'author' => $author, 'fields' => $fields );\r
+ $request = array( 'action' => 'query_plugins', 'timeout' => 15, 'request' => serialize( $args) );\r
+ $response = wp_remote_post( $url, array( 'body' => $request ) );\r
+ $plugins_info = unserialize( $response['body'] );\r
+ $result = array();\r
+ $total_dl = '';\r
+ $total_avg = '';\r
+ if ( isset( $plugins_info ) ) {\r
+ # let's build our array of data to return\r
+ foreach ($plugins_info->plugins as $plugin) {\r
+ $pl_name = $plugin->slug;\r
+ $pl_dirlink = 'https://wordpress.org/plugins/' . $pl_name;\r
+ $pl_icon = 'https://ps.w.org/' . $pl_name . '/assets/icon-256x256.png';\r
+ $pl_download = $plugin->download_link;\r
+ $pl_downloaded = $plugin->downloaded;\r
+ $pl_home = $plugin->homepage;\r
+ $pl_voters = $plugin->num_ratings;\r
+ $pl_stars = $plugin->ratings;\r
+ $avg_vote = ($pl_stars[1] + $pl_stars[2] * 2 + $pl_stars[3] * 3 + $pl_stars[4] * 4 + $pl_stars[5] * 5) / $pl_voters;\r
+ $pl_avg_vote = ( ! is_int($avg_vote) ? 0 : $avg_vote );\r
+ $total_dl += $pl_downloaded;\r
+ $total_avg += $pl_avg_vote;\r
+ $result[$pl_name] = array(\r
+ 'dir_link' => $pl_dirlink,\r
+ 'dl_link' => $pl_download,\r
+ 'home_link' => $pl_home,\r
+ 'icon' => $pl_icon,\r
+ 'downloaded' => $pl_downloaded,\r
+ 'voters' => $pl_voters,\r
+ 'avg_rating' => $pl_avg_vote\r
+ );\r
+ }\r
+ $avg = $total_avg / count($plugins_info->plugins);\r
+ $result['total_plugins'] = count($plugins_info->plugins);\r
+ $result['total_downloads'] = $total_dl;\r
+ $result['average_rating'] = round( $avg, 2);\r
+ return $result;\r
+ }\r
+ return array();\r
+}\r
+\r
+/**\r
+ * This function will execute with the scheduled cron job and update the data inside the option\r
+ * @since 0.1\r
+ */\r
+function dnxwporg_save_plugins_stats() {\r
+\r
+ $author = get_option('dnxwporg_author');\r
+ $plugins = dnxwporg_get_plugins_stats( $author );\r
+ update_option( 'dnxwporg_plugins_stats', $plugins );\r
+}\r
+add_action( 'dnxwporg_save_plugins_stats', 'dnxwporg_save_plugins_stats' );\r
+\r
+/**\r
+ * we schedule the cron if it's not yet scheduled\r
+ * @since 0.1\r
+ */\r
+if ( ! wp_next_scheduled( 'dnxwporg_save_plugins_stats' ) ) {\r
+ wp_schedule_event( 1407110400, 'daily', 'dnxwporg_save_plugins_stats' ); // 1407110400 is 08 / 4 / 2014 @ 0:0:0 UTC\r
+}\r
+\r
+/**\r
+ * Add function to widgets_init that'll load our widget.\r
+ * @since 0.1\r
+ */\r
+add_action( 'widgets_init', 'danixland_wporg_widget' );\r
+\r
+/**\r
+ * Register our widget.\r
+ * 'dnx_WPOrg' is the widget class used below.\r
+ *\r
+ * @since 0.1\r
+ */\r
+function danixland_wporg_widget() {\r
+ register_widget( 'dnx_WPOrg' );\r
+}\r
+\r
+/**\r
+ * dnx_WPOrg class.\r
+ * This class handles everything that needs to be handled with the widget:\r
+ * the settings, form, display, and update. Nice!\r
+ *\r
+ * @since 0.1\r
+ */\r
+class dnx_WPOrg extends WP_Widget {\r
+\r
+\r
+ /**\r
+ * Widget setup.\r
+ */\r
+ public function __construct() {\r
+ parent::__construct(\r
+ 'danixland-wporg-stats',\r
+ __( 'danixland WP.org Stats', 'dnxwporg' ),\r
+ array(\r
+ 'customize_selective_refresh' => true,\r
+ 'description' => __('Use this widget to display statistics about your WP.org hosted plugins on your site', 'dnxwporg'),\r
+ )\r
+ );\r
+ }\r
+\r
+ /**\r
+ * Displays the setup form for the widget in the widgets page\r
+ */\r
+ public function form( $instance ) {\r
+ $defaults = array( \r
+ 'title' => __( 'WP.org Plugins Stats', 'dnxwporg' ),\r
+ 'author' => get_option( 'dnxwporg_author' ),\r
+ 'description' => get_option( 'dnxwporg_description' ),\r
+ 'desc_content' => get_option( 'dnxwporg_desc_content' ),\r
+ 'link_wporg' => get_option( 'dnxwporg_linkto' ),\r
+ 'use_icons' => get_option( 'dnxwporg_use_icons' )\r
+ );\r
+ $instance = wp_parse_args( (array) $instance, $defaults );\r
+ extract( $instance, EXTR_SKIP );\r
+ ?>\r
+\r
+ <p>\r
+ <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'dnxwporg'); ?></label>\r
+ <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $title; ?>" style="width:100%;" />\r
+ </p>\r
+ <p>\r
+ <label for="<?php echo $this->get_field_id( 'author' ); ?>"><?php _e('The username of the Plugin\'s author on WP.org', 'dnxwporg'); ?></label>\r
+ <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'author' ); ?>" name="<?php echo $this->get_field_name( 'author' ); ?>" value="<?php echo $author ?>" style="width:100%;" />\r
+ </p>\r
+ <p>\r
+ <input class="checkbox" type="checkbox" <?php checked( $description, true ); ?> id="dnxwporg_use_desc" name="<?php echo $this->get_field_name( 'description' ); ?>" />\r
+ <label for="<?php echo $this->get_field_id( 'description' ); ?>"><?php _e('Select this if you want to display a small description inside the widget.', 'dnxwporg'); ?></label>\r
+ <textarea id="dnxwporg_custom_desc" name="<?php echo $this->get_field_name( 'desc_content' ); ?>" rows="10" cols="30" <?php disabled( $description, false ); ?> /><?php echo $desc_content; ?></textarea>\r
+ <label for="<?php echo $this->get_field_id( 'desc_content' ); ?>"><?php _e('If active but empty, the standard plugin description will be used.', 'dnxwporg'); ?></label>\r
+ </p>\r
+ <p>\r
+ <input class="checkbox" type="checkbox" <?php checked( $link_wporg, true ); ?> id="<?php echo $this->get_field_id( 'link_wporg' ); ?>" name="<?php echo $this->get_field_name( 'link_wporg' ); ?>" />\r
+ <label for="<?php echo $this->get_field_id( 'link_wporg' ); ?>"><?php _e('If selected, your links will point to the WP.org plugin page, if not, the PLUGIN URI will be used instead.', 'dnxwporg'); ?></label>\r
+ </p>\r
+ <p>\r
+ <input class="checkbox" type="checkbox" <?php checked( $use_icons, true ); ?> id="<?php echo $this->get_field_id( 'use_icons' ); ?>" name="<?php echo $this->get_field_name( 'use_icons' ); ?>" />\r
+ <label for="<?php echo $this->get_field_id( 'use_icons' ); ?>"><?php _e('If selected, the widget will display the icon from the assets directory in your plugin svn folder.', 'dnxwporg'); ?></label>\r
+ </p>\r
+ <?php\r
+ }\r
+\r
+ /**\r
+ * Update the widget content\r
+ */\r
+ public function update( $new_instance, $old_instance ) {\r
+ $instance = $old_instance;\r
+\r
+ //Strip tags from title and name to remove HTML \r
+ $instance['title'] = sanitize_text_field( $new_instance['title'] );\r
+ $instance['author'] = strip_tags( $new_instance['author'] );\r
+ $instance['description'] = (bool) $new_instance['description'];\r
+ $instance['desc_content'] = sanitize_textarea_field( $new_instance['desc_content'] );\r
+ $instance['link_wporg'] = (bool) $new_instance['link_wporg'];\r
+ $instance['use_icons'] = (bool) $new_instance['use_icons'];\r
+\r
+ update_option( 'dnxwporg_author', $instance['author'] );\r
+ if ($old_instance['author'] !== $new_instance['author']) {\r
+ update_option( 'dnxwporg_plugins_stats', dnxwporg_get_plugins_stats( $instance['author'] ) );\r
+ }\r
+ update_option( 'dnxwporg_linkto', $instance['link_wporg'] );\r
+ update_option( 'dnxwporg_use_icons', $instance['use_icons'] );\r
+ update_option( 'dnxwporg_description', $instance['description'] );\r
+ update_option( 'dnxwporg_desc_content', $instance['desc_content'] );\r
+ \r
+ return $instance;\r
+ }\r
+\r
+ /**\r
+ * How to display the widget on the public side of the site.\r
+ */\r
+ public function widget( $args, $instance ) {\r
+ extract( $args );\r
+\r
+ /* Our variables from the widget settings. */\r
+ $title = apply_filters('widget_title', $instance['title'] );\r
+ $author = sanitize_user(get_option('dnxwporg_author'), true);\r
+ $description = get_option('dnxwporg_description');\r
+ $desc_content = get_option('dnxwporg_desc_content');\r
+ $linkto = get_option('dnxwporg_linkto');\r
+ $use_icons = get_option('dnxwporg_use_icons');\r
+\r
+ /* we recover the data from the option if it is set, otherwhise we call the retrieve function directly */\r
+ $option_stats = get_option('dnxwporg_plugins_stats');\r
+ $plugins = ( false === $option_stats ? dnxwporg_get_plugins_stats() : $option_stats );\r
+\r
+ /* Before widget (defined by themes). */\r
+ echo $before_widget;\r
+\r
+ /* Display the widget title if one was input (before and after defined by themes). */\r
+ if ( $title ) {\r
+ echo $before_title . $title . $after_title;\r
+ }\r
+\r
+ ?>\r
+ <div class="dnxwporg_container">\r
+ <?php if ($description) { ?>\r
+ <div class="dnxwporg_description">\r
+ <?php if (empty($desc_content)) : ?>\r
+ <p class="dnxwporg-pl-standard-desc"><?php printf( _n('%s contributed <span class="dnxwporg-pl-count">%s</span> plugin with <span class="dnxwporg-pl-dloads">%s</span> total downloads and an average rating of <span class="dnxwporg-pl-avg-rating">%s</span>', '%s contributed <span class="dnxwporg-pl-count">%s</span> plugins with <span class="dnxwporg-pl-dloads">%s</span> total downloads and an average rating of <span class="dnxwporg-pl-avg-rating">%s</span>', $plugins['total_plugins'], 'dnxwporg' ), $author, $plugins['total_plugins'], $plugins['total_downloads'], $plugins['average_rating'] ); ?></p>\r
+ <?php else : ?>\r
+ <p class="dnxwporg-pl-custom-desc"><?php echo $desc_content; ?></p>\r
+ <?php endif; ?>\r
+ </div>\r
+ <?php } ?>\r
+ <div class="dnxwporg_pl_list_container">\r
+ <ul class="dnxwporg_pl_list">\r
+ <?php foreach ($plugins as $name => $data) { \r
+ if (is_array($data) ) : ?>\r
+ <li class="dnxwporg_pl_item">\r
+ <a class="dnxwporg-pl-link" href="<?php echo ($linkto ? $data['dir_link'] : $data['home_link']) ?>">\r
+ <?php if ($use_icons) : ?>\r
+ <img class="dnxwporg-pl-link-icon" src="<?php echo $data['icon']; ?>" alt="<?php echo $name; ?>">\r
+ <?php endif; ?>\r
+ <span class="dnxwporg-pl-link-name"><?php echo $name; ?></span>\r
+ </a>\r
+ <p class="dnxwporg-pl-stats">\r
+ <span class="dnxwporg-pl-tot-dloads"><?php echo $data['downloaded']; ?></span>\r
+ <span class="dnxwporg-pl-avg-vote"><?php echo $data['avg_rating']; ?></span>\r
+ </p>\r
+ </li>\r
+ <?php endif;\r
+ } ?>\r
+ </ul>\r
+ </div>\r
+ </div>\r
+<?php\r
+ /* After widget (defined by themes). */\r
+ echo $after_widget;\r
+ }\r
+}\r
+\r
+?>\r