forked from alleyinteractive/apple-news
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apple-news.php
213 lines (182 loc) · 5.86 KB
/
apple-news.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
/**
* Entry point for the plugin.
*
* This file is read by WordPress to generate the plugin information in the
* admin panel.
*
* @link http://github.com/alleyinteractive/apple-news
* @since 0.2.0
* @package WP_Plugin
*/
/*
* Plugin Name: Publish to Apple News
* Plugin URI: http://github.com/alleyinteractive/apple-news
* Description: Export and sync posts to Apple format.
* Version: 2.4.8
* Author: Alley
* Author URI: https://alley.com
* Text Domain: apple-news
* Domain Path: lang/
*/
/**
* A shim for wp_date, if it is not defined.
*
* @param string $format PHP date format.
* @param int $timestamp Optional. Unix timestamp. Defaults to current time.
* @param DateTimeZone $timezone Optional. Timezone to output result in. Defaults to timezone from site settings.
*
* @return string|false The date, translated if locale specifies it. False on invalid timestamp input.
*/
function apple_news_date( $format, $timestamp = null, $timezone = null ) {
// If wp_date exists (WP >= 5.3.0) use it.
if ( function_exists( 'wp_date' ) ) {
return wp_date( $format, $timestamp, $timezone );
}
// Fall back to using the date function if wp_date does not exist, to preserve backwards compatibility.
return date( $format, $timestamp ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
}
require_once plugin_dir_path( __FILE__ ) . './includes/meta.php';
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Activate the plugin.
*/
function apple_news_activate_wp_plugin() {
// Check for PHP version.
if ( version_compare( PHP_VERSION, '5.3.6' ) < 0 ) {
deactivate_plugins( basename( __FILE__ ) );
wp_die( esc_html__( 'This plugin requires at least PHP 5.3.6', 'apple-news' ) );
}
}
require plugin_dir_path( __FILE__ ) . 'includes/apple-exporter/class-settings.php';
/**
* Deactivate the plugin.
*/
function apple_news_uninstall_wp_plugin() {
$settings = new Apple_Exporter\Settings();
foreach ( array_keys( $settings->all() ) as $name ) {
delete_option( $name );
}
}
// WordPress VIP plugins do not execute these hooks, so ignore in that environment.
if ( ! defined( 'WPCOM_IS_VIP_ENV' ) || ! WPCOM_IS_VIP_ENV ) {
register_activation_hook( __FILE__, 'apple_news_activate_wp_plugin' );
register_uninstall_hook( __FILE__, 'apple_news_uninstall_wp_plugin' );
}
// Initialize plugin class.
require plugin_dir_path( __FILE__ ) . 'includes/class-apple-news.php';
require plugin_dir_path( __FILE__ ) . 'admin/class-admin-apple-news.php';
/**
* Load plugin textdomain.
*
* @since 0.9.0
*/
function apple_news_load_textdomain() {
load_plugin_textdomain( 'apple-news', false, plugin_dir_path( __FILE__ ) . '/lang' );
}
add_action( 'plugins_loaded', 'apple_news_load_textdomain' );
/**
* Gets plugin data.
* Used to provide generator info in the metadata class.
*
* @return array
*
* @since 1.0.4
*/
function apple_news_get_plugin_data() {
if ( ! function_exists( 'get_plugin_data' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
return get_plugin_data( plugin_dir_path( __FILE__ ) . '/apple-news.php' );
}
new Admin_Apple_News();
/**
* Reports whether an export is currently happening.
*
* @return bool True if exporting, false if not.
* @since 1.4.0
*/
function apple_news_is_exporting() {
return Apple_Actions\Index\Export::is_exporting();
}
/**
* Check if Block Editor is active.
* Must only be used after plugins_loaded action is fired.
*
* @return bool
*/
function apple_news_block_editor_is_active() {
$active = true;
// Gutenberg plugin is installed and activated.
$gutenberg = ! ( false === has_filter( 'replace_editor', 'gutenberg_init' ) );
// Block editor since 5.0.
$block_editor = version_compare( $GLOBALS['wp_version'], '5.0-beta', '>' );
if ( ! $gutenberg && ! $block_editor ) {
$active = false;
}
if ( $active && apple_news_is_classic_editor_plugin_active() ) {
$editor_option = get_option( 'classic-editor-replace' );
$block_editor_active = [ 'no-replace', 'block' ];
$active = in_array( $editor_option, $block_editor_active, true );
}
/**
* Overrides whether Apple News thinks the block editor is active or not.
*
* @since 2.0.0
*
* @param bool $active Whether Apple News thinks the block editor is active or not.
*/
return apply_filters( 'apple_news_block_editor_is_active', $active );
}
/**
* Check if Block Editor is active for a given post ID.
*
* @param int $post_id Optional. The post ID to check. Defaults to the current post ID.
* @return bool
*/
function apple_news_block_editor_is_active_for_post( $post_id = 0 ) {
// If get_current_screen is not defined, we can't get info about the view, so bail out.
if ( ! function_exists( 'get_current_screen' ) || ! function_exists( 'use_block_editor_for_post' ) ) {
return false;
}
// Only return true if we are on the post add/edit screen.
$screen = get_current_screen();
if ( empty( $screen->base ) || 'post' !== $screen->base ) {
return false;
}
// If the post ID isn't specified, pull the current post ID.
if ( empty( $post_id ) ) {
$post_id = get_the_ID();
}
// If the post ID isn't defined, bail out.
if ( empty( $post_id ) ) {
return false;
}
return use_block_editor_for_post( $post_id );
}
/**
* Check if Classic Editor plugin is active.
*
* @return bool
*/
function apple_news_is_classic_editor_plugin_active() {
if ( ! function_exists( 'is_plugin_active' ) ) {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
}
if ( is_plugin_active( 'classic-editor/classic-editor.php' ) ) {
return true;
}
return false;
}
/**
* Given a user ID, a post ID, and an action, determines whether a user can
* perform the action or not.
*
* @param int $post_id The ID of the post to check.
* @param string $action The action to check. One of 'publish', 'update', 'delete'.
* @param int $user_id The user ID to check.
*
* @return bool True if the user can perform the action, false otherwise.
*/