array( __CLASS__, 'render_block' ) ) ); add_shortcode( 'trackpluse_list', 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['author'] ) ? (string) $attributes['author'] : '', isset( $attributes['plugins'] ) ? (string) $attributes['plugins'] : '', isset( $attributes['themes'] ) ? (string) $attributes['themes'] : '', isset( $attributes['shown'] ) ? $attributes['shown'] : TrackPluse_Settings::DEFAULT_SHOWN, ! isset( $attributes['showSummary'] ) || (bool) $attributes['showSummary'], ! 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 yet. Choose a username or slugs in the block’s settings, or in Settings → TrackPluse.', 'trackpluse' ) . '

' : ''; } return '
'trackpluse-list-block' ) ) . '>' . $html . '
'; } /** * [trackpluse_list author="jordanlee" plugins="my-plugin,my-other-plugin" themes="" shown="5" summary="yes" link="no"] * * @param array|string $atts The shortcode's attributes. * @return string */ public static function shortcode( $atts ) { $atts = shortcode_atts( array( 'author' => '', 'plugins' => '', 'themes' => '', 'shown' => TrackPluse_Settings::DEFAULT_SHOWN, 'summary' => 'yes', 'link' => 'no', ), $atts, 'trackpluse_list' ); $html = self::render( $atts['author'], $atts['plugins'], $atts['themes'], $atts['shown'], 'no' !== $atts['summary'], 'yes' === $atts['link'] ); return '' === $html ? '' : '
' . $html . '
'; } /** * The list's HTML, or '' when there's nothing to show. * * @param string $author WordPress.org username, or ''. * @param string $plugins Plugin slugs, separated by commas, spaces or lines. * @param string $themes Theme slugs, the same way. * @param mixed $shown How many listings to show. * @param bool $summary Whether to show the totals. * @param bool $link Whether to link to TrackWP under the list. * @return string */ private static function render( $author, $plugins, $themes, $shown, $summary, $link ) { $author = TrackPluse_Settings::username( (string) $author ); $plugins = TrackPluse_Settings::slugs( (string) $plugins ); $themes = TrackPluse_Settings::slugs( (string) $themes ); // Nothing named here: what Settings → TrackPluse chose. if ( '' === $author && array() === $plugins && array() === $themes ) { $settings = TrackPluse_Settings::get(); $author = $settings['author']; $plugins = $settings['plugins']; $themes = $settings['themes']; } if ( '' === $author && array() === $plugins && array() === $themes ) { return ''; } // A page shouldn't wait long on TrackWP; the figures are kept six hours after. $stats = TrackPluse_Api::stats( $author, $plugins, $themes, 5 ); if ( is_wp_error( $stats ) || array() === $stats['data'] ) { return ''; } ob_start(); if ( $summary ) { TrackPluse_Listing::summary( $stats['data'] ); } echo ''; if ( $link ) { $url = '' !== $author ? TrackPluse_Api::base() . '/authors/' . rawurlencode( $author ) : TrackPluse_Api::base() . '/'; echo '

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

'; } return (string) ob_get_clean(); } }