array( __CLASS__, 'render_block' ) ) ); add_shortcode( 'trackpluse_stats', array( __CLASS__, 'shortcode' ) ); } /** * The block on the page. * * @param array $attributes The block's settings. * @return string */ public static function render_block( $attributes ) { $html = self::render( isset( $attributes['slug'] ) ? (string) $attributes['slug'] : '', isset( $attributes['type'] ) ? (string) $attributes['type'] : 'plugin', isset( $attributes['fields'] ) ? (array) $attributes['fields'] : self::DEFAULT_FIELDS, ! isset( $attributes['showName'] ) || (bool) $attributes['showName'], ! empty( $attributes['link'] ) ); if ( '' === $html ) { // Only the editor's preview explains why there's nothing; visitors see nothing. return wp_is_serving_rest_request() ? '

' . esc_html__( 'No WordPress.org figures for this slug yet. Check the slug in the block’s settings.', 'trackpluse' ) . '

' : ''; } return '
'trackpluse-stats' ) ) . '>' . $html . '
'; } /** * [trackpluse_stats slug="my-plugin" type="plugin" fields="installs,rating" name="yes" link="no"] * * @param array|string $atts The shortcode's attributes. * @return string */ public static function shortcode( $atts ) { $atts = shortcode_atts( array( 'slug' => '', 'type' => 'plugin', 'fields' => implode( ',', self::DEFAULT_FIELDS ), 'name' => 'yes', 'link' => 'no', ), $atts, 'trackpluse_stats' ); $html = self::render( (string) $atts['slug'], (string) $atts['type'], array_map( 'trim', explode( ',', (string) $atts['fields'] ) ), 'no' !== strtolower( (string) $atts['name'] ), 'yes' === strtolower( (string) $atts['link'] ) ); if ( '' === $html ) { return ''; } wp_enqueue_style( 'trackpluse-stats-style' ); return '
' . $html . '
'; } /** * The figures as HTML, or '' when there are none. * * @param string $slug The listing's slug. * @param string $type 'plugin' or 'theme'. * @param string[] $fields Figures to show. * @param bool $show_name Whether to show the listing's name. * @param bool $link Whether to link to the listing's TrackWP page. * @return string */ public static function render( $slug, $type, $fields, $show_name, $link ) { $slug = TrackPluse_Settings::slugs( array( $slug ) ); $type = 'theme' === $type ? 'theme' : 'plugin'; if ( array() === $slug ) { return ''; } // A visitor's page view may fetch: wait less than the dashboard does. $stats = 'theme' === $type ? TrackPluse_Api::stats( '', array(), $slug, 5 ) : TrackPluse_Api::stats( '', $slug, array(), 5 ); if ( is_wp_error( $stats ) ) { return ''; } $listing = null; foreach ( $stats['data'] as $row ) { if ( isset( $row['slug'], $row['type'] ) && $row['slug'] === $slug[0] && $row['type'] === $type ) { $listing = $row; } } if ( null === $listing ) { return ''; } $items = array(); foreach ( array_intersect( self::FIELDS, $fields ) as $field ) { $item = self::item( $field, $listing ); if ( null !== $item ) { $items[] = '
  • ' . esc_html( $item[0] ) . ' ' . esc_html( $item[1] ) . '
  • '; } } if ( array() === $items ) { return ''; } $name = isset( $listing['name'] ) ? wp_strip_all_tags( (string) $listing['name'] ) : ''; $html = $show_name && '' !== $name ? '

    ' . esc_html( $name ) . '

    ' : ''; $html .= ''; if ( $link && ! empty( $listing['url'] ) ) { $html .= '

    ' . esc_html__( 'Live stats by TrackWP', 'trackpluse' ) . '

    '; } return $html; } /** * One figure as [value, label], or null without data for it. * * @param string $field The figure. * @param array $listing The listing's figures. * @return string[]|null */ private static function item( $field, $listing ) { switch ( $field ) { case 'installs': return isset( $listing['active_installs'] ) ? array( TrackPluse_Format::installs( $listing['active_installs'] ), __( 'active installs', 'trackpluse' ) ) : null; case 'downloads': return ! empty( $listing['daily_downloads'] ) ? array( number_format_i18n( (int) $listing['daily_downloads'] ), __( 'downloads a day', 'trackpluse' ) ) : null; case 'downloaded': return ! empty( $listing['downloaded'] ) ? array( TrackPluse_Format::count( $listing['downloaded'] ), __( 'downloads in all', 'trackpluse' ) ) : null; case 'rating': if ( empty( $listing['num_ratings'] ) ) { return null; } return array( /* translators: %s: rating out of 5, e.g. 4.8. */ sprintf( __( '%s ★', 'trackpluse' ), number_format_i18n( (float) $listing['rating'], 1 ) ), /* translators: %s: number of ratings. */ sprintf( _n( 'from %s rating', 'from %s ratings', (int) $listing['num_ratings'], 'trackpluse' ), number_format_i18n( (int) $listing['num_ratings'] ) ), ); case 'version': return ! empty( $listing['version'] ) ? array( (string) $listing['version'], __( 'current version', 'trackpluse' ) ) : null; case 'tested': return ! empty( $listing['tested'] ) ? array( (string) $listing['tested'], __( 'tested up to WordPress', 'trackpluse' ) ) : null; case 'updated': $ago = TrackPluse_Format::ago( isset( $listing['last_updated'] ) ? $listing['last_updated'] : null ); return '' !== $ago ? array( $ago, __( 'last updated', 'trackpluse' ) ) : null; case 'rank': return ! empty( $listing['rank'] ) ? array( '#' . number_format_i18n( (int) $listing['rank'] ), 'theme' === $listing['type'] ? __( 'most popular theme on WordPress.org', 'trackpluse' ) : __( 'most popular plugin on WordPress.org', 'trackpluse' ), ) : null; } return null; } }