option && $cache ) { return $this->option; } $option = get_option( self::OPTION_KEY, array() ); $this->option = array( 'update' => ! empty( $option['update'] ) ? $option['update'] : 0, 'notifications' => ! empty( $option['notifications'] ) ? $option['notifications'] : array(), 'blocked' => ! empty( $option['blocked'] ) ? $option['blocked'] : array(), ); return $this->option; } /** * Fetch notifications from feed */ public function fetch_notifications() { $res = wp_remote_get( self::SOURCE_URL ); if ( is_wp_error( $res ) ) { return array(); } $body = wp_remote_retrieve_body( $res ); if ( empty( $body ) ) { return array(); } return $this->validate( json_decode( $body, true ) ); } /** * Validate notification data before it is saved */ public function validate( $notifications ) { $data = array(); if ( ! is_array( $notifications ) || empty( $notifications || empty( $notifications['notifications'] ) ) ) { return $data; } $option = $this->get_option(); foreach ( $notifications['notifications'] as $notification ) { if ( empty( $notification['content'] ) ) { continue; } if ( ! empty( $notification['end_date'] ) && time() > strtotime( $notification['end_date'] ) ) { continue; } if ( ! empty( $option['blocked'] ) && in_array( $notification['id'], $option['blocked'] ) ) { continue; } $data[] = $notification; } return $data; } /** * Check start and end dates */ public function check_dates( $notifications ) { if ( ! is_array( $notifications ) || empty( $notifications ) ) { return array(); } foreach ( $notifications as $key => $notification ) { if ( ( ! empty( $notification['start_date'] ) && time() < strtotime( $notification['start_date'] ) ) || ( ! empty( $notification['end_date'] ) && time() > strtotime( $notification['end_date'] ) ) ) { unset( $notifications[ $key ] ); } } return $notifications; } /** * Get notification details */ public function get() { $option = $this->get_option(); if ( empty( $option['update'] ) || time() > $option['update'] + DAY_IN_SECONDS ) { if ( ! wp_next_scheduled( 'ocean_admin_notifications_update' ) ) { wp_schedule_single_event( time() + 60, 'ocean_admin_notifications_update' ); } } $notifications = ! empty( $option['notifications'] ) ? $this->check_dates( $option['notifications'] ) : array(); return array_merge( $notifications, array() ); } /** * Get notifications count */ public function get_count() { return count( $this->get() ); } /** * Update notification details from remote storage */ public function update() { $notifications = $this->fetch_notifications(); $option = $this->get_option(); update_option( self::OPTION_KEY, array( 'update' => time(), 'notifications' => $notifications, 'blocked' => $option['blocked'], ) ); } public function enqueues() { $notifications = $this->get(); if ( empty( $notifications ) ) { return; } wp_enqueue_style( 'ocean-admin-notifications', plugins_url( 'assets/css/notifications.min.css', __FILE__ ), array(), OCEANWP_THEME_VERSION ); wp_enqueue_script( 'ocean-admin-notifications', plugins_url( 'assets/js/notifications.min.js', __FILE__ ), array( 'jquery' ), OCEANWP_THEME_VERSION ); wp_localize_script( 'ocean-admin-notifications', 'ocean_notifications_admin', $this->get_localized_data() ); } /** * Output notifications */ public function output() { $notifications = $this->get(); if ( empty( $notifications ) ) { return; } $notifications_html = ''; $current_class = ' current'; $content_allowed_tags = array( 'em' => array(), 'strong' => array(), 'span' => array( 'style' => array(), ), 'a' => array( 'href' => array(), 'target' => array(), 'rel' => array(), ), ); foreach ( $notifications as $notification ) { // Buttons HTML $buttons_html = ''; if ( ! empty( $notification['button_1_data'] ) ) { $buttons_html .= sprintf( '%4$s', ! empty( $notification['button_1_data']['url'] ) ? esc_url( $notification['button_1_data']['url'] ) : '', $notification['button_1_data']['primary'] === 'yes' ? 'primary' : 'secondary', ! empty( $notification['button_1_data']['target'] ) && $notification['button_1_data']['target'] === '_blank' ? ' target="_blank" rel="noopener noreferrer"' : '', ! empty( $notification['button_1_data']['title'] ) ? sanitize_text_field( $notification['button_1_data']['title'] ) : '' ); } if ( ! empty( $notification['button_2_data'] ) ) { $buttons_html .= sprintf( '%4$s', ! empty( $notification['button_2_data']['url'] ) ? esc_url( $notification['button_2_data']['url'] ) : '', $notification['button_2_data']['primary'] === 'yes' ? 'primary' : 'secondary', ! empty( $notification['button_2_data']['target'] ) && $notification['button_2_data']['target'] === '_blank' ? ' target="_blank" rel="noopener noreferrer"' : '', ! empty( $notification['button_2_data']['title'] ) ? sanitize_text_field( $notification['button_2_data']['title'] ) : '' ); } $buttons_html = ! empty( $buttons_html ) ? '
' : ''; // Notification HTML $notifications_html .= sprintf( ' ', ! empty( $notification['title'] ) ? sanitize_text_field( $notification['title'] ) : '', ! empty( $notification['content'] ) ? wp_kses( $notification['content'], $content_allowed_tags ) : '', $buttons_html, ! empty( $notification['id'] ) ? esc_attr( sanitize_text_field( $notification['id'] ) ) : 0, $current_class ); $current_class = ''; } ?>