Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 46 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Log | |
0.00% |
0 / 46 |
|
0.00% |
0 / 5 |
56 | |
0.00% |
0 / 1 |
| register_post_type | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
| insert | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| post_to_entry | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| get_latest | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
| clear | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\Jetpack_Inspect; |
| 4 | |
| 5 | class Log { |
| 6 | |
| 7 | const POST_TYPE_NAME = 'jetpack_inspect_log'; |
| 8 | |
| 9 | /** |
| 10 | * Static initialization. |
| 11 | */ |
| 12 | public static function register_post_type() { |
| 13 | |
| 14 | // Check if post type already registered. |
| 15 | if ( post_type_exists( static::POST_TYPE_NAME ) ) { |
| 16 | return; |
| 17 | } |
| 18 | |
| 19 | register_post_type( |
| 20 | static::POST_TYPE_NAME, |
| 21 | array( |
| 22 | 'label' => 'Jetpack Inspect Log', |
| 23 | 'description' => 'Cache entries for the Jetpack Boost plugin.', |
| 24 | 'public' => false, |
| 25 | ) |
| 26 | ); |
| 27 | } |
| 28 | |
| 29 | public static function insert( $url, $data ) { |
| 30 | |
| 31 | $data_post_data = array( |
| 32 | 'post_type' => static::POST_TYPE_NAME, |
| 33 | 'post_title' => $url, |
| 34 | 'post_name' => uniqid( 'jetpack_inspect_log_', true ), |
| 35 | 'post_status' => 'publish', |
| 36 | 'post_content' => base64_encode( wp_json_encode( $data, JSON_UNESCAPED_SLASHES ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
| 37 | ); |
| 38 | |
| 39 | wp_insert_post( $data_post_data ); |
| 40 | } |
| 41 | |
| 42 | public static function post_to_entry( \WP_Post $post ): array { |
| 43 | $post_id = $post->ID; |
| 44 | try { |
| 45 | // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
| 46 | $data = json_decode( base64_decode( $post->post_content ), true, 512, JSON_THROW_ON_ERROR | JSON_BIGINT_AS_STRING | JSON_OBJECT_AS_ARRAY ); |
| 47 | } catch ( \JsonException $e ) { |
| 48 | $data = 'Error decoding JSON: ' . $e->getMessage(); |
| 49 | } |
| 50 | |
| 51 | return array_merge( |
| 52 | array( |
| 53 | 'id' => $post_id, |
| 54 | 'date' => $post->post_date, |
| 55 | ), |
| 56 | $data |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | public static function get_latest() { |
| 61 | $posts = get_posts( |
| 62 | array( |
| 63 | 'post_type' => static::POST_TYPE_NAME, |
| 64 | 'numberposts' => 50, |
| 65 | 'orderby' => 'date', |
| 66 | 'order' => 'DESC', |
| 67 | 'no_found_rows' => true, |
| 68 | 'ignore_sticky_posts' => true, |
| 69 | ) |
| 70 | ); |
| 71 | |
| 72 | return array_map( array( __CLASS__, 'post_to_entry' ), $posts ); |
| 73 | } |
| 74 | |
| 75 | public static function clear() { |
| 76 | global $wpdb; |
| 77 | |
| 78 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 79 | return $wpdb->delete( |
| 80 | $wpdb->posts, |
| 81 | array( 'post_type' => static::POST_TYPE_NAME ), |
| 82 | array( '%s' ) |
| 83 | ); |
| 84 | } |
| 85 | } |