-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.php
183 lines (163 loc) · 5.13 KB
/
utils.php
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* Miscellaneous utility functions.
*
* @package Civil_Publisher
*/
namespace Civil_Publisher;
/**
* Check if old manager + post-panel functionality should be enabled.
*
* @return bool Whether or not it's enabled.
*/
function is_manager_enabled() {
// Essentially disable for anyone who doesn't already have it set up.
return ! empty( get_option( NEWSROOM_ADDRESS_OPTION_KEY ) );
}
/**
* Gets the post types for which to support publishing on Civil.
*
* @return array The supported post types.
*/
function get_civil_post_types() {
/**
* Filters the post types available for blockchain support
*
* @param $post_types The supported post types.
*/
return apply_filters( 'civil_publisher_post_types', array( 'post', 'page' ) );
}
/**
* Checks if the currently-logged-in-user can sign posts with the plugin.
*
* @return bool Whether or not tthey can sign.
*/
function current_user_can_sign_posts() {
// If user can't edit published posts, then they can't sign any updates to their posts, so rather than let them sign drafts and have invalid (and removed) signature on anly published updates, don't let them sign at all (until we have some way to add signatures outside of the edit post context).
return current_user_can( 'edit_published_posts' );
}
/**
* Check if given string is a valid hex ETH wallet address.
*
* @param string $addr Address to check.
* @return bool Whether or not address is valid hex ETH wallet address.
*/
function is_valid_eth_address( $addr ) {
return preg_match( '/^(0x)?[0-9a-f]{40}$/i', $addr );
}
/**
* Check if given string is a valid hex ETH transaction hash.
*
* @param string $hash hash to check.
* @return bool Whether or not address is valid hex ETH wallet address.
*/
function is_valid_txhash( $hash ) {
return preg_match( '/^(0x)?[0-9a-f]{64}$/i', $hash );
}
/**
* Gets subset of WP user data (just ID and display name) from given WP_User, or data from WP_User that's linked to given co-authors-plus guest author.
*
* @param object $user A WP_User object or co-authors-plus author object.
* @return array Associative array of data from WP_User object, but any display name set in guest author overrides regular user display name.
*/
function get_user_data( $user ) {
$data = null;
if ( ! empty( $user->linked_account ) ) {
// From co-authors plus.
$actual_user = get_user_by( 'login', $user->linked_account );
if ( ! empty( $actual_user ) ) {
$data = $actual_user->data;
if ( ! empty( $user->display_name ) ) {
// Guest author profile had its own display name, let's prefer that.
$data->display_name = $user->display_name;
}
}
}
if ( ! $data ) {
$data = $user->data;
}
return array(
'ID' => $data->ID,
'display_name' => $data->display_name,
);
}
/**
* Gets subset of data (just ID and display name) about all post authors for this post.
*
* @param int $post_id Post ID.
* @return array Data from WP_Users who are authors of the post - any co-authors-plus guest authors are dereferenced to their WP_Users.
*/
function get_post_authors_data( $post_id ) {
$authors = array();
if ( function_exists( 'get_coauthors' ) ) {
$authors = get_coauthors( $post_id );
} else {
$post = get_post( $post_id );
$author = get_user_by( 'id', $post->post_author );
if ( ! empty( $author ) ) {
$authors = array( $author );
}
}
return array_map( __NAMESPACE__ . '\get_user_data', $authors );
}
/**
* Scripts used for all our plugin scripts.
*
* @param string $script_name Name of dependency that will use these constants.
*/
function common_scripts( $script_name ) {
constants_script( $script_name );
if ( WP_DEBUG ) {
wp_enqueue_script(
'civil-publisher-live-reload',
'http://localhost:35729/livereload.js',
array(),
'1.0',
true
);
}
}
/**
* Insert a JS script to define constants to pass to frontend.
*
* @param string $script_name Name of dependency that will use these constants.
*/
function constants_script( $script_name ) {
$constants_json = json_encode(
array(
'wpDebug' => WP_DEBUG,
'newsroomAddress' => get_option( NEWSROOM_ADDRESS_OPTION_KEY ),
'wpSiteUrl' => site_url(),
'wpAdminUrl' => get_admin_url(),
'logoUrl' => get_site_icon_url(),
'adminEmail' => get_bloginfo( 'admin_email' ),
'newsroomTxHash' => get_option( NEWSROOM_TXHASH_OPTION_KEY ),
'networkName' => get_option( NETWORK_NAME_OPTION_KEY ),
)
);
wp_add_inline_script( $script_name, "window.civilNamespace = $constants_json;" . PHP_EOL, 'before' );
}
/**
* Checks if Gutenberg editor is enabled, either by plugin on old WP or by virtue of new WP.
*
* @return boolean Whether it's enabled.
*/
function is_gutenberg_enabled() {
if ( is_gutenberg_disabled_by_classic_editor() ) {
return false;
}
return is_plugin_active( 'gutenberg/gutenberg.php' ) || version_compare( get_bloginfo( 'version' ), '5.0', '>=' );
}
/**
* Checks if Gutenberg editor is disabled by the standard Classic Editor plugin.
*
* @return boolean Whether it's disabled.
*/
function is_gutenberg_disabled_by_classic_editor() {
if ( is_plugin_active( 'classic-editor/classic-editor.php' ) ) {
if ( 'block' !== get_option( 'classic-editor-replace' ) ) {
return true;
}
}
return false;
}