| @@ -100,8 +100,12 @@ | ||
| 100 | 100 | if ( isset( $item['metrics']['visitors'] ) ) { |
| 101 | 101 | $data['visitors'] = number_format_i18n( $item['metrics']['visitors'] ); |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | + if ( isset( $item['metrics']['recirculation_rate'] ) ) { | |
| 105 | + $data['recirculationRate'] = number_format_i18n( $item['metrics']['recirculation_rate'], 3 ); | |
| 106 | + } | |
| 107 | + | |
| 104 | 108 | // The avg_engaged metric can be in different locations depending on the |
| 105 | 109 | // endpoint and passed sort/url parameters. |
| 106 | 110 | $avg_engaged = $item['metrics']['avg_engaged'] ?? $item['avg_engaged'] ?? null; |
| 107 | 111 | if ( null !== $avg_engaged ) { |
| @@ -115,14 +119,39 @@ | ||
| 115 | 119 | if ( isset( $item['title'] ) ) { |
| 116 | 120 | $data['title'] = $item['title']; |
| 117 | 121 | } |
| 118 | 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 | + | |
| 119 | 144 | if ( isset( $item['url'] ) ) { |
| 120 | - $site_id = $this->parsely->get_site_id(); | |
| 121 | - // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.url_to_postid_url_to_postid | |
| 122 | - $post_id = url_to_postid( $item['url'] ); // 0 if the post cannot be found. | |
| 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 ); | |
| 123 | 148 | |
| 124 | - $post_url = Parsely::get_url_with_itm_source( $item['url'], null ); | |
| 149 | + // Only updates the canonical URL of posts the current user can edit. | |
| 150 | + if ( 0 !== $post_id && current_user_can( 'edit_post', $post_id ) ) { | |
| 151 | + Parsely::set_canonical_url( $post_id, $post_url ); | |
| 152 | + } | |
| 153 | + | |
| 125 | 154 | if ( Utils::parsely_is_https_supported() ) { |
| 126 | 155 | $post_url = str_replace( 'http://', 'https://', $post_url ); |
| 127 | 156 | } |
| 128 | 157 | |