first commit

This commit is contained in:
2024-07-31 13:12:38 +07:00
commit b4e8cbe182
10213 changed files with 3125839 additions and 0 deletions

View File

@@ -0,0 +1,179 @@
<?php
namespace Elementor\Modules\Notifications;
use Elementor\User;
class API {
const NOTIFICATIONS_URL = 'https://assets.elementor.com/notifications/v1/notifications.json';
public static function get_notifications_by_conditions( $force_request = false ) {
$notifications = static::get_notifications( $force_request );
$filtered_notifications = [];
foreach ( $notifications as $notification ) {
if ( empty( $notification['conditions'] ) ) {
$filtered_notifications = static::add_to_array( $filtered_notifications, $notification );
continue;
}
if ( ! static::check_conditions( $notification['conditions'] ) ) {
continue;
}
$filtered_notifications = static::add_to_array( $filtered_notifications, $notification );
}
return $filtered_notifications;
}
private static function get_notifications( $force_request = false ) {
$notifications = self::get_transient( '_elementor_notifications_data' );
if ( $force_request || false === $notifications ) {
$notifications = static::fetch_data();
static::set_transient( '_elementor_notifications_data', $notifications, '+1 hour' );
}
return $notifications;
}
private static function fetch_data() : array {
$response = wp_remote_get( self::NOTIFICATIONS_URL );
if ( is_wp_error( $response ) ) {
return [];
}
$data = json_decode( wp_remote_retrieve_body( $response ), true );
if ( empty( $data['notifications'] ) || ! is_array( $data['notifications'] ) ) {
return [];
}
return $data['notifications'];
}
private static function add_to_array( $filtered_notifications, $notification ) {
foreach ( $filtered_notifications as $filtered_notification ) {
if ( $filtered_notification['id'] === $notification['id'] ) {
return $filtered_notifications;
}
}
$filtered_notifications[] = $notification;
return $filtered_notifications;
}
private static function check_conditions( $groups ) {
foreach ( $groups as $group ) {
if ( static::check_group( $group ) ) {
return true;
}
}
return false;
}
private static function check_group( $group ) {
$is_or_relation = ! empty( $group['relation'] ) && 'OR' === $group['relation'];
unset( $group['relation'] );
$result = false;
foreach ( $group as $condition ) {
// Reset results for each condition.
$result = false;
switch ( $condition['type'] ) {
case 'wordpress': // phpcs:ignore WordPress.WP.CapitalPDangit.Misspelled
// include an unmodified $wp_version
include ABSPATH . WPINC . '/version.php';
$result = version_compare( $wp_version, $condition['version'], $condition['operator'] );
break;
case 'multisite':
$result = is_multisite() === $condition['multisite'];
break;
case 'language':
$in_array = in_array( get_locale(), $condition['languages'], true );
$result = 'in' === $condition['operator'] ? $in_array : ! $in_array;
break;
case 'plugin':
if ( ! function_exists( 'is_plugin_active' ) ) {
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}
$is_plugin_active = is_plugin_active( $condition['plugin'] );
if ( empty( $condition['operator'] ) ) {
$condition['operator'] = '==';
}
$result = '==' === $condition['operator'] ? $is_plugin_active : ! $is_plugin_active;
break;
case 'theme':
$theme = wp_get_theme();
if ( wp_get_theme()->parent() ) {
$theme = wp_get_theme()->parent();
}
if ( $theme->get_template() === $condition['theme'] ) {
$version = $theme->version;
} else {
$version = '';
}
$result = version_compare( $version, $condition['version'], $condition['operator'] );
break;
case 'introduction_meta':
$result = User::get_introduction_meta( $condition['meta'] );
break;
default:
/**
* Filters the notification condition, whether to check the group or not.
*
* The dynamic portion of the hook name, `$condition['type']`, refers to the condition type.
*
* @since 3.19.0
*
* @param bool $result Whether to check the group.
* @param array $condition Notification condition.
*/
$result = apply_filters( "elementor/notifications/condition/{$condition['type']}", $result, $condition );
break;
}
if ( ( $is_or_relation && $result ) || ( ! $is_or_relation && ! $result ) ) {
return $result;
}
}
return $result;
}
private static function get_transient( $cache_key ) {
$cache = get_option( $cache_key );
if ( empty( $cache['timeout'] ) ) {
return false;
}
if ( current_time( 'timestamp' ) > $cache['timeout'] ) {
return false;
}
return json_decode( $cache['value'], true );
}
private static function set_transient( $cache_key, $value, $expiration = '+12 hours' ) {
$data = [
'timeout' => strtotime( $expiration, current_time( 'timestamp' ) ),
'value' => json_encode( $value ),
];
return update_option( $cache_key, $data, false );
}
}

