| 1 | <?php |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | declare(strict_types=1); |
| 17 | |
| 18 | const WPCOM_LAYOUT_GRID_USAGE_BLOCK_NAME = 'jetpack/layout-grid'; |
| 19 | const WPCOM_LAYOUT_GRID_USAGE_SEEN_OPTION = 'wpcom_layout_grid_block_seen'; |
| 20 | const WPCOM_LAYOUT_GRID_USAGE_IMPORT_TRANSIENT = 'wpcom_layout_grid_block_import_seen'; |
| 21 | const WPCOM_LAYOUT_GRID_USAGE_CRON_TRANSIENT = 'wpcom_layout_grid_block_cron_seen'; |
| 22 | const WPCOM_LAYOUT_GRID_USAGE_LOG_FEATURE = 'atomic_layout_grid_block'; |
| 23 | const WPCOM_LAYOUT_GRID_USAGE_LOG_MESSAGE = 'layout_grid_block_observed'; |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | function wpcom_layout_grid_usage_should_load() { |
| 31 | if ( ! class_exists( '\Automattic\Jetpack\Status\Host' ) ) { |
| 32 | return false; |
| 33 | } |
| 34 | return ( new \Automattic\Jetpack\Status\Host() )->is_woa_site(); |
| 35 | } |
| 36 | |
| 37 | if ( wpcom_layout_grid_usage_should_load() ) { |
| 38 | add_action( 'wp_after_insert_post', 'wpcom_layout_grid_usage_react_to_post_insert', 10, 4 ); |
| 39 | add_action( 'add_option_widget_block', 'wpcom_layout_grid_usage_react_to_widget_block_added', 10, 2 ); |
| 40 | add_action( 'update_option_widget_block', 'wpcom_layout_grid_usage_react_to_widget_block_updated', 10, 2 ); |
| 41 | add_filter( 'render_block_' . WPCOM_LAYOUT_GRID_USAGE_BLOCK_NAME, 'wpcom_layout_grid_usage_react_to_block_render', 10, 2 ); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | function wpcom_layout_grid_usage_react_to_post_insert( $post_id, $post, $update, $post_before ) { |
| 55 | unset( $update ); |
| 56 | |
| 57 | |
| 58 | if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) { |
| 59 | return; |
| 60 | } |
| 61 | if ( ! $post instanceof \WP_Post ) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | if ( ! has_block( WPCOM_LAYOUT_GRID_USAGE_BLOCK_NAME, (string) $post->post_content ) ) { |
| 67 | return; |
| 68 | } |
| 69 | if ( $post_before instanceof \WP_Post && has_block( WPCOM_LAYOUT_GRID_USAGE_BLOCK_NAME, (string) $post_before->post_content ) ) { |
| 70 | return; |
| 71 | } |
| 72 | $is_importing = defined( 'WP_IMPORTING' ) && WP_IMPORTING; |
| 73 | $is_cron = defined( 'DOING_CRON' ) && DOING_CRON; |
| 74 | if ( ! wpcom_layout_grid_usage_should_log_in_context( $is_importing, $is_cron ) ) { |
| 75 | return; |
| 76 | } |
| 77 | $dispatched = wpcom_layout_grid_usage_log_observation( |
| 78 | array( |
| 79 | 'surface' => 'post_insert', |
| 80 | 'post_type' => (string) $post->post_type, |
| 81 | 'origin' => wpcom_layout_grid_usage_classify_origin(), |
| 82 | ) |
| 83 | ); |
| 84 | |
| 85 | |
| 86 | if ( $dispatched ) { |
| 87 | wpcom_layout_grid_usage_mark_context_seen( $is_importing, $is_cron ); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | function wpcom_layout_grid_usage_should_log_in_context( $is_importing, $is_cron ) { |
| 102 | $key = wpcom_layout_grid_usage_context_transient_key( $is_importing, $is_cron ); |
| 103 | if ( null === $key ) { |
| 104 | return true; |
| 105 | } |
| 106 | return ! get_transient( $key ); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | function wpcom_layout_grid_usage_mark_context_seen( $is_importing, $is_cron ) { |
| 117 | $key = wpcom_layout_grid_usage_context_transient_key( $is_importing, $is_cron ); |
| 118 | if ( null === $key ) { |
| 119 | return; |
| 120 | } |
| 121 | set_transient( $key, 1, DAY_IN_SECONDS ); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | function wpcom_layout_grid_usage_context_transient_key( $is_importing, $is_cron ) { |
| 132 | if ( $is_importing ) { |
| 133 | return WPCOM_LAYOUT_GRID_USAGE_IMPORT_TRANSIENT; |
| 134 | } |
| 135 | if ( $is_cron ) { |
| 136 | return WPCOM_LAYOUT_GRID_USAGE_CRON_TRANSIENT; |
| 137 | } |
| 138 | return null; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | function wpcom_layout_grid_usage_classify_origin() { |
| 176 | |
| 177 | if ( function_exists( 'wpcomsh_is_migration_in_progress' ) && wpcomsh_is_migration_in_progress() ) { |
| 178 | return 'migration'; |
| 179 | } |
| 180 | if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
| 181 | return 'import'; |
| 182 | } |
| 183 | if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) { |
| 184 | return 'xmlrpc'; |
| 185 | } |
| 186 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 187 | return 'cli'; |
| 188 | } |
| 189 | if ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
| 190 | return 'cron'; |
| 191 | } |
| 192 | if ( function_exists( 'get_current_user_id' ) && get_current_user_id() > 0 ) { |
| 193 | return 'editor'; |
| 194 | } |
| 195 | if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { |
| 196 | return 'rest'; |
| 197 | } |
| 198 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 199 | return 'ajax'; |
| 200 | } |
| 201 | return 'programmatic'; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | function wpcom_layout_grid_usage_react_to_widget_block_added( $option, $value ) { |
| 212 | unset( $option ); |
| 213 | if ( ! wpcom_layout_grid_usage_widget_value_contains_block( $value ) ) { |
| 214 | return; |
| 215 | } |
| 216 | wpcom_layout_grid_usage_log_observation( |
| 217 | array( |
| 218 | 'surface' => 'widget_add', |
| 219 | 'origin' => wpcom_layout_grid_usage_classify_origin(), |
| 220 | ) |
| 221 | ); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | function wpcom_layout_grid_usage_react_to_widget_block_updated( $old_value, $value ) { |
| 232 | if ( ! wpcom_layout_grid_usage_widget_value_contains_block( $value ) ) { |
| 233 | return; |
| 234 | } |
| 235 | if ( wpcom_layout_grid_usage_widget_value_contains_block( $old_value ) ) { |
| 236 | return; |
| 237 | } |
| 238 | wpcom_layout_grid_usage_log_observation( |
| 239 | array( |
| 240 | 'surface' => 'widget_update', |
| 241 | 'origin' => wpcom_layout_grid_usage_classify_origin(), |
| 242 | ) |
| 243 | ); |
| 244 | } |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | function wpcom_layout_grid_usage_react_to_block_render( $block_content, $parsed_block ) { |
| 258 | unset( $parsed_block ); |
| 259 | if ( get_option( WPCOM_LAYOUT_GRID_USAGE_SEEN_OPTION ) ) { |
| 260 | return $block_content; |
| 261 | } |
| 262 | |
| 263 | |
| 264 | |
| 265 | if ( wpcom_layout_grid_usage_log_observation( |
| 266 | array( |
| 267 | 'surface' => 'render', |
| 268 | |
| 269 | |
| 270 | |
| 271 | 'origin' => 'render', |
| 272 | ) |
| 273 | ) ) { |
| 274 | update_option( WPCOM_LAYOUT_GRID_USAGE_SEEN_OPTION, 1, false ); |
| 275 | } |
| 276 | return $block_content; |
| 277 | } |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | function wpcom_layout_grid_usage_widget_value_contains_block( $value ) { |
| 287 | if ( ! is_array( $value ) ) { |
| 288 | return false; |
| 289 | } |
| 290 | foreach ( $value as $widget ) { |
| 291 | if ( |
| 292 | is_array( $widget ) |
| 293 | && isset( $widget['content'] ) |
| 294 | && is_string( $widget['content'] ) |
| 295 | && has_block( WPCOM_LAYOUT_GRID_USAGE_BLOCK_NAME, $widget['content'] ) |
| 296 | ) { |
| 297 | return true; |
| 298 | } |
| 299 | } |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | function wpcom_layout_grid_usage_log_observation( array $extra ) { |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | if ( ! (bool) apply_filters( 'wpcom_layout_grid_usage_log_enabled', true, $extra ) ) { |
| 327 | return false; |
| 328 | } |
| 329 | if ( ! class_exists( '\Automattic\Jetpack\Jetpack_Mu_Wpcom' ) ) { |
| 330 | return false; |
| 331 | } |
| 332 | |
| 333 | try { |
| 334 | $active_plugins_raw = get_option( 'active_plugins' ); |
| 335 | $active_plugins = is_array( $active_plugins_raw ) ? array_values( $active_plugins_raw ) : array(); |
| 336 | |
| 337 | |
| 338 | if ( function_exists( 'is_multisite' ) && is_multisite() && function_exists( 'get_site_option' ) ) { |
| 339 | $sitewide_raw = get_site_option( 'active_sitewide_plugins' ); |
| 340 | if ( is_array( $sitewide_raw ) && ! empty( $sitewide_raw ) ) { |
| 341 | $active_plugins = array_values( array_unique( array_merge( $active_plugins, array_keys( $sitewide_raw ) ) ) ); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | |
| 346 | $payload = array_merge( |
| 347 | array( |
| 348 | 'active_theme' => function_exists( 'get_stylesheet' ) ? (string) get_stylesheet() : '', |
| 349 | 'active_plugins' => $active_plugins, |
| 350 | 'is_rest' => defined( 'REST_REQUEST' ) && REST_REQUEST, |
| 351 | 'is_cli' => defined( 'WP_CLI' ) && WP_CLI, |
| 352 | 'is_cron' => defined( 'DOING_CRON' ) && DOING_CRON, |
| 353 | 'is_importing' => defined( 'WP_IMPORTING' ) && WP_IMPORTING, |
| 354 | 'trace' => wpcom_layout_grid_usage_attribute_source(), |
| 355 | ), |
| 356 | $extra |
| 357 | ); |
| 358 | |
| 359 | \Automattic\Jetpack\Jetpack_Mu_Wpcom::log2logstash( |
| 360 | WPCOM_LAYOUT_GRID_USAGE_LOG_FEATURE, |
| 361 | WPCOM_LAYOUT_GRID_USAGE_LOG_MESSAGE, |
| 362 | wpcom_layout_grid_usage_redact_paths( $payload ) |
| 363 | ); |
| 364 | return true; |
| 365 | } catch ( \Throwable $e ) { |
| 366 | unset( $e ); |
| 367 | return false; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | function wpcom_layout_grid_usage_attribute_source() { |
| 380 | |
| 381 | $frames = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); |
| 382 | $self_file = __FILE__; |
| 383 | $relevant = array(); |
| 384 | foreach ( $frames as $frame ) { |
| 385 | $entry = wpcom_layout_grid_usage_format_attribution_frame( $frame, $self_file ); |
| 386 | if ( null === $entry ) { |
| 387 | continue; |
| 388 | } |
| 389 | $relevant[] = $entry; |
| 390 | if ( count( $relevant ) >= 8 ) { |
| 391 | break; |
| 392 | } |
| 393 | } |
| 394 | return $relevant; |
| 395 | } |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | function wpcom_layout_grid_usage_format_attribution_frame( $frame, string $self_file ) { |
| 409 | if ( ! is_array( $frame ) || empty( $frame['file'] ) || ! is_string( $frame['file'] ) ) { |
| 410 | return null; |
| 411 | } |
| 412 | if ( $frame['file'] === $self_file ) { |
| 413 | return null; |
| 414 | } |
| 415 | if ( ! preg_match( '#/wp-content/(plugins|themes|mu-plugins)/#', $frame['file'] ) ) { |
| 416 | return null; |
| 417 | } |
| 418 | $line = isset( $frame['line'] ) ? (int) $frame['line'] : 0; |
| 419 | return $frame['file'] . ':' . $line; |
| 420 | } |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | function wpcom_layout_grid_usage_redact_paths( $value ) { |
| 430 | if ( is_array( $value ) ) { |
| 431 | return array_map( 'wpcom_layout_grid_usage_redact_paths', $value ); |
| 432 | } |
| 433 | if ( ! is_string( $value ) || '' === $value ) { |
| 434 | return $value; |
| 435 | } |
| 436 | $replacements = array(); |
| 437 | if ( defined( 'WP_CONTENT_DIR' ) && '' !== WP_CONTENT_DIR ) { |
| 438 | $replacements[ rtrim( WP_CONTENT_DIR, '/' ) . '/' ] = '.../'; |
| 439 | } |
| 440 | if ( defined( 'ABSPATH' ) && '' !== ABSPATH ) { |
| 441 | $replacements[ rtrim( ABSPATH, '/' ) . '/' ] = '.../'; |
| 442 | } |
| 443 | if ( empty( $replacements ) ) { |
| 444 | return $value; |
| 445 | } |
| 446 | return strtr( $value, $replacements ); |
| 447 | } |