Completed
Branch ci (4cd40d)
by litefeel
03:54 queued 01:39
created

writing-on-github.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Plugin Name: Writing on GitHub
4
 * Plugin URI: https://github.com/litefeel/writing-on-github
5
 * Description: A WordPress plugin to allow you writing on GitHub (or Jekyll site).
6
 * Version: 1.1
7
 * Author:  litefeel
8
 * Author URI: https://www.litefeel.com
9
 * License: GPLv2
10
 * Text Domain: writing-on-github
11
 */
12
13
// If the functions have already been autoloaded, don't reload.
14
// This fixes function duplication during unit testing.
15
if ( defined( 'WRITING_ON_GITHUB_TEST' ) && WRITING_ON_GITHUB_TEST ) {
16
	$path = dirname( __FILE__ ) . '/vendor/autoload_52.php';
17
	include_once $path;
18
}
19
20
21
require_once(dirname(__FILE__) . '/Spyc.php');
22
require_once(dirname(__FILE__) . '/lib/cache.php');
23
require_once(dirname(__FILE__) . '/lib/database.php');
24
require_once(dirname(__FILE__) . '/lib/admin.php');
25
require_once(dirname(__FILE__) . '/lib/payload.php');
26
require_once(dirname(__FILE__) . '/lib/post.php');
27
// require_once(dirname(__FILE__) . '/lib/cli.php');
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
28
require_once(dirname(__FILE__) . '/lib/controller.php');
29
require_once(dirname(__FILE__) . '/lib/export.php');
30
require_once(dirname(__FILE__) . '/lib/semaphore.php');
31
require_once(dirname(__FILE__) . '/lib/request.php');
32
require_once(dirname(__FILE__) . '/lib/client/base.php');
33
require_once(dirname(__FILE__) . '/lib/client/fetch.php');
34
require_once(dirname(__FILE__) . '/lib/client/persist.php');
35
require_once(dirname(__FILE__) . '/lib/import.php');
36
require_once(dirname(__FILE__) . '/lib/api.php');
37
require_once(dirname(__FILE__) . '/lib/fileinfo.php');
38
require_once(dirname(__FILE__) . '/lib/blob.php');
39
require_once(dirname(__FILE__) . '/lib/response.php');
40
// require_once(dirname(__FILE__) . '/views/setting-field.php');
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
41
// require_once(dirname(__FILE__) . '/views/options.php');
42
// require_once(dirname(__FILE__) . '/views/user-setting-field.php');
43
44
add_action( 'plugins_loaded', array( new Writing_On_GitHub, 'boot' ) );
45
46
class Writing_On_GitHub {
47
48
	/**
49
	 * Object instance
50
	 * @var self
51
	 */
52
	public static $instance;
53
54
	/**
55
	 * Language text domain
56
	 * @var string
57
	 */
58
	public static $text_domain = 'writing-on-github';
59
60
	/**
61
	 * Current version
62
	 * @var string
63
	 */
64
	public static $version = '1.7.5';
65
66
	/**
67
	 * Controller object
68
	 * @var Writing_On_GitHub_Controller
69
	 */
70
	public $controller;
71
72
	/**
73
	 * Controller object
74
	 * @var Writing_On_GitHub_Admin
75
	 */
76
	public $admin;
77
78
	/**
79
	 * CLI object.
80
	 *
81
	 * @var Writing_On_GitHub_CLI
82
	 */
83
	protected $cli;
84
85
	/**
86
	 * Request object.
87
	 *
88
	 * @var Writing_On_GitHub_Request
89
	 */
90
	protected $request;
91
92
	/**
93
	 * Response object.
94
	 *
95
	 * @var Writing_On_GitHub_Response
96
	 */
97
	protected $response;
98
99
	/**
100
	 * Api object.
101
	 *
102
	 * @var Writing_On_GitHub_Api
103
	 */
104
	protected $api;
105
106
	/**
107
	 * Import object.
108
	 *
109
	 * @var Writing_On_GitHub_Import
110
	 */
111
	protected $import;
112
113
	/**
114
	 * Export object.
115
	 *
116
	 * @var Writing_On_GitHub_Export
117
	 */
118
	protected $export;
119
120
	/**
121
	 * Semaphore object.
122
	 *
123
	 * @var Writing_On_GitHub_Semaphore
124
	 */
125
	protected $semaphore;
126
127
	/**
128
	 * Database object.
129
	 *
130
	 * @var Writing_On_GitHub_Database
131
	 */
132
	protected $database;
133
134
	/**
135
	 * Cache object.
136
	 *
137
	 * @var Writing_On_GitHub_Cache
138
	 */
139
	protected $cache;
140
141
	/**
142
	 * Called at load time, hooks into WP core
143
	 */
144
	public function __construct() {
145
		self::$instance = $this;
146
147
		if ( is_admin() ) {
148
			$this->admin = new Writing_On_GitHub_Admin;
149
		}
150
151
		$this->controller = new Writing_On_GitHub_Controller( $this );
152
153
		if ( defined( 'WP_CLI' ) && WP_CLI ) {
154
			WP_CLI::add_command( 'wogh', $this->cli() );
155
		}
156
	}
157
158
	/**
159
	 * Attaches the plugin's hooks into WordPress.
160
	 */
161
	public function boot() {
162
		register_activation_hook( __FILE__, array( $this, 'activate' ) );
163
		add_action( 'admin_notices', array( $this, 'activation_notice' ) );
164
165
		add_action( 'init', array( $this, 'l10n' ) );
166
167
		// Controller actions.
168
		add_action( 'save_post', array( $this->controller, 'export_post' ) );
169
		add_action( 'delete_post', array( $this->controller, 'delete_post' ) );
170
		add_action( 'wp_ajax_nopriv_wogh_push_request', array( $this->controller, 'pull_posts' ) );
171
		add_action( 'wogh_export', array( $this->controller, 'export_all' ), 10, 1 );
172
		add_action( 'wogh_import', array( $this->controller, 'import_master' ), 10, 1 );
173
		add_filter( 'get_edit_post_link', array( $this, 'edit_post_link' ), 10, 3 );
174
175
		add_filter( 'wogh_post_meta', array( $this, 'ignore_post_meta' ), 10, 1 );
176
		add_filter( 'wogh_pre_import_meta', array( $this, 'ignore_post_meta' ), 10, 1 );
177
178
		do_action( 'wogh_boot', $this );
179
	}
180
181
	public function edit_post_link($link, $postID, $context) {
0 ignored issues
show
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
182
		if ( ! wp_is_post_revision( $postID ) ) {
183
			$post = new Writing_On_GitHub_Post( $postID, Writing_On_GitHub::$instance->api() );
184
			if ( $post->is_on_github() ) {
185
				return $post->github_edit_url();
186
			}
187
		}
188
189
	    return $link;
190
	}
191
192
	public function ignore_post_meta($meta) {
193
		$ignore_meta_keys = get_option('wogh_ignore_metas');
194
		if (empty($ignore_meta_keys)) {
195
			return $meta;
196
		}
197
198
		$keys = preg_split("/\\r\\n|\\r|\\n/", $ignore_meta_keys);
199
		if (empty($keys)) {
200
			return $meta;
201
		}
202
		foreach ($keys as $key => $value) {
203
			$keys[$key] = trim($value);
204
		}
205
206
		foreach ($meta as $key => $value) {
207
			if (in_array($key, $keys)) {
208
				unset($meta[$key]);
209
			}
210
		}
211
212
		return $meta;
213
	}
214
215
	/**
216
	 * Init i18n files
217
	 */
218
	public function l10n() {
219
		load_plugin_textdomain( self::$text_domain );
220
	}
221
222
	/**
223
	 * Sets and kicks off the export cronjob
224
	 */
225
	public function start_export() {
226
		$this->start_cron( 'export' );
227
	}
228
229
	/**
230
	 * Sets and kicks off the import cronjob
231
	 */
232
	public function start_import() {
233
		$this->start_cron( 'import' );
234
	}
235
236
	/**
237
	 * Enables the admin notice on initial activation
238
	 */
239
	public function activate() {
240
		if ( 'yes' !== get_option( '_wogh_fully_exported' ) ) {
241
			set_transient( '_wogh_activated', 'yes' );
242
		}
243
	}
244
245
	/**
246
	 * Displays the activation admin notice
247
	 */
248
	public function activation_notice() {
249
		if ( ! get_transient( '_wogh_activated' ) ) {
250
			return;
251
		}
252
253
		delete_transient( '_wogh_activated' );
254
255
		?><div class="updated">
256
			<p>
257
				<?php
258
					printf(
259
						__( 'To set up your site to sync with GitHub, update your <a href="%s">settings</a> and click "Export to GitHub."', 'writing-on-github' ),
260
						admin_url( 'options-general.php?page=' . static::$text_domain)
261
					);
262
				?>
263
			</p>
264
		</div><?php
265
	}
266
267
	/**
268
	 * Get the Controller object.
269
	 *
270
	 * @return Writing_On_GitHub_Controller
271
	 */
272
	public function controller() {
273
		return $this->controller;
274
	}
275
276
	/**
277
	 * Lazy-load the CLI object.
278
	 *
279
	 * @return Writing_On_GitHub_CLI
280
	 */
281
	public function cli() {
282
		if ( ! $this->cli ) {
283
			$this->cli = new Writing_On_GitHub_CLI;
284
		}
285
286
		return $this->cli;
287
	}
288
289
	/**
290
	 * Lazy-load the Request object.
291
	 *
292
	 * @return Writing_On_GitHub_Request
293
	 */
294
	public function request() {
295
		if ( ! $this->request ) {
296
			$this->request = new Writing_On_GitHub_Request( $this );
297
		}
298
299
		return $this->request;
300
	}
301
302
	/**
303
	 * Lazy-load the Response object.
304
	 *
305
	 * @return Writing_On_GitHub_Response
306
	 */
307
	public function response() {
308
		if ( ! $this->response ) {
309
			$this->response = new Writing_On_GitHub_Response( $this );
310
		}
311
312
		return $this->response;
313
	}
314
315
	/**
316
	 * Lazy-load the Api object.
317
	 *
318
	 * @return Writing_On_GitHub_Api
319
	 */
320
	public function api() {
321
		if ( ! $this->api ) {
322
			$this->api = new Writing_On_GitHub_Api( $this );
323
		}
324
325
		return $this->api;
326
	}
327
328
	/**
329
	 * Lazy-load the Import object.
330
	 *
331
	 * @return Writing_On_GitHub_Import
332
	 */
333
	public function import() {
334
		if ( ! $this->import ) {
335
			$this->import = new Writing_On_GitHub_Import( $this );
336
		}
337
338
		return $this->import;
339
	}
340
341
	/**
342
	 * Lazy-load the Export object.
343
	 *
344
	 * @return Writing_On_GitHub_Export
345
	 */
346
	public function export() {
347
		if ( ! $this->export ) {
348
			$this->export = new Writing_On_GitHub_Export( $this );
349
		}
350
351
		return $this->export;
352
	}
353
354
	/**
355
	 * Lazy-load the Semaphore object.
356
	 *
357
	 * @return Writing_On_GitHub_Semaphore
358
	 */
359
	public function semaphore() {
360
		if ( ! $this->semaphore ) {
361
			$this->semaphore = new Writing_On_GitHub_Semaphore;
362
		}
363
364
		return $this->semaphore;
365
	}
366
367
	/**
368
	 * Lazy-load the Database object.
369
	 *
370
	 * @return Writing_On_GitHub_Database
371
	 */
372
	public function database() {
373
		if ( ! $this->database ) {
374
			$this->database = new Writing_On_GitHub_Database( $this );
375
		}
376
377
		return $this->database;
378
	}
379
380
	/**
381
	 * Lazy-load the Cache object.
382
	 *
383
	 * @return Writing_On_GitHub_Cache
384
	 */
385
	public function cache() {
386
		if ( ! $this->cache ) {
387
			$this->cache = new Writing_On_GitHub_Cache;
388
		}
389
390
		return $this->cache;
391
	}
392
393
	/**
394
	 * Print to WP_CLI if in CLI environment or
395
	 * write to debug.log if WP_DEBUG is enabled
396
	 * @source http://www.stumiller.me/sending-output-to-the-wordpress-debug-log/
397
	 *
398
	 * @param mixed $msg
399
	 * @param string $write
400
	 */
401
	public static function write_log( $msg, $write = 'line' ) {
402
		if ( defined( 'WP_CLI' ) && WP_CLI ) {
403
			if ( is_array( $msg ) || is_object( $msg ) ) {
404
				WP_CLI::print_value( $msg );
405
			} else {
406
				WP_CLI::$write( $msg );
407
			}
408
		} elseif ( true === WP_DEBUG ) {
409
			if ( is_array( $msg ) || is_object( $msg ) ) {
410
				error_log( print_r( $msg, true ) );
411
			} else {
412
				error_log( $msg );
413
			}
414
		}
415
	}
416
417
	/**
418
	 * Kicks of an import or export cronjob.
419
	 *
420
	 * @param $type
421
	 */
422
	protected function start_cron( $type ) {
423
		update_option( '_wogh_' . $type . '_started', 'yes' );
424
		$user_id = get_current_user_id();
425
		wp_schedule_single_event( time(), 'wogh_' . $type . '', array( $user_id ) );
426
		spawn_cron();
427
	}
428
}
429