Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
48.00% covered (danger)
48.00%
36 / 75
56.25% covered (warning)
56.25%
9 / 16
CRAP
0.00% covered (danger)
0.00%
0 / 1
Videopress
47.95% covered (danger)
47.95%
35 / 73
56.25% covered (warning)
56.25%
9 / 16
138.59
0.00% covered (danger)
0.00%
0 / 1
 register_endpoints
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 permissions_callback
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_name
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_title
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_description
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_long_description
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_features
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 get_pricing_for_ui
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 get_post_checkout_url
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_wpcom_product_slug
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_post_activation_url
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_manage_url
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 get_paid_plan_product_slugs
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 is_upgradable_by_bundle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_videopress_stats
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 1
132
 get_site_videopress_data
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * VideoPress product
4 *
5 * @package my-jetpack
6 */
7
8namespace Automattic\Jetpack\My_Jetpack\Products;
9
10use Automattic\Jetpack\My_Jetpack\Hybrid_Product;
11use Automattic\Jetpack\My_Jetpack\Wpcom_Products;
12use Automattic\Jetpack\VideoPress\Stats as VideoPress_Stats;
13use WP_Error;
14use WP_REST_Response;
15
16if ( ! defined( 'ABSPATH' ) ) {
17    exit( 0 );
18}
19
20/**
21 * Class responsible for handling the VideoPress product
22 */
23class Videopress extends Hybrid_Product {
24    private const VIDEOPRESS_STATS_KEY  = 'my-jetpack-videopress-stats';
25    private const VIDEOPRESS_PERIOD_KEY = 'my-jetpack-videopress-period';
26
27    /**
28     * The product slug
29     *
30     * @var string
31     */
32    public static $slug = 'videopress';
33
34    /**
35     * The Jetpack module name
36     *
37     * @var string
38     */
39    public static $module_name = 'videopress';
40
41    /**
42     * The slug of the plugin associated with this product.
43     *
44     * @var string
45     */
46    public static $plugin_slug = 'jetpack-videopress';
47
48    /**
49     * The category of the product
50     *
51     * @var string
52     */
53    public static $category = 'performance';
54
55    /**
56     * The filename (id) of the plugin associated with this product.
57     *
58     * @var string
59     */
60    public static $plugin_filename = array(
61        'jetpack-videopress/jetpack-videopress.php',
62        'videopress/jetpack-videopress.php',
63        'jetpack-videopress-dev/jetpack-videopress.php',
64    );
65
66    /**
67     * Search only requires site connection
68     *
69     * @var boolean
70     */
71    public static $requires_user_connection = true;
72
73    /**
74     * VideoPress has a standalone plugin
75     *
76     * @var bool
77     */
78    public static $has_standalone_plugin = true;
79
80    /**
81     * Whether this product has a free offering
82     *
83     * @var bool
84     */
85    public static $has_free_offering = true;
86
87    /**
88     * The feature slug that identifies the paid plan
89     *
90     * @var string
91     */
92    public static $feature_identifying_paid_plan = 'videopress';
93
94        /**
95         * Setup VideoPress REST API endpoints
96         *
97         * @return void
98         */
99    public static function register_endpoints(): void {
100        parent::register_endpoints();
101        // Get Jetpack VideoPress data.
102        register_rest_route(
103            'my-jetpack/v1',
104            '/site/videopress/data',
105            array(
106                'methods'             => \WP_REST_Server::READABLE,
107                'callback'            => __CLASS__ . '::get_site_videopress_data',
108                'permission_callback' => __CLASS__ . '::permissions_callback',
109            )
110        );
111    }
112
113    /**
114     * Checks if the user has the correct permissions
115     */
116    public static function permissions_callback() {
117        return current_user_can( 'edit_posts' );
118    }
119
120    /**
121     * Get the product name
122     *
123     * @return string
124     */
125    public static function get_name() {
126        return 'VideoPress';
127    }
128
129    /**
130     * Get the product title
131     *
132     * @return string
133     */
134    public static function get_title() {
135        return 'Jetpack VideoPress';
136    }
137
138    /**
139     * Get the internationalized product description
140     *
141     * @return string
142     */
143    public static function get_description() {
144        return __( 'Powerful and flexible video hosting.', 'jetpack-my-jetpack' );
145    }
146
147    /**
148     * Get the internationalized product long description
149     *
150     * @return string
151     */
152    public static function get_long_description() {
153        return __( 'Stunning-quality, ad-free video in the WordPress Editor', 'jetpack-my-jetpack' );
154    }
155
156    /**
157     * Get the internationalized features list
158     *
159     * @return array Boost features list
160     */
161    public static function get_features() {
162        return array(
163            _x( '1TB of storage', 'VideoPress Product Feature', 'jetpack-my-jetpack' ),
164            _x( 'Built into WordPress editor', 'VideoPress Product Feature', 'jetpack-my-jetpack' ),
165            _x( 'Ad-free and customizable player', 'VideoPress Product Feature', 'jetpack-my-jetpack' ),
166            _x( 'Unlimited users', 'VideoPress Product Feature', 'jetpack-my-jetpack' ),
167        );
168    }
169
170    /**
171     * Get the product princing details
172     *
173     * @return array Pricing details
174     */
175    public static function get_pricing_for_ui() {
176        return array_merge(
177            array(
178                'available'          => true,
179                'wpcom_product_slug' => static::get_wpcom_product_slug(),
180            ),
181            Wpcom_Products::get_product_pricing( static::get_wpcom_product_slug() )
182        );
183    }
184
185    /**
186     * Get the URL the user is taken after purchasing the product through the checkout
187     *
188     * @return ?string
189     */
190    public static function get_post_checkout_url() {
191        return self::get_manage_url();
192    }
193
194    /**
195     * Get the WPCOM product slug used to make the purchase
196     *
197     * @return ?string
198     */
199    public static function get_wpcom_product_slug() {
200        return 'jetpack_videopress';
201    }
202
203    /**
204     * Get the URL the user is taken after activating the product
205     *
206     * @return ?string
207     */
208    public static function get_post_activation_url() {
209        return ''; // stay in My Jetpack page.
210    }
211
212    /**
213     * Get the URL where the user manages the product
214     *
215     * @return ?string
216     */
217    public static function get_manage_url() {
218        if ( method_exists( 'Automattic\Jetpack\VideoPress\Initializer', 'should_initialize_admin_ui' ) && \Automattic\Jetpack\VideoPress\Initializer::should_initialize_admin_ui() ) {
219            return \Automattic\Jetpack\VideoPress\Admin_UI::get_admin_page_url();
220        } else {
221            return admin_url( 'admin.php?page=jetpack#/settings?term=videopress' );
222        }
223    }
224
225    /**
226     * Get the product-slugs of the paid plans for this product (not including bundles)
227     *
228     * @return array
229     */
230    public static function get_paid_plan_product_slugs() {
231        return array(
232            'jetpack_videopress',
233            'jetpack_videopress_monthly',
234            'jetpack_videopress_bi_yearly',
235        );
236    }
237
238    /**
239     * Return product bundles list
240     * that supports the product.
241     *
242     * @return boolean|array Products bundle list.
243     */
244    public static function is_upgradable_by_bundle() {
245        return array( 'complete' );
246    }
247
248    /**
249     * Get stats for VideoPress
250     *
251     * @return array|WP_Error
252     */
253    private static function get_videopress_stats() {
254        $video_count = array_sum( (array) wp_count_attachments( 'video' ) );
255
256        if ( ! class_exists( 'Automattic\Jetpack\VideoPress\Stats' ) ) {
257            return array(
258                'videoCount' => $video_count,
259            );
260        }
261
262        $featured_stats = get_transient( self::VIDEOPRESS_STATS_KEY );
263
264        if ( $featured_stats ) {
265            return array(
266                'featuredStats' => $featured_stats,
267                'videoCount'    => $video_count,
268            );
269        }
270
271        $stats_period     = get_transient( self::VIDEOPRESS_PERIOD_KEY );
272        $videopress_stats = new VideoPress_Stats();
273
274        // If the stats period exists, retrieve that information without checking the view count.
275        // If it does not, check the view count of monthly stats and determine if we want to show yearly or monthly stats.
276        if ( $stats_period ) {
277            if ( $stats_period === 'day' ) {
278                $featured_stats = $videopress_stats->get_featured_stats( 60, 'day' );
279            } else {
280                $featured_stats = $videopress_stats->get_featured_stats( 2, 'year' );
281            }
282        } else {
283            $featured_stats = $videopress_stats->get_featured_stats( 60, 'day' );
284
285            if (
286                ! is_wp_error( $featured_stats ) &&
287                $featured_stats &&
288                ( $featured_stats['data']['views']['current'] < 500 || $featured_stats['data']['views']['previous'] < 500 )
289            ) {
290                $featured_stats = $videopress_stats->get_featured_stats( 2, 'year' );
291            }
292        }
293
294        if ( is_wp_error( $featured_stats ) || ! $featured_stats ) {
295            return array(
296                'videoCount' => $video_count,
297            );
298        }
299
300        set_transient( self::VIDEOPRESS_PERIOD_KEY, $featured_stats['period'], WEEK_IN_SECONDS );
301        set_transient( self::VIDEOPRESS_STATS_KEY, $featured_stats, DAY_IN_SECONDS );
302
303        return array(
304            'featuredStats' => $featured_stats,
305            'videoCount'    => $video_count,
306        );
307    }
308
309    /**
310     * Get VideoPress data for the REST API
311     *
312     * @return WP_REST_Response|WP_Error
313     */
314    public static function get_site_videopress_data() {
315        $videopress_stats = self::get_videopress_stats();
316
317        return rest_ensure_response( $videopress_stats );
318    }
319}