-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpresence-api.php
More file actions
169 lines (145 loc) · 5.89 KB
/
presence-api.php
File metadata and controls
169 lines (145 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* Plugin Name: Presence API
* Description: System-wide presence and awareness for WordPress.
* Version: 0.1.2
* Requires at least: 7.0
* Requires PHP: 7.4
* Author: Joe Fusco
* Text Domain: presence-api
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*
* @package Presence_API
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $wp_version;
if ( version_compare( $wp_version, '7.0-alpha', '<' ) ) {
add_action(
'admin_notices',
function () {
echo '<div class="notice notice-error"><p>';
echo esc_html__( 'Presence API requires WordPress 7.0 or later.', 'presence-api' );
echo '</p></div>';
}
);
return;
}
// If core or another plugin already provides the presence table, this plugin is not needed.
global $wpdb;
if ( isset( $wpdb->presence ) ) {
add_action(
'admin_notices',
function () {
echo '<div class="notice notice-warning"><p>';
echo esc_html__( 'Presence API: The presence table is already registered by WordPress or another plugin.', 'presence-api' );
echo '</p></div>';
}
);
return;
}
define( 'WP_PRESENCE_VERSION', '0.1.2' );
define( 'WP_PRESENCE_DB_VERSION', '1' );
define( 'WP_PRESENCE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
if ( ! defined( 'WP_PRESENCE_DEFAULT_TTL' ) ) {
define( 'WP_PRESENCE_DEFAULT_TTL', 60 );
}
/**
* Registers the presence table name on $wpdb.
*/
function wp_presence_register_table() {
global $wpdb;
$wpdb->presence = $wpdb->prefix . 'presence';
$wpdb->tables[] = 'presence';
}
wp_presence_register_table();
require_once WP_PRESENCE_PLUGIN_DIR . 'includes/functions.php';
require_once WP_PRESENCE_PLUGIN_DIR . 'includes/class-wp-rest-presence-controller.php';
require_once WP_PRESENCE_PLUGIN_DIR . 'includes/heartbeat.php';
require_once WP_PRESENCE_PLUGIN_DIR . 'includes/cron.php';
require_once WP_PRESENCE_PLUGIN_DIR . 'includes/post-lock-bridge.php';
require_once WP_PRESENCE_PLUGIN_DIR . 'includes/lifecycle.php';
require_once WP_PRESENCE_PLUGIN_DIR . 'includes/admin-bar.php';
require_once WP_PRESENCE_PLUGIN_DIR . 'includes/user-list.php';
require_once WP_PRESENCE_PLUGIN_DIR . 'includes/post-list.php';
require_once WP_PRESENCE_PLUGIN_DIR . 'includes/widgets/class-wp-presence-widget-whos-online.php';
require_once WP_PRESENCE_PLUGIN_DIR . 'includes/widgets/class-wp-presence-widget-active-posts.php';
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
require_once WP_PRESENCE_PLUGIN_DIR . 'includes/debugger-widget.php';
require_once WP_PRESENCE_PLUGIN_DIR . 'includes/db-viewer.php';
}
if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once WP_PRESENCE_PLUGIN_DIR . 'includes/cli/class-wp-presence-cli-command.php';
WP_CLI::add_command( 'presence', 'WP_Presence_CLI_Command' );
}
/**
* Registers presence support for core post types.
*
* Plugins can opt in their own post types with:
* add_post_type_support( 'product', 'presence' );
*/
function wp_presence_register_post_type_support() {
add_post_type_support( 'post', 'presence' );
add_post_type_support( 'page', 'presence' );
}
/**
* Registers the presence REST routes.
*/
function wp_presence_register_rest_routes() {
$controller = new WP_REST_Presence_Controller();
$controller->register_routes();
}
/**
* Loads the plugin text domain for translations.
*/
function wp_presence_load_textdomain() {
load_plugin_textdomain( 'presence-api', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/**
* Handles plugin activation.
*/
function wp_presence_activate() {
wp_maybe_create_presence_table();
wp_presence_schedule_cleanup();
}
/**
* Cleans up on plugin deactivation.
*/
function wp_presence_deactivate() {
wp_clear_scheduled_hook( 'wp_delete_expired_presence_data' );
}
add_action( 'init', 'wp_presence_register_table', 0 );
add_action( 'init', 'wp_presence_register_post_type_support' );
add_action( 'init', 'wp_presence_load_textdomain' );
add_action( 'admin_init', 'wp_maybe_create_presence_table' );
add_action( 'cli_init', 'wp_maybe_create_presence_table' );
add_action( 'rest_api_init', 'wp_presence_register_rest_routes' );
add_action( 'wp_delete_expired_presence_data', 'wp_delete_expired_presence_data' );
add_action( 'admin_init', 'wp_presence_schedule_cleanup' );
// phpcs:ignore WordPress.WP.CronInterval -- 60-second interval is intentional for presence cleanup.
add_filter( 'cron_schedules', 'wp_presence_cron_schedules' );
add_action( 'admin_enqueue_scripts', 'wp_presence_enqueue_heartbeat_ping' );
add_action( 'wp_enqueue_scripts', 'wp_presence_enqueue_heartbeat_ping' );
add_action( 'admin_enqueue_scripts', 'wp_presence_enqueue_editor_ping' );
add_filter( 'heartbeat_received', 'wp_presence_editor_heartbeat_received', 10, 3 );
add_filter( 'heartbeat_received', 'wp_presence_bridge_post_lock', 11, 3 );
add_action( 'wp_login', 'wp_presence_on_login', 10, 2 );
add_action( 'wp_logout', 'wp_presence_on_logout' );
add_action( 'admin_bar_menu', 'wp_presence_admin_bar_node', 80 );
add_action( 'admin_enqueue_scripts', 'wp_presence_admin_bar_assets' );
add_action( 'wp_enqueue_scripts', 'wp_presence_admin_bar_assets' );
add_filter( 'views_users', 'wp_presence_users_views' );
add_action( 'pre_get_users', 'wp_presence_filter_online_users' );
add_action( 'admin_init', 'wp_presence_register_post_list_columns' );
add_action( 'wp_dashboard_setup', array( 'WP_Presence_Widget_Whos_Online', 'register' ) );
add_filter( 'heartbeat_received', array( 'WP_Presence_Widget_Whos_Online', 'heartbeat_received' ), 10, 3 );
add_action( 'wp_dashboard_setup', array( 'WP_Presence_Widget_Active_Posts', 'register' ) );
add_filter( 'heartbeat_received', array( 'WP_Presence_Widget_Active_Posts', 'heartbeat_received' ), 10, 3 );
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
add_action( 'wp_dashboard_setup', 'wp_presence_heartbeat_widget_register' );
add_filter( 'heartbeat_received', 'wp_presence_heartbeat_widget_received', 10, 3 );
}
register_activation_hook( __FILE__, 'wp_presence_activate' );
register_deactivation_hook( __FILE__, 'wp_presence_deactivate' );