litefeel /
writing-on-github
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.5.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.php'; |
||
| 17 | include_once $path; |
||
| 18 | } |
||
| 19 | |||
| 20 | |||
| 21 | // require_once(dirname(__FILE__) . '/Spyc.php'); |
||
| 22 | require_once(dirname(__FILE__) . '/lib/cache.php'); |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 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'); |
||
| 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'); |
||
| 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 | * Controller object |
||
| 62 | * @var Writing_On_GitHub_Controller |
||
| 63 | */ |
||
| 64 | public $controller; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Controller object |
||
| 68 | * @var Writing_On_GitHub_Admin |
||
| 69 | */ |
||
| 70 | public $admin; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * CLI object. |
||
| 74 | * |
||
| 75 | * @var Writing_On_GitHub_CLI |
||
| 76 | */ |
||
| 77 | protected $cli; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Request object. |
||
| 81 | * |
||
| 82 | * @var Writing_On_GitHub_Request |
||
| 83 | */ |
||
| 84 | protected $request; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Response object. |
||
| 88 | * |
||
| 89 | * @var Writing_On_GitHub_Response |
||
| 90 | */ |
||
| 91 | protected $response; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Api object. |
||
| 95 | * |
||
| 96 | * @var Writing_On_GitHub_Api |
||
| 97 | */ |
||
| 98 | protected $api; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Import object. |
||
| 102 | * |
||
| 103 | * @var Writing_On_GitHub_Import |
||
| 104 | */ |
||
| 105 | protected $import; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Export object. |
||
| 109 | * |
||
| 110 | * @var Writing_On_GitHub_Export |
||
| 111 | */ |
||
| 112 | protected $export; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Semaphore object. |
||
| 116 | * |
||
| 117 | * @var Writing_On_GitHub_Semaphore |
||
| 118 | */ |
||
| 119 | protected $semaphore; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Database object. |
||
| 123 | * |
||
| 124 | * @var Writing_On_GitHub_Database |
||
| 125 | */ |
||
| 126 | protected $database; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Cache object. |
||
| 130 | * |
||
| 131 | * @var Writing_On_GitHub_Cache |
||
| 132 | */ |
||
| 133 | protected $cache; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Called at load time, hooks into WP core |
||
| 137 | */ |
||
| 138 | public function __construct() { |
||
| 139 | self::$instance = $this; |
||
| 140 | |||
| 141 | if ( is_admin() ) { |
||
| 142 | $this->admin = new Writing_On_GitHub_Admin; |
||
| 143 | } |
||
| 144 | |||
| 145 | $this->controller = new Writing_On_GitHub_Controller( $this ); |
||
| 146 | |||
| 147 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
||
| 148 | WP_CLI::add_command( 'wogh', $this->cli() ); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Attaches the plugin's hooks into WordPress. |
||
| 154 | */ |
||
| 155 | public function boot() { |
||
| 156 | register_activation_hook( __FILE__, array( $this, 'activate' ) ); |
||
| 157 | add_action( 'admin_notices', array( $this, 'activation_notice' ) ); |
||
| 158 | |||
| 159 | add_action( 'init', array( $this, 'l10n' ) ); |
||
| 160 | |||
| 161 | // Controller actions. |
||
| 162 | add_action( 'save_post', array( $this->controller, 'export_post' ) ); |
||
| 163 | add_action( 'delete_post', array( $this->controller, 'delete_post' ) ); |
||
| 164 | add_action( 'wp_ajax_nopriv_wogh_push_request', array( $this->controller, 'pull_posts' ) ); |
||
| 165 | add_action( 'wogh_export', array( $this->controller, 'export_all' ), 10, 2 ); |
||
| 166 | add_action( 'wogh_import', array( $this->controller, 'import_master' ), 10, 1 ); |
||
| 167 | add_filter( 'get_edit_post_link', array( $this, 'edit_post_link' ), 10, 3 ); |
||
| 168 | |||
| 169 | // add_filter( 'wogh_post_meta', array( $this, 'ignore_post_meta' ), 10, 1 ); |
||
| 170 | // add_filter( 'wogh_pre_import_meta', array( $this, 'ignore_post_meta' ), 10, 1 ); |
||
| 171 | add_filter( 'the_content', array( $this, 'the_content' ) ); |
||
| 172 | |||
| 173 | do_action( 'wogh_boot', $this ); |
||
| 174 | } |
||
| 175 | |||
| 176 | public function edit_post_link($link, $postID, $context) { |
||
| 177 | if ( ! wp_is_post_revision( $postID ) ) { |
||
| 178 | $post = new Writing_On_GitHub_Post( $postID, Writing_On_GitHub::$instance->api() ); |
||
| 179 | if ( $post->is_on_github() ) { |
||
| 180 | return $post->github_edit_url(); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | return $link; |
||
| 185 | } |
||
| 186 | |||
| 187 | public function ignore_post_meta($meta) { |
||
| 188 | $ignore_meta_keys = get_option('wogh_ignore_metas'); |
||
| 189 | if (empty($ignore_meta_keys)) { |
||
| 190 | return $meta; |
||
| 191 | } |
||
| 192 | |||
| 193 | $keys = preg_split("/\\r\\n|\\r|\\n/", $ignore_meta_keys); |
||
| 194 | if (empty($keys)) { |
||
| 195 | return $meta; |
||
| 196 | } |
||
| 197 | foreach ($keys as $key => $value) { |
||
| 198 | $keys[$key] = trim($value); |
||
| 199 | } |
||
| 200 | |||
| 201 | foreach ($meta as $key => $value) { |
||
| 202 | if (in_array($key, $keys)) { |
||
| 203 | unset($meta[$key]); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | return $meta; |
||
| 208 | } |
||
| 209 | |||
| 210 | public function the_content($content) { |
||
| 211 | $arr = wp_upload_dir(); |
||
| 212 | $baseurl = $arr['baseurl'] . '/writing-on-github'; |
||
| 213 | |||
| 214 | $content = preg_replace_callback( |
||
| 215 | '/(<img [^>]*?src=[\'"])\s*(\/images\/[^\s#]\S+)\s*([\'"][^>]*?>)/', |
||
| 216 | View Code Duplication | function($matchs) use ($baseurl) { |
|
| 217 | $url = $baseurl . $matchs[2]; |
||
| 218 | return "${matchs[1]}$url${matchs[3]}"; |
||
| 219 | }, |
||
| 220 | $content |
||
| 221 | ); |
||
| 222 | |||
| 223 | $content = preg_replace_callback( |
||
| 224 | '/(<a [^>]*?href=[\'"])\s*(\/images\/[^\s#]\S+)\s*([\'"][^>]*?>)/', |
||
| 225 | View Code Duplication | function($matchs) use ($baseurl) { |
|
| 226 | $url = $baseurl . $matchs[2]; |
||
| 227 | return "${matchs[1]}$url${matchs[3]}"; |
||
| 228 | }, |
||
| 229 | $content |
||
| 230 | ); |
||
| 231 | return $content; |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Init i18n files |
||
| 236 | */ |
||
| 237 | public function l10n() { |
||
| 238 | load_plugin_textdomain( self::$text_domain ); |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Sets and kicks off the export cronjob |
||
| 243 | */ |
||
| 244 | public function start_export( $force = false ) { |
||
| 245 | $this->start_cron( 'export', $force ); |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Sets and kicks off the import cronjob |
||
| 250 | */ |
||
| 251 | public function start_import() { |
||
| 252 | $this->start_cron( 'import' ); |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Enables the admin notice on initial activation |
||
| 257 | */ |
||
| 258 | public function activate() { |
||
| 259 | if ( 'yes' !== get_option( '_wogh_fully_exported' ) ) { |
||
| 260 | set_transient( '_wogh_activated', 'yes' ); |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Displays the activation admin notice |
||
| 266 | */ |
||
| 267 | public function activation_notice() { |
||
| 268 | if ( ! get_transient( '_wogh_activated' ) ) { |
||
| 269 | return; |
||
| 270 | } |
||
| 271 | |||
| 272 | delete_transient( '_wogh_activated' ); |
||
| 273 | |||
| 274 | ?><div class="updated"> |
||
| 275 | <p> |
||
| 276 | <?php |
||
| 277 | printf( |
||
| 278 | __( 'To set up your site to sync with GitHub, update your <a href="%s">settings</a> and click "Export to GitHub."', 'writing-on-github' ), |
||
| 279 | admin_url( 'options-general.php?page=' . static::$text_domain) |
||
| 280 | ); |
||
| 281 | ?> |
||
| 282 | </p> |
||
| 283 | </div><?php |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Get the Controller object. |
||
| 288 | * |
||
| 289 | * @return Writing_On_GitHub_Controller |
||
| 290 | */ |
||
| 291 | public function controller() { |
||
| 292 | return $this->controller; |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Lazy-load the CLI object. |
||
| 297 | * |
||
| 298 | * @return Writing_On_GitHub_CLI |
||
| 299 | */ |
||
| 300 | public function cli() { |
||
| 301 | if ( ! $this->cli ) { |
||
| 302 | $this->cli = new Writing_On_GitHub_CLI; |
||
| 303 | } |
||
| 304 | |||
| 305 | return $this->cli; |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Lazy-load the Request object. |
||
| 310 | * |
||
| 311 | * @return Writing_On_GitHub_Request |
||
| 312 | */ |
||
| 313 | public function request() { |
||
| 314 | if ( ! $this->request ) { |
||
| 315 | $this->request = new Writing_On_GitHub_Request( $this ); |
||
| 316 | } |
||
| 317 | |||
| 318 | return $this->request; |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Lazy-load the Response object. |
||
| 323 | * |
||
| 324 | * @return Writing_On_GitHub_Response |
||
| 325 | */ |
||
| 326 | public function response() { |
||
| 327 | if ( ! $this->response ) { |
||
| 328 | $this->response = new Writing_On_GitHub_Response( $this ); |
||
| 329 | } |
||
| 330 | |||
| 331 | return $this->response; |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Lazy-load the Api object. |
||
| 336 | * |
||
| 337 | * @return Writing_On_GitHub_Api |
||
| 338 | */ |
||
| 339 | public function api() { |
||
| 340 | if ( ! $this->api ) { |
||
| 341 | $this->api = new Writing_On_GitHub_Api( $this ); |
||
| 342 | } |
||
| 343 | |||
| 344 | return $this->api; |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Lazy-load the Import object. |
||
| 349 | * |
||
| 350 | * @return Writing_On_GitHub_Import |
||
| 351 | */ |
||
| 352 | public function import() { |
||
| 353 | if ( ! $this->import ) { |
||
| 354 | $this->import = new Writing_On_GitHub_Import( $this ); |
||
| 355 | } |
||
| 356 | |||
| 357 | return $this->import; |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Lazy-load the Export object. |
||
| 362 | * |
||
| 363 | * @return Writing_On_GitHub_Export |
||
| 364 | */ |
||
| 365 | public function export() { |
||
| 366 | if ( ! $this->export ) { |
||
| 367 | $this->export = new Writing_On_GitHub_Export( $this ); |
||
| 368 | } |
||
| 369 | |||
| 370 | return $this->export; |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Lazy-load the Semaphore object. |
||
| 375 | * |
||
| 376 | * @return Writing_On_GitHub_Semaphore |
||
| 377 | */ |
||
| 378 | public function semaphore() { |
||
| 379 | if ( ! $this->semaphore ) { |
||
| 380 | $this->semaphore = new Writing_On_GitHub_Semaphore; |
||
| 381 | } |
||
| 382 | |||
| 383 | return $this->semaphore; |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Lazy-load the Database object. |
||
| 388 | * |
||
| 389 | * @return Writing_On_GitHub_Database |
||
| 390 | */ |
||
| 391 | public function database() { |
||
| 392 | if ( ! $this->database ) { |
||
| 393 | $this->database = new Writing_On_GitHub_Database( $this ); |
||
| 394 | } |
||
| 395 | |||
| 396 | return $this->database; |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Lazy-load the Cache object. |
||
| 401 | * |
||
| 402 | * @return Writing_On_GitHub_Cache |
||
| 403 | */ |
||
| 404 | public function cache() { |
||
| 405 | if ( ! $this->cache ) { |
||
| 406 | $this->cache = new Writing_On_GitHub_Cache; |
||
| 407 | } |
||
| 408 | |||
| 409 | return $this->cache; |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Print to WP_CLI if in CLI environment or |
||
| 414 | * write to debug.log if WP_DEBUG is enabled |
||
| 415 | * @source http://www.stumiller.me/sending-output-to-the-wordpress-debug-log/ |
||
| 416 | * |
||
| 417 | * @param mixed $msg |
||
| 418 | * @param string $write |
||
| 419 | */ |
||
| 420 | public static function write_log( $msg, $write = 'line' ) { |
||
| 421 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
||
| 422 | if ( is_array( $msg ) || is_object( $msg ) ) { |
||
| 423 | WP_CLI::print_value( $msg ); |
||
| 424 | } else { |
||
| 425 | WP_CLI::$write( $msg ); |
||
| 426 | } |
||
| 427 | } elseif ( true === WP_DEBUG ) { |
||
| 428 | if ( is_array( $msg ) || is_object( $msg ) ) { |
||
| 429 | error_log( print_r( $msg, true ) ); |
||
| 430 | } else { |
||
| 431 | error_log( $msg ); |
||
| 432 | } |
||
| 433 | } |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Kicks of an import or export cronjob. |
||
| 438 | * |
||
| 439 | * @param bool $force |
||
| 440 | * @param string $type |
||
| 441 | */ |
||
| 442 | protected function start_cron( $type, $force = false ) { |
||
| 443 | update_option( '_wogh_' . $type . '_started', 'yes' ); |
||
| 444 | $user_id = get_current_user_id(); |
||
| 445 | wp_schedule_single_event( time(), 'wogh_' . $type . '', array( $user_id, $force ) ); |
||
| 446 | spawn_cron(); |
||
| 447 | } |
||
| 448 | } |
||
| 449 |