-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFS.php
276 lines (243 loc) · 7.87 KB
/
FS.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
* Base Feed Scraper class
* @package FS
*/
class FS
{
/**
* Returns the time the next scrape is scheduled for
* @param string $format The format the date should be returned in
* @return string
*/
public static function nextScheduledScrape( $format = 'd/m/Y H:i:s' )
{
$nextScheduledTimestamp = wp_next_scheduled( 'fs_scrape_interval' );
$nextScheduledDate = new DateTime( '@'. $nextScheduledTimestamp );
$nextScheduledDate->setTimeZone( new DateTimeZone( FS_OUTPUT_TIMEZONE ) );
return $nextScheduledDate->format( $format );
}
/**
* Initialises the plugin
*/
public static function init()
{
self::_registerActions();
self::_registerFilters();
}
/**
* Callback for fs_scrape_interval action
*/
public static function onFSScrapeInterval()
{
FS_Feeds::scrape();
}
/**
* Callback for wordpress admin_enqueue_scripts action
*/
public static function onWPAdminEnqueueScripts()
{
self::_addStyleSheetLink();
}
/**
* Callback for wordpress admin_menu action
*/
public static function onWPAdminMenu()
{
FS_Feed_Admin::addMetaBoxes();
}
/**
* Callback fro wordpress before_delete_post action
*/
public static function onWPBeforeDeletePost( $postID )
{
FS_Feed_Admin::handleDelete( $postID );
}
/**
* Callback for get_sample_permalink_html filter
* @param string $return The existing HTML for the permalink area
* @return string
*/
public static function onWPGetSamplePermalinkHTML( $return )
{
global $post;
// No need to display the permalink and related buttons if its a feed/entry
if( $post->post_type === 'fs_feed'
|| $post->post_Type === 'fs_feed_entry' )
{
return '';
}
return $return;
}
/**
* Callback for wordpress init action
*/
public static function onWPInit()
{
self::_loadTextDomain();
self::_addLatestEntriesShortcode();
FS_Feed::registerWPPostType();
FS_Feed_Entry::registerWPPostType();
}
/**
* Callback for wordpress manage_fs_feed_entry_posts_custom_column action
* @param string $columnName
* @param int $ID The Id of the post the column is being generated for
*/
public static function onWPManageFSFeedEntryPostsCustomColumn( $columnName, $ID )
{
FS_Feed_Entry_Admin::addColumnContent( $columnName, $ID );
}
/**
* Callback for wordpress manage_fs_feed_posts_custom_column action
* @param string $columnName
* @param int $ID The Id of the post the column is being generated for
*/
public static function onWPManageFSFeedPostsCustomColumn( $columnName, $ID )
{
FS_Feed_Admin::addColumnContent( $columnName, $ID );
}
/**
* Callback for WP's manage_fs_feed_entry_posts_column action
* @param array $columns WP's array of default columns
* @return array
*/
public static function onWPManageFSFeedEntryPostsColumns( $columns )
{
FS_Feed_Entry_Admin::addColumns( &$columns );
return $columns;
}
/**
* Callback for WP's manage_fs_feed_posts_column action
* @param array $columns WP's array of default columns
* @return array
*/
public static function onWPManageFSFeedPostsColumns( $columns )
{
FS_Feed_Admin::addColumns( &$columns );
return $columns;
}
/**
* Callback to be executed when the plugin is activated
*/
public static function onWPPluginActivation()
{
self::_registerScrapeCron();
}
/**
* Callback to be executed when the plugin is disabled
*/
public static function onWPPluginDeactivation()
{
self::_unregisterScrapeCron();
}
/**
* Callback for WP's post_row_actions action
* Removes unnecessary administration links from the WP feed/feed entry table
* @param array $actions Array of action links
* @param object $post WP post object for the current row
* @return array
*/
public static function onWPPostRowActions( $actions, $post )
{
if( $post->post_type === 'fs_feed' || $post->post_type === 'fs_feed_entry' )
{
unset( $actions['inline hide-if-no-js'], $actions['view'] );
}
if( $post->post_type === 'fs_feed_entry' )
{
unset( $actions['edit'] );
}
return $actions;
}
public static function onWPPostUpdatedMessages( $messages )
{
FS_Feed_Admin::filterPostUpdatedMessages( &$messages );
FS_Feed_Entry_Admin::filterPostUpdatedMessages( &$messages );
return $messages;
}
/**
* Callback for wordpress save_post action
* @return type
*/
public static function onWPSavePost( $postID )
{
FS_Feed_Admin::handleSave( $postID );
}
/**
* Removes any data the plugin has saved, reinitialises from new
*/
public static function reset()
{
FS_Feed_Entries::remove();
self::_unregisterScrapeCron();
self::onWPPluginActivation();
}
/**
* Registers [latest_feed_entries] wordpress shortcode
*/
private static function _addLatestEntriesShortcode()
{
add_shortcode( 'latest_feed_entries', array( 'FS_Feed_Entries', 'handleShortcode' ) );
}
/**
* Registers style sheet for addition
*/
private static function _addStyleSheetLink()
{
$styleUrl = plugins_url( 'assets/default.css', __FILE__ );
$styleFile = WP_PLUGIN_DIR . '/feed-scraper/assets/default.css';
if( file_exists( $styleFile ) )
{
wp_register_style( 'feed-scraper', $styleUrl );
wp_enqueue_style( 'feed-scraper' );
}
}
/**
* Loads text domain for wp gettext translations
*/
private static function _loadTextDomain()
{
$plugin_dir = basename( dirname( __FILE__ ) ) . '/languages/';
load_plugin_textdomain( 'feed-scraper', false, $plugin_dir );
}
/**
* Registers needed actions to appropriate wordpress hooks
*/
private static function _registerActions()
{
add_action( 'admin_enqueue_scripts', array( 'FS', 'onWPAdminEnqueueScripts' ) );
add_action( 'admin_menu', array( 'FS', 'onWPAdminMenu' ) );
add_action( 'before_delete_post', array( 'FS', 'onWPBeforeDeletePost' ) );
add_action( 'fs_scrape_interval', array( 'FS', 'onFSScrapeInterval' ) );
add_action( 'init', array( 'FS', 'onWPInit' ) );
add_action( 'manage_fs_feed_entry_posts_custom_column', array( 'FS', 'onWPManageFSFeedEntryPostsCustomColumn' ), 10, 2 );
add_action( 'manage_fs_feed_posts_custom_column', array( 'FS', 'onWPManageFSFeedPostsCustomColumn' ), 10, 2 );
add_action( 'post_row_actions', array( 'FS', 'onWPPostRowActions' ), 10, 2 );
add_action( 'save_post', array( 'FS', 'onWPSavePost' ) );
}
/**
* Registers filters
*/
private static function _registerFilters()
{
add_filter( 'get_sample_permalink_html', array( 'FS', 'onWPGetSamplePermalinkHTML' ) );
add_filter( 'manage_fs_feed_entry_posts_columns', array( 'FS', 'onWPManageFSFeedEntryPostsColumns' ) );
add_filter( 'manage_fs_feed_posts_columns', array( 'FS', 'onWPManageFSFeedPostsColumns' ) );
add_filter( 'post_updated_messages', array( 'FS', 'onWPPostUpdatedMessages' ) );
}
/**
* Registers a WP cron job to scrape for new feed entries
*/
private static function _registerScrapeCron()
{
wp_schedule_event( current_time( 'timestamp' ), 'hourly', 'fs_scrape_interval' );
}
/**
* Unregisters the cron job
*/
private static function _unregisterScrapeCron()
{
wp_clear_scheduled_hook( 'fs_scrape_interval' );
}
}