block_slug` property * * @return mixed */ protected function set_block_slug() { $this->block_slug = 'plugin-cards'; } /** * Set the attributes required on the server side. * * @return mixed */ protected function set_attributes() { $this->attributes = array( 'slug' => array( 'type' => 'string', ), 'className' => array( 'type' => 'string', ), ); } /** * Block render function for server-side. * * This method will pe passed to the render_callback parameter and it will output * the server side output of the block. * * @param array $attributes Blocks attrs. * * @return mixed|string */ protected function render( $attributes ) { if ( empty( $attributes['slug'] ) ) { return; } $results = $this->search( $attributes['slug'] ); if ( ! is_wp_error( $results['data'] ) ) { $results = $results['data']; $icon = ''; if ( isset( $results->icons['svg'] ) ) { $icon = $results->icons['svg']; } if ( isset( $results->icons['2x'] ) ) { $icon = $results->icons['2x']; } if ( isset( $results->icons['1x'] ) ) { $icon = $results->icons['1x']; } if ( isset( $results->icons['default'] ) ) { $icon = $results->icons['default']; } $class = 'wp-block-themeisle-blocks-plugin-cards'; if ( isset( $attributes['className'] ) ) { $class .= ' ' . esc_attr( $attributes['className'] ); } $markup = '

' . esc_html( $results->name ) . '

' . $results->author . '
' . $this->get_ratings( $results->rating ) . '
' . esc_html( $results->short_description ) . '
' . __( 'Plugin Stats', 'themeisle-companion' ) . '
' . number_format( $results->active_installs ) . '+ ' . __( 'active installs', 'themeisle-companion' ) . '
' . floatval( $results->version ) . '+ ' . __( 'version', 'themeisle-companion' ) . '
' . floatval( $results->tested ) . '+ ' . __( 'tested up to', 'themeisle-companion' ) . '
'; return $markup; } } /** * Search WordPress Plugin * * Search WordPress plugin using WordPress.org API. * * @param mixed $request Rest request. * * @return mixed */ protected function search( $request ) { $return = array( 'success' => false, 'data' => esc_html__( 'Something went wrong', 'themeisle-companion' ), ); $slug = $request; require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; $request = array( 'per_page' => 12, 'slug' => $slug, 'fields' => array( 'active_installs' => true, 'added' => false, 'donate_link' => false, 'downloadlink' => true, 'homepage' => true, 'icons' => true, 'last_updated' => false, 'requires' => true, 'requires_php' => false, 'screenshots' => false, 'short_description' => true, 'slug' => false, 'sections' => false, 'requires' => false, 'rating' => true, 'ratings' => false, ), ); $results = plugins_api( 'plugin_information', $request ); if ( is_wp_error( $request ) ) { $return['data'] = 'error'; return $return; } $return['success'] = true; // Get data from API. $return['data'] = $results; return $return; } /** * Get Rating Stars * * Get 0-5 star rating from rating score. * * @param string $rating Rating value. * * @return mixed|string */ protected function get_ratings( $rating ) { $rating = round( $rating / 10, 0 ) / 2; $full_stars = floor( $rating ); $half_stars = ceil( $rating - $full_stars ); $empty_stars = 5 - $full_stars - $half_stars; $output = str_repeat( '', $full_stars ); $output .= str_repeat( '', $half_stars ); $output .= str_repeat( '', $empty_stars ); return $output; } }