PluginProbe
Parse.ly / 3.16.2
Parse.ly v3.16.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 3.20.2 3.20.3 All 105 releases
wp-parsely / src / Telemetry / Events / recommended-widget.php

recommended-widget.php in Parse.ly 3.16.2, at src/Telemetry/Events/recommended-widget.php

132 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Telemetry: Records events related to the Recommended Widget
4 *
5 * @package Parsely\Telemetry
6 * @since 3.12.0
7 */
8
9 declare(strict_types=1);
10
11 namespace Parsely\Telemetry;
12
13 use WP_Widget;
14
15 /**
16 * This is determined by the value passed to the `WP_Widget` constructor.
17 *
18 * @see https://github.com/Parsely/wp-parsely/blob/e9f1b8cd1a94743e068681a8106176d23857992d/src/class-parsely-recommended-widget.php#L28
19 */
20 const PARSELY_RECOMMENDED_WIDGET_BASE_ID = 'parsely_recommended_widget';
21
22 /**
23 * Records an event whenever a Recommended Widget instance gets deleted.
24 *
25 * @since 3.12.0
26 *
27 * @param string $widget_id ID of the widget marked for deletion.
28 * @param string $sidebar_id ID of the sidebar the widget was deleted from.
29 * @param string $id_base ID base for the widget.
30 * @param Telemetry_System $telemetry_system The Telemetry System to use.
31 */
32 function record_widget_deleted(
33 string $widget_id,
34 string $sidebar_id,
35 string $id_base,
36 Telemetry_System $telemetry_system
37 ): void {
38 if ( strtolower( PARSELY_RECOMMENDED_WIDGET_BASE_ID ) !== strtolower( $id_base ) ) {
39 return;
40 }
41
42 $telemetry_system->record_event(
43 'wpparsely_delete_widget',
44 array(
45 'id_base' => $id_base,
46 )
47 );
48 }
49
50 /**
51 * Records an event whenever a Recommended Widget instance gets updated.
52 *
53 * @since 3.12.0
54 *
55 * @param array<string, mixed>|false $instance The current widget instance's settings.
56 * @param array<string, mixed>|null $new_instance Array of new widget settings.
57 * @param array<string, mixed>|null $old_instance Array of old widget settings.
58 * @param WP_Widget $widget_obj The current widget instance.
59 * @param Telemetry_System $telemetry_system The Telemetry System to use.
60 * @return array<string, mixed>|false The updated widget settings.
61 */
62 function record_widget_updated(
63 $instance,
64 ?array $new_instance,
65 ?array $old_instance,
66 WP_Widget $widget_obj,
67 Telemetry_System $telemetry_system
68 ) {
69 if ( ! is_array( $instance ) ) {
70 return $instance;
71 }
72
73 $id_base = $widget_obj->id_base;
74 if ( PARSELY_RECOMMENDED_WIDGET_BASE_ID !== $id_base ) {
75 return $instance;
76 }
77
78 global $wp;
79
80 if (
81 isset( $wp->query_vars['rest_route'] ) &&
82 '/wp/v2/widget-types/parsely_recommended_widget/encode' === $wp->query_vars['rest_route']
83 ) {
84 // This is an "encode" call to preview the widget in the admin.
85 // Track this event separately.
86 $telemetry_system->record_event(
87 'wpparsely_widget_prepublish_change',
88 compact( 'id_base' )
89 );
90 return $instance;
91 }
92
93 if ( null === $old_instance ) {
94 // If there is no old instance, all keys are updated. We have this
95 // shortcut so we don't have to do `array_keys` of null, thus raising a
96 // warning.
97 $updated_keys = array_keys( $instance );
98 } else {
99 $all_keys = array_unique(
100 array_merge( array_keys( $old_instance ), array_keys( $instance ) )
101 );
102
103 $updated_keys = array_reduce(
104 $all_keys,
105 function ( array $carry, string $key ) use ( $old_instance, $instance ) {
106 if (
107 isset( $old_instance[ $key ] ) === isset( $instance[ $key ] ) &&
108 wp_json_encode( $old_instance[ $key ] ) === wp_json_encode( $instance[ $key ] )
109 ) {
110 return $carry;
111 }
112 $carry[] = $key;
113
114 return $carry;
115 },
116 array()
117 );
118 }
119
120 if ( 0 < count( $updated_keys ) ) {
121 $telemetry_system->record_event(
122 'wpparsely_widget_updated',
123 array(
124 'updated_keys' => $updated_keys,
125 'id_base' => $id_base,
126 )
127 );
128 }
129
130 return $instance;
131 }
132