View File

@@ -0,0 +1,101 @@
<?php
namespace Elementor\Modules\Notifications;
use Elementor\Core\Base\Module as BaseModule;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Module extends BaseModule {
public function get_name() {
return 'notification-center';
}
public function __construct() {
parent::__construct();
add_action( 'elementor/admin_top_bar/before_enqueue_scripts', function() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
wp_enqueue_script(
'e-admin-notifications',
$this->get_js_assets_url( 'admin-notifications' ),
[
'elementor-v2-ui',
'elementor-v2-icons',
'elementor-v2-query',
'wp-i18n',
],
ELEMENTOR_VERSION,
true
);
wp_localize_script(
'e-admin-notifications',
'elementorNotifications',
$this->get_app_js_config()
);
wp_set_script_translations( 'e-editor-notifications', 'elementor' );
}, 5 /* Before Elementor's admin enqueue scripts */ );
add_action( 'elementor/editor/v2/scripts/enqueue', [ $this, 'enqueue_editor_scripts' ] );
add_action( 'elementor/editor/after_enqueue_scripts', [ $this, 'enqueue_editor_scripts' ] );
add_action( 'elementor/ajax/register_actions', [ $this, 'register_ajax_actions' ] );
}
public function enqueue_editor_scripts() {
$deps = [
'elementor-editor',
'elementor-v2-ui',
'elementor-v2-icons',
'elementor-v2-query',
'wp-i18n',
];
$is_editor_v2 = current_action() === 'elementor/editor/v2/scripts/enqueue';
if ( $is_editor_v2 ) {
$deps[] = 'elementor-v2-editor-app-bar';
}
wp_enqueue_script(
'e-editor-notifications',
$this->get_js_assets_url( 'editor-notifications' ),
$deps,
ELEMENTOR_VERSION,
true
);
wp_localize_script(
'e-editor-notifications',
'elementorNotifications',
$this->get_app_js_config()
);
wp_set_script_translations( 'e-editor-notifications', 'elementor' );
}
private function get_app_js_config() : array {
return [
'is_unread' => Options::has_unread_notifications(),
];
}
public function register_ajax_actions( $ajax ) {
$ajax->register_ajax_action( 'notifications_get', [ $this, 'ajax_get_notifications' ] );
}
public function ajax_get_notifications() {
$notifications = API::get_notifications_by_conditions( true );
Options::mark_notification_read( $notifications );
return $notifications;
}
}

View File

@@ -0,0 +1,66 @@
<?php
namespace Elementor\Modules\Notifications;
class Options {
public static function has_unread_notifications() : bool {
$current_user = wp_get_current_user();
if ( ! $current_user ) {
return false;
}
$unread_notifications = get_transient( "elementor_unread_notifications_{$current_user->ID}" );
if ( false === $unread_notifications ) {
$notifications = API::get_notifications_by_conditions();
$notifications_ids = wp_list_pluck( $notifications, 'id' );
$unread_notifications = array_diff( $notifications_ids, static::get_notifications_dismissed() );
set_transient( "elementor_unread_notifications_{$current_user->ID}", $unread_notifications, HOUR_IN_SECONDS );
}
return ! empty( $unread_notifications );
}
public static function get_notifications_dismissed() {
$current_user = wp_get_current_user();
if ( ! $current_user ) {
return [];
}
$notifications_dismissed = get_user_meta( $current_user->ID, '_e_notifications_dismissed', true );
if ( ! is_array( $notifications_dismissed ) ) {
$notifications_dismissed = [];
}
return $notifications_dismissed;
}
public static function mark_notification_read( $notifications ) : bool {
$current_user = wp_get_current_user();
if ( ! $current_user ) {
return false;
}
$notifications_dismissed = static::get_notifications_dismissed();
foreach ( $notifications as $notification ) {
if ( ! in_array( $notification['id'], $notifications_dismissed, true ) ) {
$notifications_dismissed[] = $notification['id'];
}
}
$notifications_dismissed = array_unique( $notifications_dismissed );
update_user_meta( $current_user->ID, '_e_notifications_dismissed', $notifications_dismissed );
delete_transient( "elementor_unread_notifications_{$current_user->ID}" );
return true;
}
}