PluginProbe
Parse.ly / 3.23.7
Parse.ly v3.23.7
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 3.20.2 3.20.3 All 105 releases
wp-parsely / src / rest-api / stats / trait-post-data.php

trait-post-data.php in Parse.ly 3.23.7, at src/rest-api/stats/trait-post-data.php

176 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Post data trait, that provides methods to handle post data and the itm_source
4 * parameter
5 *
6 * @package Parsely
7 * @since 3.17.0
8 */
9
10 declare(strict_types=1);
11
12 namespace Parsely\REST_API\Stats;
13
14 use Parsely\Parsely;
15 use Parsely\Utils\Utils;
16 use stdClass;
17 use WP_REST_Request;
18
19 /**
20 * Post data trait, that provides methods to handle post data and the itm_source
21 * parameter.
22 *
23 * @since 3.17.0
24 */
25 trait Post_Data_Trait {
26 /**
27 * The itm_source of the post's URL.
28 *
29 * @since 3.17.0
30 *
31 * @var string $itm_source
32 */
33 private $itm_source;
34
35 /**
36 * Sets the itm_source value.
37 *
38 * @since 3.17.0
39 *
40 * @param string $source The source of the item.
41 */
42 private function set_itm_source( string $source ): void {
43 $this->itm_source = $source;
44 }
45
46 /**
47 * Sets the itm_source value from the request, if it exists.
48 *
49 * @since 3.17.0
50 *
51 * @param WP_REST_Request $request The request object.
52 */
53 private function set_itm_source_from_request( WP_REST_Request $request ): void {
54 $source = $request->get_param( 'itm_source' );
55 if ( null !== $source ) {
56 $this->set_itm_source( $source );
57 }
58 }
59
60 /**
61 * Returns the itm_source parameter arguments, to be used in the REST API
62 * route registration.
63 *
64 * @since 3.17.0
65 *
66 * @return array<string, mixed>
67 */
68 private function get_itm_source_param_args(): array {
69 return array(
70 'itm_source' => array(
71 'description' => __( 'The source of the item.', 'wp-parsely' ),
72 'type' => 'string',
73 'required' => false,
74 ),
75 );
76 }
77
78 /**
79 * Extracts the post data from the passed object.
80 *
81 * Should only be used with endpoints that return post data.
82 *
83 * @since 3.10.0
84 * @since 3.17.0 Moved from the `Base_API_Proxy` class.
85 *
86 * @param array<mixed> $item The object to extract the data from.
87 * @return array<string, mixed> The extracted data.
88 */
89 protected function extract_post_data( array $item ): array {
90 $data = array();
91
92 if ( isset( $item['author'] ) ) {
93 $data['author'] = $item['author'];
94 }
95
96 if ( isset( $item['metrics']['views'] ) ) {
97 $data['views'] = number_format_i18n( $item['metrics']['views'] );
98 }
99
100 if ( isset( $item['metrics']['visitors'] ) ) {
101 $data['visitors'] = number_format_i18n( $item['metrics']['visitors'] );
102 }
103
104 if ( isset( $item['metrics']['recirculation_rate'] ) ) {
105 $data['recirculationRate'] = number_format_i18n( $item['metrics']['recirculation_rate'], 3 );
106 }
107
108 // The avg_engaged metric can be in different locations depending on the
109 // endpoint and passed sort/url parameters.
110 $avg_engaged = $item['metrics']['avg_engaged'] ?? $item['avg_engaged'] ?? null;
111 if ( null !== $avg_engaged ) {
112 $data['avgEngaged'] = Utils::get_formatted_duration( (float) $avg_engaged );
113 }
114
115 if ( isset( $item['pub_date'] ) ) {
116 $data['date'] = wp_date( Utils::get_date_format(), strtotime( $item['pub_date'] ) );
117 }
118
119 if ( isset( $item['title'] ) ) {
120 $data['title'] = $item['title'];
121 }
122
123 // Handle the campaign metrics, if they exist.
124 if ( isset( $item['campaign_metrics'] ) && is_array( $item['campaign_metrics'] ) ) {
125 $data['campaign'] = array();
126
127 if ( isset( $item['campaign_metrics']['views'] ) ) {
128 $data['campaign']['views'] = number_format_i18n( $item['campaign_metrics']['views'] );
129 }
130
131 if ( isset( $item['campaign_metrics']['visitors'] ) ) {
132 $data['campaign']['visitors'] = number_format_i18n( $item['campaign_metrics']['visitors'] );
133 }
134
135 if ( isset( $item['campaign_metrics']['recirculation_rate'] ) ) {
136 $data['campaign']['recirculationRate'] = number_format_i18n( $item['campaign_metrics']['recirculation_rate'], 3 );
137 }
138
139 if ( isset( $item['campaign_metrics']['avg_engaged'] ) ) {
140 $data['campaign']['avgEngaged'] = Utils::get_formatted_duration( (float) $item['campaign_metrics']['avg_engaged'] );
141 }
142 }
143
144 if ( isset( $item['url'] ) ) {
145 $site_id = $this->parsely->get_site_id();
146 $post_id = Utils::get_post_id_by_url( $item['url'] );
147 $post_url = Parsely::get_url_with_itm_source( $item['url'], null );
148
149 // If we have a post ID, update the post canonical URL.
150 if ( 0 !== $post_id ) {
151 Parsely::set_canonical_url( $post_id, $post_url );
152 }
153
154 if ( Utils::parsely_is_https_supported() ) {
155 $post_url = str_replace( 'http://', 'https://', $post_url );
156 }
157
158 $data['rawUrl'] = $post_url;
159 $data['dashUrl'] = Parsely::get_dash_url( $site_id, $post_url );
160 $data['id'] = Parsely::get_url_with_itm_source( $post_url, null ); // Unique.
161 $data['postId'] = $post_id; // Might not be unique.
162 $data['url'] = Parsely::get_url_with_itm_source( $post_url, $this->itm_source );
163
164 // Set thumbnail URL, falling back to the Parse.ly thumbnail if needed.
165 $thumbnail_url = get_the_post_thumbnail_url( $post_id, 'thumbnail' );
166 if ( false !== $thumbnail_url ) {
167 $data['thumbnailUrl'] = $thumbnail_url;
168 } elseif ( isset( $item['thumb_url_medium'] ) ) {
169 $data['thumbnailUrl'] = $item['thumb_url_medium'];
170 }
171 }
172
173 return $data;
174 }
175 }
176