-
Notifications
You must be signed in to change notification settings - Fork 0
/
social-media-station.php
376 lines (315 loc) · 10.9 KB
/
social-media-station.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
/**
* Plugin Name: Social Media Station
* Plugin URI: https://wordpress.org/plugins/social-media-station/
* Description: Manage social media posts on a single platform.
* Requires at least: 6.1
* Requires PHP: 7.0
* Version: 0.1.0
* Author: Nicholas Babu
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: social-media-station
* Tested up to: 6.1
*
* @package social-media-station
*/
namespace Nick\SocialMediaStation;
if (!defined('ABSPATH')) {
exit();
}
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
define( 'SOCIAL_MEDIA_STATION_PLUGIN_DIR', __DIR__ );
/**
* Load composer dependencies.
*/
require_once SOCIAL_MEDIA_STATION_PLUGIN_DIR . '/vendor/autoload.php';
use Nick\SocialMediaStation\Service\PostToSocialMedia;
if (!class_exists('Social_Media_Station')) {
class Social_Media_Station {
use ConfigsTrait;
public static $socialMediaStation;
private function __construct() {
$this->setup();
}
private function setup(){
if ( ! function_exists( 'sms_fs' ) ) {
// Create a helper function for easy SDK access.
function sms_fs() {
global $sms_fs;
if ( ! isset( $sms_fs ) ) {
// Include Freemius SDK.
require_once dirname(__FILE__) . '/freemius/start.php';
$sms_fs = fs_dynamic_init( array(
'id' => '11728',
'slug' => 'social-media-station',
'type' => 'plugin',
'public_key' => 'pk_2cb7eb7698dd5f56c75e42dd78fcd',
'is_premium' => true,
'premium_suffix' => 'Custom intergration',
// If your plugin is a serviceware, set this option to false.
'has_premium_version' => true,
'has_addons' => false,
'has_paid_plans' => true,
'menu' => array(
'slug' => 'social_media_station',
),
// Set the SDK to work in a sandbox mode (for development & testing).
// IMPORTANT: MAKE SURE TO REMOVE SECRET KEY BEFORE DEPLOYMENT.
'secret_key' => 'undefined',
) );
}
return $sms_fs;
}
// Init Freemius.
sms_fs();
// Signal that SDK was initiated.
do_action( 'sms_fs_loaded' );
}
register_activation_hook ( __FILE__ , array ( $this , 'social_media_station_activate_setup' ) );
register_deactivation_hook ( __FILE__ , array ( $this , 'social_media_station_deactivate_cleanup' ) );
//add_action( 'init', 'create_block_social_media_station_block_init' );
add_action('init', array ($this, 'social_media_station_register'));
add_action('admin_menu', array ($this, 'social_media_station_menu'));
add_filter( 'plugin_action_links_' . plugin_basename(__FILE__),array ($this, 'social_media_station_action_links' ));
add_action( 'admin_enqueue_scripts', array ($this, 'social_media_station_admin_scripts'), 10 );
// Get social media options.
add_action ( 'wp_ajax_social_media_station_config_options' , array (
$this ,
'social_media_station_config_options'
) );
add_filter( 'cron_schedules', array ($this, 'social_media_station_add_cron_interval') );
// Cron tasks for scheduled posts.
if (!wp_next_scheduled('social_media_station_cron')) {
wp_schedule_event(time(), 'five_minutes', array ($this, 'social_media_station_cron'));
}
}
/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
public function social_media_station_activate_setup() {
}
/**
* Clean up after uninstall.
* @return void
*/
public function social_media_station_deactivate_cleanup() {
// delete configurations
delete_option('social_media_station_settings_section');
}
/**
* Adds custom cron interval.
* @param $schedules
*
* @return mixed
*/
public function social_media_station_add_cron_interval( $schedules ) {
$schedules['five_minutes'] = array(
'interval' => 5 * 60,
'display' => esc_html__( 'Every Five minutes' ), );
return $schedules;
}
/**
* Posts to social media during cron.
* @return void
*/
public function social_media_station_cron() {
$social_media = new PostToSocialMedia();
$social_media->postToSocials();
}
/**
* Requests related to configurations(options).
* @return void
*/
public function social_media_station_config_options() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
if (isset($_POST['configs'])) {
// Sanitize all text input values.
$data = $this->sanitizeConfigs($_POST['configs']);
$data = serialize($data);
update_option('social_media_station_settings_section', $data);
}
$data = get_option('social_media_station_settings_section');
if ($data) {
$data = unserialize($data);
} else {
$data = [
'configs' => [
'facebook' => [
['key' => 'ACCESS_TOKEN', 'value' => ''],
['key' => 'APP_ID', 'value' => ''],
['key' => 'APP_SECRET', 'value' => ''],
['key' => 'PAGE_POST_ID', 'value' => ''],
],
'tiktok' => [
['key' => 'app_id', 'value' => ''],
['key' => 'app_secret', 'value' => ''],
],
'twitter' => [
['key' => 'CONSUMER_KEY', 'value' => ''],
['key' => 'CONSUMER_SECRET', 'value' => ''],
['key' => 'OAUTH_TOKEN', 'value' => ''],
['key' => 'OAUTH_TOKEN_SECRET', 'value' => ''],
],
],
];
}
wp_send_json_success ($data);
}
/**
* Add settings page link with plugin.
*/
public function social_media_station_action_links( $links ){
$plugin_action_links = array(
'<a href="' . admin_url( 'admin.php?page=social_media_station' ) . '"> '. __('Settings', '') . '</a>',
);
return array_merge( $links, $plugin_action_links );
}
/**
* Plugin settings page
*/
public function social_media_station_register() {
register_block_type( __DIR__ . '/build' );
// Register social media post
register_post_type('social_media',
array (
'labels' => array(
'name' => __( 'Social media', 'social-media-station' ),
'singular_name' => __( 'Social media', 'social-media-station' ),
'add_new' => __( 'New Social media post', 'social-media-station' ),
'new_item' => __( 'New Book', 'social-media-station' ),
'view_item' => __( 'View Books', 'social-media-station' ),
),
'description' => __('Social media posts entries.', 'social-media-station'),
'has_archive' => true,
'public' => true,
'rewrite' => array('slug' => 'social_media'),
'query_var' => true,
'show_in_rest' => true,
'publicly_queryable' => true,
'exclude_from_search' => true,
'supports' => array ('title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields', 'comments'),
'template' => array (
array ('social-media-station/content-block',
// array (
// 'about' => 'A default about!!',
// ),
),
)
)
);
// register a new section
register_setting(
'social_media_station_settings_section',
'social_media_station_data',
array(
'default' => '',
'show_in_rest' => false,
'type' => 'string',
)
);
}
//Register plugin admin menu
public function social_media_station_menu() {
add_menu_page(
__('Social media station configuration', 'social-media-station'),
__('Settings', 'social-media-station'),
'manage_options',
'social_media_station',
array ( $this, 'social_media_station_admin_settings_output'),
'dashicons-randomize');
}
//Plugin options form
public function social_media_station_admin_settings_output(){
?>
<div id="social-media-station-plugin-settings"></div>
<?php
$socials = [
'facebook' => [
'name' => 'Facebook',
'config' => [
['key' => 'ACCESS_TOKEN', 'name' => 'Access Token', 'description' => 'The Access token'],
['key' => 'APP_ID', 'name' => 'App Id', 'description' => 'Facebook app id'],
['key' => 'APP_SECRET', 'name' => 'App secret', 'description' => 'Facebook app secret'],
['key' => 'PAGE_POST_ID', 'name' => 'Page Id', 'description' => 'The Facebook page id to post to'],
]
],
'tiktok' => [
'name' => 'Tiktok',
'config' => [
['key' => 'app_id', 'name' => 'App Id', 'description' => 'Tiktok app id'],
['key' => 'app_secret', 'name' => 'App secret', 'description' => 'Tiktok app secret'],
]
],
'twitter' => [
'name' => 'Twitter',
'config' => [
['key' => 'CONSUMER_KEY', 'name' => 'Consumer key', 'description' => 'Twitter consumer key'],
['key' => 'CONSUMER_SECRET', 'name' => 'Consumer secret', 'description' => 'Twitter consumer secret'],
['key' => 'OAUTH_TOKEN', 'name' => 'Auth token', 'description' => 'Twitter oath token'],
['key' => 'OAUTH_TOKEN_SECRET', 'name' => 'Auth token secret', 'description' => 'Twitter oath token secret'],
]
],
];
$data = [
'socials' => $socials,
];
$admin_js = plugins_url( '/', __FILE__ ) . 'build/admin.js';
wp_enqueue_script(
'social-media-station-admin',
$admin_js,
array('react', 'react-dom', 'wp-api', 'wp-components', 'wp-dom-ready', 'wp-element', 'wp-i18n'),
1,
true
);
wp_add_inline_script(
'social-media-station-admin',
'var socialMediaData = ' . wp_json_encode( $data ),
'before'
);
$admin_css = 'build/admin.css';
$dir = __DIR__;
wp_enqueue_style(
'social-media-station-admin',
plugins_url( $admin_css, __FILE__ ),
['wp-components'],
filemtime( "$dir/$admin_css" )
);
}
// Register and enqueue admin scripts
public function social_media_station_admin_scripts() {
$dir = __DIR__;
$script_asset_path = "$dir/build/admin.asset.php";
if ( ! file_exists( $script_asset_path ) ) {
throw new Error(
'You need to run `npm start` or `npm run build` to build the asset files first.'
);
}
$admin_js = plugins_url( '/', __FILE__ ) . 'build/admin.js';
wp_register_script(
'social-media-station-admin',
$admin_js,
array('react', 'react-dom', 'wp-api', 'wp-components', 'wp-dom-ready', 'wp-element', 'wp-i18n'),
1,
false
);
wp_set_script_translations('social-media-station-admin', 'social-media-station' );
}
public static function run() {
if (!isset(self::$socialMediaStation)) {
self::$socialMediaStation = new Social_Media_Station;
}
return self::$socialMediaStation;
}
}
Social_Media_Station::run();
}