PluginProbe
Parse.ly / 3.24.2
Parse.ly v3.24.2
3.24.3 3.24.2 3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 All 107 releases
wp-parsely / src / content-helper / dashboard-widget / provider.ts

provider.ts in Parse.ly 3.24.2, at src/content-helper/dashboard-widget/provider.ts

98 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * WordPress dependencies
3 */
4 import { __ } from '@wordpress/i18n';
5 import { addQueryArgs } from '@wordpress/url';
6
7 /**
8 * Internal dependencies
9 */
10 import {
11 ContentHelperError,
12 ContentHelperErrorCode,
13 } from '../common/content-helper-error';
14 import { BaseProvider } from '../common/providers/base-provider';
15 import { TopPostsSettings } from '../common/settings';
16 import { getApiPeriodParams } from '../common/utils/api';
17 import { PostData } from '../common/utils/post';
18
19 export const TOP_POSTS_DEFAULT_LIMIT = 5;
20
21 export class DashboardWidgetProvider extends BaseProvider {
22 /**
23 * The singleton instance of the DashboardWidgetProvider.
24 *
25 * @since 3.15.0
26 */
27 private static instance: DashboardWidgetProvider;
28
29 /**
30 * Returns the singleton instance of the DashboardWidgetProvider.
31 *
32 * @since 3.15.0
33 *
34 * @return {DashboardWidgetProvider} The singleton instance.
35 */
36 public static getInstance(): DashboardWidgetProvider {
37 if ( ! this.instance ) {
38 this.instance = new DashboardWidgetProvider();
39 }
40
41 return this.instance;
42 }
43
44 /**
45 * Returns the site's top posts.
46 *
47 * @param {TopPostsSettings} settings The settings to use.
48 * @param {number} page The page to fetch, defaults to the first page.
49 *
50 * @return {Promise<Array<PostData>>} Object containing message and posts.
51 */
52 public async getTopPosts(
53 settings: TopPostsSettings, page: number = 1
54 ): Promise<PostData[]> {
55 let data: PostData[] = [];
56
57 try {
58 data = await this.fetchTopPostsFromWpEndpoint( settings, page );
59 } catch ( contentHelperError ) {
60 return Promise.reject( contentHelperError );
61 }
62
63 if ( 0 === data.length ) {
64 return Promise.reject( new ContentHelperError(
65 __( 'No Top Posts data is available.', 'wp-parsely' ),
66 ContentHelperErrorCode.ParselyApiReturnedNoData,
67 ''
68 ) );
69 }
70
71 return data;
72 }
73
74 /**
75 * Fetches the site's top posts data from the WordPress REST API.
76 *
77 * @param {TopPostsSettings} settings The settings to use.
78 * @param {number} page The page to fetch.
79 *
80 * @return {Promise<Array<PostData>>} Array of fetched posts.
81 */
82 private async fetchTopPostsFromWpEndpoint(
83 settings: TopPostsSettings, page: number
84 ): Promise<PostData[]> {
85 const response = this.fetch<PostData[]>( {
86 path: addQueryArgs( '/wp-parsely/v2/stats/posts/', {
87 limit: TOP_POSTS_DEFAULT_LIMIT,
88 ...getApiPeriodParams( settings.Period ),
89 sort: settings.Metric,
90 page,
91 itm_source: 'wp-parsely-content-helper',
92 } ),
93 } );
94
95 return response ?? [];
96 }
97 }
98