init(); } } /** * Initialize class. * * @since 1.8.3 */ public function init() { $this->hooks(); } /** * Register hooks. * * @since 1.8.3 */ public function hooks() { add_action( 'charitable_maybe_show_plugin_notifications', [ $this, 'output' ], 99 ); // where the notifications comes out in the admin. add_action( 'charitable_admin_notifications_update', [ $this, 'update' ] ); add_action( 'deactivate_plugin', [ $this, 'delete' ], 10, 2 ); } /** * Check if user has access and is enabled. * * @since 1.8.3 * * @return bool */ public function has_access() { $access = charitable_current_user_can( 'administrator' ) && ! charitable_get_option( 'hide-announcements' ); /** * Allow modifying state if a user has access. * * @since 1.7.0.3 * * @param bool $access True if user has access. */ return (bool) apply_filters( 'charitable_admin_notifications_has_access', $access ); } /** * Add notifications to the WordPress admin menu for Charitable. * * @since 1.8.3 * * @param array $submenu Array of menu items. * @return bool */ public function add_notifications_to_menu( $submenu = array() ) { if ( ( defined( 'CHARITABLE_NOTIFICATIONS_FORCE_OFF' ) && CHARITABLE_NOTIFICATIONS_FORCE_OFF ) ) { return $submenu; } $notifications_menu_item = array( 'page_title' => __( 'Notifications', 'charitable' ), 'menu_title' => __( 'Notifications', 'charitable' ), 'menu_slug' => 'charitable-notifications', 'function' => array( $this, 'render_plugin_notifications_page' ), 'capability' => 'manage_charitable_settings', ); // if there are new notifications add the indicator to the menu title. if ( $this->get_new_notifications_count() ) { $notifications_menu_item['menu_title'] .= '
'; } // If the checklist exists in the submenu, add this item after it. $checklist_key = array_search( 'charitable-setup-checklist', array_column( $submenu, 'menu_slug' ), true ); if ( false !== $checklist_key ) { $submenu = array_merge( array_slice( $submenu, 0, $checklist_key + 1 ), array( $notifications_menu_item ), array_slice( $submenu, $checklist_key + 1 ) ); } else { array_unshift( $submenu, $notifications_menu_item ); } return $submenu; } /** * Technically this should render the plugin notifications page. * However, notifications aren't rendering with this method. There should be a redirect, but the existance of this function prevents fatal errors. * * @since 1.8.3 */ public function render_plugin_notifications_page() { $this->output(); } /** * Get option value. * * @since 1.8.3 * * @param bool $cache Reference property cache if available. * * @return array */ public function get_option( $cache = true ) { if ( $this->option && $cache ) { return $this->option; } $option = (array) get_option( 'charitable_notifications', [] ); // $option = []; $this->option = [ 'update' => ! empty( $option['update'] ) ? (int) $option['update'] : 0, 'feed' => ! empty( $option['feed'] ) ? (array) $option['feed'] : [], 'events' => ! empty( $option['events'] ) ? (array) $option['events'] : [], 'dismissed' => ! empty( $option['dismissed'] ) ? (array) $option['dismissed'] : [], ]; return $this->option; } /** * Fetch notifications from feed. * * @since 1.8.3 * * @return array */ public function fetch_feed() { $args = [ 'timeout' => 10, 'sslverify' => false, 'user-agent' => charitable_get_default_user_agent(), ]; $response = wp_remote_get( self::SOURCE_URL, $args ); if ( is_wp_error( $response ) ) { return []; } $body = wp_remote_retrieve_body( $response ); if ( empty( $body ) ) { return []; } return $this->verify( json_decode( $body, true ) ); } /** * Verify notification data before it is saved. * * @since 1.8.3 * * @param array $notifications Array of notifications items to verify. * * @return array */ public function verify( $notifications ) { $data = []; if ( ! is_array( $notifications ) || empty( $notifications ) ) { return $data; } foreach ( $notifications as $notification ) { // Ignore if one of the conditional checks is true: // // 1. notification message is empty. // 2. license type does not match. // 3. notification is expired. // 4. notification has already been dismissed. // 5. notification existed before installing Charitable. // (Prevents bombarding the user with notifications after activation). if ( empty( $notification['content'] ) || ! $this->is_license_type_match( $notification ) || $this->is_expired( $notification ) || $this->is_dismissed( $notification ) || $this->is_existed( $notification ) ) { continue; } $data[] = $notification; } return $data; } /** * Verify saved notification data for active notifications. * * @since 1.8.3 * * @param array $notifications Array of notifications items to verify. * * @return array */ public function verify_active( $notifications ) { if ( ! is_array( $notifications ) || empty( $notifications ) ) { return []; } $current_timestamp = time(); // Remove notifications that are not active. foreach ( $notifications as $key => $notification ) { if ( ( ! empty( $notification['start'] ) && $current_timestamp < strtotime( $notification['start'] ) ) || ( ! empty( $notification['end'] ) && $current_timestamp > strtotime( $notification['end'] ) ) ) { unset( $notifications[ $key ] ); } } return $notifications; } /** * Get notification data. * * @since 1.8.3 * * @return array */ public function get() { if ( defined( 'CHARITABLE_DISABLE_NOTIFICATIONS' ) && CHARITABLE_DISABLE_NOTIFICATIONS ) { return []; } if ( ! $this->has_access() ) { return []; } $option = $this->get_option(); // Update notifications using async task. if ( empty( $option['update'] ) || time() > $option['update'] + DAY_IN_SECONDS ) { $option = $this->update(); } elseif ( empty( $option['feed'] ) && empty( $option['events'] ) ) { // regardless of the update time, if there are no notifications, update attempt. $option = $this->update(); } $feed = ! empty( $option['feed'] ) ? $this->verify_active( $option['feed'] ) : []; $events = ! empty( $option['events'] ) ? $this->verify_active( $option['events'] ) : []; return array_merge( $feed, $events ); } /** * Get notification count in the feed, regardless if they are dismissed or not. * * @since 1.8.3 * * @return int */ public function get_count() { return count( $this->get() ); } /** * Get new notifications by comparing those in the feed vs dismissed. * * @since 1.8.3 * * @return int */ public function get_new_notifications_count() { $option = $this->get_option(); $dimissed_ids = ! empty( $option['dismissed'] ) ? $option['dismissed'] : []; $notification_feed = $this->get(); // if $option is empty or if the feed is empty, there are no new notifications. if ( empty( $option ) || empty( $notification_feed ) ) { return 0; } $count = 0; foreach ( $notification_feed as $notification ) { // check the id of the notification against the dismissed ids. if ( ! in_array( (string) $notification['id'], $dimissed_ids, true ) ) { ++$count; } } return (int) $count; } /** * Add a new Event Driven notification. * * @since 1.8.3 * * @param array $notification Notification data. */ public function add( $notification ) { if ( ! $this->is_valid( $notification ) ) { return; } $option = $this->get_option(); // Notification ID already exists. if ( ! empty( $option['events'][ $notification['id'] ] ) ) { return; } update_option( 'charitable_notifications', [ 'update' => $option['update'], 'feed' => $option['feed'], 'events' => array_merge( $notification, $option['events'] ), 'dismissed' => $option['dismissed'], ] ); } /** * Determine if notification data is valid. * * @since 1.8.3 * * @param array $notification Notification data. * * @return bool */ public function is_valid( $notification ) { if ( empty( $notification['id'] ) ) { return false; } return ! empty( $this->verify( [ $notification ] ) ); } /** * Determine if notification has already been dismissed. * * @since 1.8.3 * * @param array $notification Notification data. * * @return bool */ private function is_dismissed( $notification ) { $option = $this->get_option(); // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict return ! empty( $option['dismissed'] ) && in_array( $notification['id'], $option['dismissed'] ); } /** * Determine if license type is match. * * @since 1.8.3 * * @param array $notification Notification data. * * @return bool */ private function is_license_type_match( $notification ) { // A specific license type is not required. if ( empty( $notification['type'] ) ) { return true; } return in_array( $this->get_license_type(), (array) $notification['type'], true ); } /** * Determine if notification is expired. * * @since 1.8.3 * * @param array $notification Notification data. * * @return bool */ private function is_expired( $notification ) { // Check if the notification 'end' parameter is set and not empty. if ( empty( $notification['end'] ) ) { return false; } // Get the WordPress timezone. $timezone = wp_timezone(); // Convert the notification end date to a DateTime object in the WordPress timezone. $end_time = new DateTime( $notification['end'], $timezone ); // Get the current server time as a DateTime object in the WordPress timezone. $current_time = new DateTime( 'now', $timezone ); // Compare the current time with the notification end time. return $current_time > $end_time; } /** * Determine if notification existed before installing Charitable. * If there is an "allow_on_activation" key, it will be used to determine to display even if the notification existed before. * * @since 1.8.3 * * @param array $notification Notification data. * * @return bool */ private function is_existed( $notification ) { $activated = charitable_get_activated_timestamp(); if ( ! isset( $notification['allow_on_activation'] ) ) { return ! empty( $activated ) && ! empty( $notification['start'] ) && $activated > strtotime( $notification['start'] ); } else { return ! empty( $activated ) && ! empty( $notification['start'] ) && false === $notification['allow_on_activation']; } } /** * Update notification data from feed. * * @since 1.8.3 */ public function update() { $option = $this->get_option(); $data = [ 'update' => time(), 'feed' => $this->fetch_feed(), 'events' => $option['events'], 'dismissed' => $option['dismissed'], ]; // phpcs:disable Charitable.PHP.ValidateHooks.InvalidHookName /** * Allow changing notification data before it will be updated in database. * * @since 1.8.3 * * @param array $data New notification data. */ $data = (array) apply_filters( 'charitable_admin_notifications_update_data', $data ); // phpcs:enable Charitable.PHP.ValidateHooks.InvalidHookName update_option( 'charitable_notifications', $data ); return $data; } /** * Remove notification data from database before a plugin is deactivated. * * @since 1.8.3 * * @param string $plugin Path to the plugin file relative to the plugins directory. * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network * or just the current site. Multisite only. Default false. */ public function delete( $plugin, $network_deactivating ) { // phpcs:ignore $charitable_plugins = [ 'charitable/charitable.php', ]; if ( ! in_array( $plugin, $charitable_plugins, true ) ) { return; } delete_option( 'charitable_notifications' ); } /** * Output notices. * * @since 1.8.3 */ public function output_notices() { return $this->output( 'notices' ); } /** * Output notifications on Form Overview admin area. * * @since 1.8.3 * * @param string $location Location where the notifications are output. * @param int $limit_active Limit of active notifications to show. */ public function output( $location = false, $limit_active = 4 ) { if ( defined( 'CHARITABLE_DISABLE_NOTIFICATIONS' ) && CHARITABLE_DISABLE_NOTIFICATIONS ) { return; } global $post; if ( 'notices' === $location && ! $this->show_notifications() ) { return; } $notifications = $this->get(); $active_notifications_html = ''; $dismissed_notifications_html = ''; $active_notifications_count = 0; $dismissed_notifications_count = 0; $current_class = ' current'; $content_allowed_tags = [ 'em' => [], 'strong' => [], 'span' => [ 'style' => [], ], 'a' => [ 'href' => [], 'target' => [], 'rel' => [], ], ]; $template_location = $location === 'dashboard' ? '/admin/templates/notifications-dashboard' : '/admin/templates/notifications'; $template_location = apply_filters( 'charitable_admin_notifications_template_location', $template_location, $location ); if ( empty( $notifications ) ) { // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo charitable_render( $template_location, [ 'notifications' => [ 'active_count' => $active_notifications_count, 'dismissed_count' => $dismissed_notifications_count, 'active_html' => $active_notifications_html, 'dismissed_html' => $dismissed_notifications_html, ], ], true ); return; } // Get active notifications. foreach ( $notifications as $notification ) { // Prepare required arguments. $notification = wp_parse_args( $notification, [ 'id' => 0, 'title' => '', 'content' => '', 'video' => '', ] ); $title = $this->get_component_data( $notification['title'] ); $content = $this->get_component_data( $notification['content'] ); if ( ! $title && ! $content ) { continue; } // If the notification ID is not a part of the dismissed notifications, add it to the list. if ( ! $this->is_dismissed( $notification ) ) { if ( 'dashboard' === $location ) { // Notification HTML, slightyl different for dashboard. $active_notifications_html .= sprintf( '