| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <?php
- /**
- * Notifications
- * @since 2.1
- */
- final class FLBuilderNotifications {
- static $url = 'https://www.wpbeaverbuilder.com/wp-json/wp/v2/fl_notification';
- static $option = 'fl_notifications';
- public static function init() {
- if ( FLBuilderModel::is_white_labeled() || true == apply_filters( 'fl_disable_notifications', false ) ) {
- return false;
- }
- add_action( 'pre_set_site_transient_update_plugins', array( 'FLBuilderNotifications', 'fetch_notifications_trigger' ) );
- add_action( 'fl_fetch_notifications', array( 'FLBuilderNotifications', 'fetch_notifications' ) );
- FLBuilderAJAX::add_action( 'fl_builder_notifications', array( 'FLBuilderNotifications', 'notications_ajax' ), array( 'read' ) );
- }
- /**
- * Transient is passed by reference here, lets not mess with it and just trigger our fetch.
- */
- public static function fetch_notifications_trigger( $transient ) {
- if ( ! did_action( 'fl_fetch_notifications' ) ) {
- do_action( 'fl_fetch_notifications' );
- }
- return $transient;
- }
- /**
- * Notification AJAX callback.
- *
- * @since 2.1
- */
- public static function notications_ajax( $read ) {
- if ( $read ) {
- self::update_state( true );
- } else {
- self::update_state( false );
- }
- wp_send_json_success();
- }
- /**
- * Fetch notifications from remote.
- *
- * @since 2.1
- */
- public static function fetch_notifications() {
- $defaults = array(
- 'read' => false,
- 'checksum' => '',
- 'data' => '{}',
- );
- $url = ( isset( $_GET['force-check'] ) ) ? self::$url . '/?a=' . rand() : self::$url;
- $stored_data = get_option( self::$option, $defaults );
- $response = wp_remote_get( $url );
- $response_code = wp_remote_retrieve_response_code( $response );
- $body = wp_remote_retrieve_body( $response );
- if ( 200 === $response_code ) {
- $body = json_decode( $body );
- // No post 0
- if ( ! isset( $body[0] ) || ! isset( $body[0]->date ) ) {
- return $stored_data;
- }
- // Generate checksum data
- $latest_checksum = self::get_checksum( $body );
- $stored_checksum = $stored_data['checksum'];
- // check if we have any unread posts by comparing checksums
- $unread = self::compare_checksums( $stored_checksum, $latest_checksum );
- $stored_data = array(
- 'read' => true,
- 'checksum' => $latest_checksum,
- 'data' => wp_json_encode( $body ),
- );
- if ( $unread ) {
- $stored_data['read'] = false;
- }
- update_option( self::$option, $stored_data );
- } else {
- error_log( 'response was not a 200' );
- }
- return $stored_data;
- }
- /**
- * Compare locally stored checksums against new data.
- * @since 2.1
- * @return bool true if new posts detected
- */
- public static function compare_checksums( $stored_checksum, $latest_checksum ) {
- if ( ! is_array( $stored_checksum ) ) {
- return true;
- }
- foreach ( $stored_checksum as $id => $date ) {
- // if a post has been deleted, then remove it from local checksum
- if ( ! isset( $latest_checksum[ $id ] ) ) {
- unset( $stored_checksum[ $id ] );
- }
- }
- $diff = array_diff_assoc( $latest_checksum, $stored_checksum );
- return ( ! empty( $diff ) ) ? true : false;
- }
- /**
- * Prepare checksum array from rest data.
- *
- * @since 2.1
- */
- public static function get_checksum( $body ) {
- $checksum = array();
- foreach ( $body as $post ) {
- $checksum[ $post->id ] = crc32( $post->content->rendered );
- }
- return (array) $checksum;
- }
- /**
- * Return notications from the db or fetch from remote
- *
- * @since 2.1
- */
- public static function get_notifications() {
- $defaults = array(
- 'read' => false,
- 'checksum' => '',
- 'data' => '{}',
- );
- $notifications = get_option( self::$option, $defaults );
- if ( '{}' == $notifications['data'] ) {
- return self::fetch_notifications();
- }
- return $notifications;
- }
- /**
- * Mark notifications read/unread
- *
- * @since 2.1
- */
- public static function update_state( $state ) {
- $defaults = array(
- 'read' => false,
- 'checksum' => '',
- 'data' => '{}',
- );
- $notifications = get_option( self::$option, $defaults );
- $notifications['read'] = $state;
- update_option( self::$option, $notifications );
- }
- }
- FLBuilderNotifications::init();
|