| @@ -14,391 +14,391 @@ | ||
| 14 | 14 | // This fixes function duplication during unit testing. | 
| 15 | 15 | $path = dirname( __FILE__ ) . '/vendor/autoload.php'; | 
| 16 | 16 |  if ( file_exists( $path ) ) { | 
| 17 | - require_once( $path ); | |
| 17 | + require_once( $path ); | |
| 18 | 18 | } | 
| 19 | 19 | |
| 20 | 20 | add_action( 'plugins_loaded', array( new Writing_On_GitHub, 'boot' ) ); | 
| 21 | 21 | |
| 22 | 22 |  class Writing_On_GitHub { | 
| 23 | 23 | |
| 24 | - /** | |
| 25 | - * Object instance | |
| 26 | - * @var self | |
| 27 | - */ | |
| 28 | - public static $instance; | |
| 29 | - | |
| 30 | - /** | |
| 31 | - * Language text domain | |
| 32 | - * @var string | |
| 33 | - */ | |
| 34 | - public static $text_domain = 'writing-on-github'; | |
| 35 | - | |
| 36 | - /** | |
| 37 | - * Controller object | |
| 38 | - * @var Writing_On_GitHub_Controller | |
| 39 | - */ | |
| 40 | - public $controller; | |
| 41 | - | |
| 42 | - /** | |
| 43 | - * Controller object | |
| 44 | - * @var Writing_On_GitHub_Admin | |
| 45 | - */ | |
| 46 | - public $admin; | |
| 47 | - | |
| 48 | - /** | |
| 49 | - * CLI object. | |
| 50 | - * | |
| 51 | - * @var Writing_On_GitHub_CLI | |
| 52 | - */ | |
| 53 | - protected $cli; | |
| 54 | - | |
| 55 | - /** | |
| 56 | - * Request object. | |
| 57 | - * | |
| 58 | - * @var Writing_On_GitHub_Request | |
| 59 | - */ | |
| 60 | - protected $request; | |
| 61 | - | |
| 62 | - /** | |
| 63 | - * Response object. | |
| 64 | - * | |
| 65 | - * @var Writing_On_GitHub_Response | |
| 66 | - */ | |
| 67 | - protected $response; | |
| 68 | - | |
| 69 | - /** | |
| 70 | - * Api object. | |
| 71 | - * | |
| 72 | - * @var Writing_On_GitHub_Api | |
| 73 | - */ | |
| 74 | - protected $api; | |
| 75 | - | |
| 76 | - /** | |
| 77 | - * Import object. | |
| 78 | - * | |
| 79 | - * @var Writing_On_GitHub_Import | |
| 80 | - */ | |
| 81 | - protected $import; | |
| 82 | - | |
| 83 | - /** | |
| 84 | - * Export object. | |
| 85 | - * | |
| 86 | - * @var Writing_On_GitHub_Export | |
| 87 | - */ | |
| 88 | - protected $export; | |
| 89 | - | |
| 90 | - /** | |
| 91 | - * Semaphore object. | |
| 92 | - * | |
| 93 | - * @var Writing_On_GitHub_Semaphore | |
| 94 | - */ | |
| 95 | - protected $semaphore; | |
| 96 | - | |
| 97 | - /** | |
| 98 | - * Database object. | |
| 99 | - * | |
| 100 | - * @var Writing_On_GitHub_Database | |
| 101 | - */ | |
| 102 | - protected $database; | |
| 103 | - | |
| 104 | - /** | |
| 105 | - * Called at load time, hooks into WP core | |
| 106 | - */ | |
| 107 | -    public function __construct() { | |
| 108 | - self::$instance = $this; | |
| 109 | - | |
| 110 | -        if ( is_admin() ) { | |
| 111 | - $this->admin = new Writing_On_GitHub_Admin( plugin_basename( __FILE__ ) ); | |
| 112 | - } | |
| 113 | - | |
| 114 | - $this->controller = new Writing_On_GitHub_Controller( $this ); | |
| 115 | - | |
| 116 | -        if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
| 117 | - WP_CLI::add_command( 'wogh', $this->cli() ); | |
| 118 | - } | |
| 119 | - } | |
| 120 | - | |
| 121 | - /** | |
| 122 | - * Attaches the plugin's hooks into WordPress. | |
| 123 | - */ | |
| 124 | -    public function boot() { | |
| 125 | - register_activation_hook( __FILE__, array( $this, 'activate' ) ); | |
| 126 | - add_action( 'admin_notices', array( $this, 'activation_notice' ) ); | |
| 127 | - | |
| 128 | - add_action( 'init', array( $this, 'l10n' ) ); | |
| 129 | - | |
| 130 | - // Controller actions. | |
| 131 | - add_action( 'save_post', array( $this->controller, 'export_post' ) ); | |
| 132 | - add_action( 'delete_post', array( $this->controller, 'delete_post' ) ); | |
| 133 | - add_action( 'wp_ajax_nopriv_wogh_push_request', array( $this->controller, 'pull_posts' ) ); | |
| 134 | - add_action( 'wogh_export', array( $this->controller, 'export_all' ), 10, 2 ); | |
| 135 | - add_action( 'wogh_import', array( $this->controller, 'import_master' ), 10, 2 ); | |
| 136 | - add_filter( 'get_edit_post_link', array( $this, 'edit_post_link' ), 10, 3 ); | |
| 137 | - | |
| 138 | - // add_filter( 'wogh_post_meta', array( $this, 'ignore_post_meta' ), 10, 1 ); | |
| 139 | - // add_filter( 'wogh_pre_import_meta', array( $this, 'ignore_post_meta' ), 10, 1 ); | |
| 140 | - add_filter( 'the_content', array( $this, 'the_content' ) ); | |
| 141 | - | |
| 142 | - do_action( 'wogh_boot', $this ); | |
| 143 | - } | |
| 144 | - | |
| 145 | -    public function edit_post_link($link, $postID, $context) { | |
| 146 | -        if ( ! wp_is_post_revision( $postID ) ) { | |
| 147 | - $post = new Writing_On_GitHub_Post( $postID, Writing_On_GitHub::$instance->api() ); | |
| 148 | -            if ( $post->is_on_github() ) { | |
| 149 | - return $post->github_edit_url(); | |
| 150 | - } | |
| 151 | - } | |
| 152 | - | |
| 153 | - return $link; | |
| 154 | - } | |
| 155 | - | |
| 156 | -    public function ignore_post_meta($meta) { | |
| 157 | -        $ignore_meta_keys = get_option('wogh_ignore_metas'); | |
| 158 | -        if (empty($ignore_meta_keys)) { | |
| 159 | - return $meta; | |
| 160 | - } | |
| 161 | - | |
| 162 | -        $keys = preg_split("/\\r\\n|\\r|\\n/", $ignore_meta_keys); | |
| 163 | -        if (empty($keys)) { | |
| 164 | - return $meta; | |
| 165 | - } | |
| 166 | -        foreach ($keys as $key => $value) { | |
| 167 | - $keys[$key] = trim($value); | |
| 168 | - } | |
| 169 | - | |
| 170 | -        foreach ($meta as $key => $value) { | |
| 171 | -            if (in_array($key, $keys)) { | |
| 172 | - unset($meta[$key]); | |
| 173 | - } | |
| 174 | - } | |
| 175 | - | |
| 176 | - return $meta; | |
| 177 | - } | |
| 178 | - | |
| 179 | -    public function the_content($content) { | |
| 180 | - $arr = wp_upload_dir(); | |
| 181 | - $baseurl = $arr['baseurl'] . '/writing-on-github'; | |
| 182 | - | |
| 183 | - $content = preg_replace_callback( | |
| 184 | - '/(<img [^>]*?src=[\'"])\s*(\/images\/[^\s#]\S+)\s*([\'"][^>]*?>)/', | |
| 185 | -            function($matchs) use ($baseurl) { | |
| 186 | - $url = $baseurl . $matchs[2]; | |
| 187 | -                return "${matchs[1]}$url${matchs[3]}"; | |
| 188 | - }, | |
| 189 | - $content | |
| 190 | - ); | |
| 191 | - | |
| 192 | - $content = preg_replace_callback( | |
| 193 | - '/(<a [^>]*?href=[\'"])\s*(\/images\/[^\s#]\S+)\s*([\'"][^>]*?>)/', | |
| 194 | -            function($matchs) use ($baseurl) { | |
| 195 | - $url = $baseurl . $matchs[2]; | |
| 196 | -                return "${matchs[1]}$url${matchs[3]}"; | |
| 197 | - }, | |
| 198 | - $content | |
| 199 | - ); | |
| 200 | - return $content; | |
| 201 | - } | |
| 202 | - | |
| 203 | - /** | |
| 204 | - * Init i18n files | |
| 205 | - */ | |
| 206 | -    public function l10n() { | |
| 207 | - load_plugin_textdomain( self::$text_domain ); | |
| 208 | - } | |
| 209 | - | |
| 210 | - /** | |
| 211 | - * Sets and kicks off the export cronjob | |
| 212 | - */ | |
| 213 | -    public function start_export( $force = false ) { | |
| 214 | - $this->start_cron( 'export', $force ); | |
| 215 | - } | |
| 216 | - | |
| 217 | - /** | |
| 218 | - * Sets and kicks off the import cronjob | |
| 219 | - */ | |
| 220 | -    public function start_import( $force = false ) { | |
| 221 | - $this->start_cron( 'import', $force ); | |
| 222 | - } | |
| 223 | - | |
| 224 | - /** | |
| 225 | - * Enables the admin notice on initial activation | |
| 226 | - */ | |
| 227 | -    public function activate() { | |
| 228 | -        if ( 'yes' !== get_option( '_wogh_fully_exported' ) ) { | |
| 229 | - set_transient( '_wogh_activated', 'yes' ); | |
| 230 | - } | |
| 231 | - } | |
| 232 | - | |
| 233 | - /** | |
| 234 | - * Displays the activation admin notice | |
| 235 | - */ | |
| 236 | -    public function activation_notice() { | |
| 237 | -        if ( ! get_transient( '_wogh_activated' ) ) { | |
| 238 | - return; | |
| 239 | - } | |
| 240 | - | |
| 241 | - delete_transient( '_wogh_activated' ); | |
| 242 | - | |
| 243 | - ?><div class="updated"> | |
| 24 | + /** | |
| 25 | + * Object instance | |
| 26 | + * @var self | |
| 27 | + */ | |
| 28 | + public static $instance; | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * Language text domain | |
| 32 | + * @var string | |
| 33 | + */ | |
| 34 | + public static $text_domain = 'writing-on-github'; | |
| 35 | + | |
| 36 | + /** | |
| 37 | + * Controller object | |
| 38 | + * @var Writing_On_GitHub_Controller | |
| 39 | + */ | |
| 40 | + public $controller; | |
| 41 | + | |
| 42 | + /** | |
| 43 | + * Controller object | |
| 44 | + * @var Writing_On_GitHub_Admin | |
| 45 | + */ | |
| 46 | + public $admin; | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * CLI object. | |
| 50 | + * | |
| 51 | + * @var Writing_On_GitHub_CLI | |
| 52 | + */ | |
| 53 | + protected $cli; | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * Request object. | |
| 57 | + * | |
| 58 | + * @var Writing_On_GitHub_Request | |
| 59 | + */ | |
| 60 | + protected $request; | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * Response object. | |
| 64 | + * | |
| 65 | + * @var Writing_On_GitHub_Response | |
| 66 | + */ | |
| 67 | + protected $response; | |
| 68 | + | |
| 69 | + /** | |
| 70 | + * Api object. | |
| 71 | + * | |
| 72 | + * @var Writing_On_GitHub_Api | |
| 73 | + */ | |
| 74 | + protected $api; | |
| 75 | + | |
| 76 | + /** | |
| 77 | + * Import object. | |
| 78 | + * | |
| 79 | + * @var Writing_On_GitHub_Import | |
| 80 | + */ | |
| 81 | + protected $import; | |
| 82 | + | |
| 83 | + /** | |
| 84 | + * Export object. | |
| 85 | + * | |
| 86 | + * @var Writing_On_GitHub_Export | |
| 87 | + */ | |
| 88 | + protected $export; | |
| 89 | + | |
| 90 | + /** | |
| 91 | + * Semaphore object. | |
| 92 | + * | |
| 93 | + * @var Writing_On_GitHub_Semaphore | |
| 94 | + */ | |
| 95 | + protected $semaphore; | |
| 96 | + | |
| 97 | + /** | |
| 98 | + * Database object. | |
| 99 | + * | |
| 100 | + * @var Writing_On_GitHub_Database | |
| 101 | + */ | |
| 102 | + protected $database; | |
| 103 | + | |
| 104 | + /** | |
| 105 | + * Called at load time, hooks into WP core | |
| 106 | + */ | |
| 107 | +	public function __construct() { | |
| 108 | + self::$instance = $this; | |
| 109 | + | |
| 110 | +		if ( is_admin() ) { | |
| 111 | + $this->admin = new Writing_On_GitHub_Admin( plugin_basename( __FILE__ ) ); | |
| 112 | + } | |
| 113 | + | |
| 114 | + $this->controller = new Writing_On_GitHub_Controller( $this ); | |
| 115 | + | |
| 116 | +		if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
| 117 | + WP_CLI::add_command( 'wogh', $this->cli() ); | |
| 118 | + } | |
| 119 | + } | |
| 120 | + | |
| 121 | + /** | |
| 122 | + * Attaches the plugin's hooks into WordPress. | |
| 123 | + */ | |
| 124 | +	public function boot() { | |
| 125 | + register_activation_hook( __FILE__, array( $this, 'activate' ) ); | |
| 126 | + add_action( 'admin_notices', array( $this, 'activation_notice' ) ); | |
| 127 | + | |
| 128 | + add_action( 'init', array( $this, 'l10n' ) ); | |
| 129 | + | |
| 130 | + // Controller actions. | |
| 131 | + add_action( 'save_post', array( $this->controller, 'export_post' ) ); | |
| 132 | + add_action( 'delete_post', array( $this->controller, 'delete_post' ) ); | |
| 133 | + add_action( 'wp_ajax_nopriv_wogh_push_request', array( $this->controller, 'pull_posts' ) ); | |
| 134 | + add_action( 'wogh_export', array( $this->controller, 'export_all' ), 10, 2 ); | |
| 135 | + add_action( 'wogh_import', array( $this->controller, 'import_master' ), 10, 2 ); | |
| 136 | + add_filter( 'get_edit_post_link', array( $this, 'edit_post_link' ), 10, 3 ); | |
| 137 | + | |
| 138 | + // add_filter( 'wogh_post_meta', array( $this, 'ignore_post_meta' ), 10, 1 ); | |
| 139 | + // add_filter( 'wogh_pre_import_meta', array( $this, 'ignore_post_meta' ), 10, 1 ); | |
| 140 | + add_filter( 'the_content', array( $this, 'the_content' ) ); | |
| 141 | + | |
| 142 | + do_action( 'wogh_boot', $this ); | |
| 143 | + } | |
| 144 | + | |
| 145 | +	public function edit_post_link($link, $postID, $context) { | |
| 146 | +		if ( ! wp_is_post_revision( $postID ) ) { | |
| 147 | + $post = new Writing_On_GitHub_Post( $postID, Writing_On_GitHub::$instance->api() ); | |
| 148 | +			if ( $post->is_on_github() ) { | |
| 149 | + return $post->github_edit_url(); | |
| 150 | + } | |
| 151 | + } | |
| 152 | + | |
| 153 | + return $link; | |
| 154 | + } | |
| 155 | + | |
| 156 | +	public function ignore_post_meta($meta) { | |
| 157 | +		$ignore_meta_keys = get_option('wogh_ignore_metas'); | |
| 158 | +		if (empty($ignore_meta_keys)) { | |
| 159 | + return $meta; | |
| 160 | + } | |
| 161 | + | |
| 162 | +		$keys = preg_split("/\\r\\n|\\r|\\n/", $ignore_meta_keys); | |
| 163 | +		if (empty($keys)) { | |
| 164 | + return $meta; | |
| 165 | + } | |
| 166 | +		foreach ($keys as $key => $value) { | |
| 167 | + $keys[$key] = trim($value); | |
| 168 | + } | |
| 169 | + | |
| 170 | +		foreach ($meta as $key => $value) { | |
| 171 | +			if (in_array($key, $keys)) { | |
| 172 | + unset($meta[$key]); | |
| 173 | + } | |
| 174 | + } | |
| 175 | + | |
| 176 | + return $meta; | |
| 177 | + } | |
| 178 | + | |
| 179 | +	public function the_content($content) { | |
| 180 | + $arr = wp_upload_dir(); | |
| 181 | + $baseurl = $arr['baseurl'] . '/writing-on-github'; | |
| 182 | + | |
| 183 | + $content = preg_replace_callback( | |
| 184 | + '/(<img [^>]*?src=[\'"])\s*(\/images\/[^\s#]\S+)\s*([\'"][^>]*?>)/', | |
| 185 | +			function($matchs) use ($baseurl) { | |
| 186 | + $url = $baseurl . $matchs[2]; | |
| 187 | +				return "${matchs[1]}$url${matchs[3]}"; | |
| 188 | + }, | |
| 189 | + $content | |
| 190 | + ); | |
| 191 | + | |
| 192 | + $content = preg_replace_callback( | |
| 193 | + '/(<a [^>]*?href=[\'"])\s*(\/images\/[^\s#]\S+)\s*([\'"][^>]*?>)/', | |
| 194 | +			function($matchs) use ($baseurl) { | |
| 195 | + $url = $baseurl . $matchs[2]; | |
| 196 | +				return "${matchs[1]}$url${matchs[3]}"; | |
| 197 | + }, | |
| 198 | + $content | |
| 199 | + ); | |
| 200 | + return $content; | |
| 201 | + } | |
| 202 | + | |
| 203 | + /** | |
| 204 | + * Init i18n files | |
| 205 | + */ | |
| 206 | +	public function l10n() { | |
| 207 | + load_plugin_textdomain( self::$text_domain ); | |
| 208 | + } | |
| 209 | + | |
| 210 | + /** | |
| 211 | + * Sets and kicks off the export cronjob | |
| 212 | + */ | |
| 213 | +	public function start_export( $force = false ) { | |
| 214 | + $this->start_cron( 'export', $force ); | |
| 215 | + } | |
| 216 | + | |
| 217 | + /** | |
| 218 | + * Sets and kicks off the import cronjob | |
| 219 | + */ | |
| 220 | +	public function start_import( $force = false ) { | |
| 221 | + $this->start_cron( 'import', $force ); | |
| 222 | + } | |
| 223 | + | |
| 224 | + /** | |
| 225 | + * Enables the admin notice on initial activation | |
| 226 | + */ | |
| 227 | +	public function activate() { | |
| 228 | +		if ( 'yes' !== get_option( '_wogh_fully_exported' ) ) { | |
| 229 | + set_transient( '_wogh_activated', 'yes' ); | |
| 230 | + } | |
| 231 | + } | |
| 232 | + | |
| 233 | + /** | |
| 234 | + * Displays the activation admin notice | |
| 235 | + */ | |
| 236 | +	public function activation_notice() { | |
| 237 | +		if ( ! get_transient( '_wogh_activated' ) ) { | |
| 238 | + return; | |
| 239 | + } | |
| 240 | + | |
| 241 | + delete_transient( '_wogh_activated' ); | |
| 242 | + | |
| 243 | + ?><div class="updated"> | |
| 244 | 244 | <p> | 
| 245 | 245 | <?php | 
| 246 | - printf( | |
| 247 | - __( 'To set up your site to sync with GitHub, update your <a href="%s">settings</a> and click "Export to GitHub."', 'writing-on-github' ), | |
| 248 | - admin_url( 'options-general.php?page=' . static::$text_domain) | |
| 249 | - ); | |
| 250 | - ?> | |
| 246 | + printf( | |
| 247 | + __( 'To set up your site to sync with GitHub, update your <a href="%s">settings</a> and click "Export to GitHub."', 'writing-on-github' ), | |
| 248 | + admin_url( 'options-general.php?page=' . static::$text_domain) | |
| 249 | + ); | |
| 250 | + ?> | |
| 251 | 251 | </p> | 
| 252 | 252 | </div><?php | 
| 253 | - } | |
| 254 | - | |
| 255 | - /** | |
| 256 | - * Get the Controller object. | |
| 257 | - * | |
| 258 | - * @return Writing_On_GitHub_Controller | |
| 259 | - */ | |
| 260 | -    public function controller() { | |
| 261 | - return $this->controller; | |
| 262 | - } | |
| 263 | - | |
| 264 | - /** | |
| 265 | - * Lazy-load the CLI object. | |
| 266 | - * | |
| 267 | - * @return Writing_On_GitHub_CLI | |
| 268 | - */ | |
| 269 | -    public function cli() { | |
| 270 | -        if ( ! $this->cli ) { | |
| 271 | - $this->cli = new Writing_On_GitHub_CLI; | |
| 272 | - } | |
| 273 | - | |
| 274 | - return $this->cli; | |
| 275 | - } | |
| 276 | - | |
| 277 | - /** | |
| 278 | - * Lazy-load the Request object. | |
| 279 | - * | |
| 280 | - * @return Writing_On_GitHub_Request | |
| 281 | - */ | |
| 282 | -    public function request() { | |
| 283 | -        if ( ! $this->request ) { | |
| 284 | - $this->request = new Writing_On_GitHub_Request( $this ); | |
| 285 | - } | |
| 286 | - | |
| 287 | - return $this->request; | |
| 288 | - } | |
| 289 | - | |
| 290 | - /** | |
| 291 | - * Lazy-load the Response object. | |
| 292 | - * | |
| 293 | - * @return Writing_On_GitHub_Response | |
| 294 | - */ | |
| 295 | -    public function response() { | |
| 296 | -        if ( ! $this->response ) { | |
| 297 | - $this->response = new Writing_On_GitHub_Response( $this ); | |
| 298 | - } | |
| 299 | - | |
| 300 | - return $this->response; | |
| 301 | - } | |
| 302 | - | |
| 303 | - /** | |
| 304 | - * Lazy-load the Api object. | |
| 305 | - * | |
| 306 | - * @return Writing_On_GitHub_Api | |
| 307 | - */ | |
| 308 | -    public function api() { | |
| 309 | -        if ( ! $this->api ) { | |
| 310 | - $this->api = new Writing_On_GitHub_Api( $this ); | |
| 311 | - } | |
| 312 | - | |
| 313 | - return $this->api; | |
| 314 | - } | |
| 315 | - | |
| 316 | - /** | |
| 317 | - * Lazy-load the Import object. | |
| 318 | - * | |
| 319 | - * @return Writing_On_GitHub_Import | |
| 320 | - */ | |
| 321 | -    public function import() { | |
| 322 | -        if ( ! $this->import ) { | |
| 323 | - $this->import = new Writing_On_GitHub_Import( $this ); | |
| 324 | - } | |
| 325 | - | |
| 326 | - return $this->import; | |
| 327 | - } | |
| 328 | - | |
| 329 | - /** | |
| 330 | - * Lazy-load the Export object. | |
| 331 | - * | |
| 332 | - * @return Writing_On_GitHub_Export | |
| 333 | - */ | |
| 334 | -    public function export() { | |
| 335 | -        if ( ! $this->export ) { | |
| 336 | - $this->export = new Writing_On_GitHub_Export( $this ); | |
| 337 | - } | |
| 338 | - | |
| 339 | - return $this->export; | |
| 340 | - } | |
| 341 | - | |
| 342 | - /** | |
| 343 | - * Lazy-load the Semaphore object. | |
| 344 | - * | |
| 345 | - * @return Writing_On_GitHub_Semaphore | |
| 346 | - */ | |
| 347 | -    public function semaphore() { | |
| 348 | -        if ( ! $this->semaphore ) { | |
| 349 | - $this->semaphore = new Writing_On_GitHub_Semaphore; | |
| 350 | - } | |
| 351 | - | |
| 352 | - return $this->semaphore; | |
| 353 | - } | |
| 354 | - | |
| 355 | - /** | |
| 356 | - * Lazy-load the Database object. | |
| 357 | - * | |
| 358 | - * @return Writing_On_GitHub_Database | |
| 359 | - */ | |
| 360 | -    public function database() { | |
| 361 | -        if ( ! $this->database ) { | |
| 362 | - $this->database = new Writing_On_GitHub_Database( $this ); | |
| 363 | - } | |
| 364 | - | |
| 365 | - return $this->database; | |
| 366 | - } | |
| 367 | - | |
| 368 | - /** | |
| 369 | - * Print to WP_CLI if in CLI environment or | |
| 370 | - * write to debug.log if WP_DEBUG is enabled | |
| 371 | - * @source http://www.stumiller.me/sending-output-to-the-wordpress-debug-log/ | |
| 372 | - * | |
| 373 | - * @param mixed $msg | |
| 374 | - * @param string $write | |
| 375 | - */ | |
| 376 | -    public static function write_log( $msg, $write = 'line' ) { | |
| 377 | -        if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
| 378 | -            if ( is_array( $msg ) || is_object( $msg ) ) { | |
| 379 | - WP_CLI::print_value( $msg ); | |
| 380 | -            } else { | |
| 381 | - WP_CLI::$write( $msg ); | |
| 382 | - } | |
| 383 | -        } elseif ( true === WP_DEBUG ) { | |
| 384 | -            if ( is_array( $msg ) || is_object( $msg ) ) { | |
| 385 | - error_log( print_r( $msg, true ) ); | |
| 386 | -            } else { | |
| 387 | - error_log( $msg ); | |
| 388 | - } | |
| 389 | - } | |
| 390 | - } | |
| 391 | - | |
| 392 | - /** | |
| 393 | - * Kicks of an import or export cronjob. | |
| 394 | - * | |
| 395 | - * @param bool $force | |
| 396 | - * @param string $type | |
| 397 | - */ | |
| 398 | -    protected function start_cron( $type, $force = false ) { | |
| 399 | - update_option( '_wogh_' . $type . '_started', 'yes' ); | |
| 400 | - $user_id = get_current_user_id(); | |
| 401 | - wp_schedule_single_event( time(), 'wogh_' . $type . '', array( $user_id, $force ) ); | |
| 402 | - spawn_cron(); | |
| 403 | - } | |
| 253 | + } | |
| 254 | + | |
| 255 | + /** | |
| 256 | + * Get the Controller object. | |
| 257 | + * | |
| 258 | + * @return Writing_On_GitHub_Controller | |
| 259 | + */ | |
| 260 | +	public function controller() { | |
| 261 | + return $this->controller; | |
| 262 | + } | |
| 263 | + | |
| 264 | + /** | |
| 265 | + * Lazy-load the CLI object. | |
| 266 | + * | |
| 267 | + * @return Writing_On_GitHub_CLI | |
| 268 | + */ | |
| 269 | +	public function cli() { | |
| 270 | +		if ( ! $this->cli ) { | |
| 271 | + $this->cli = new Writing_On_GitHub_CLI; | |
| 272 | + } | |
| 273 | + | |
| 274 | + return $this->cli; | |
| 275 | + } | |
| 276 | + | |
| 277 | + /** | |
| 278 | + * Lazy-load the Request object. | |
| 279 | + * | |
| 280 | + * @return Writing_On_GitHub_Request | |
| 281 | + */ | |
| 282 | +	public function request() { | |
| 283 | +		if ( ! $this->request ) { | |
| 284 | + $this->request = new Writing_On_GitHub_Request( $this ); | |
| 285 | + } | |
| 286 | + | |
| 287 | + return $this->request; | |
| 288 | + } | |
| 289 | + | |
| 290 | + /** | |
| 291 | + * Lazy-load the Response object. | |
| 292 | + * | |
| 293 | + * @return Writing_On_GitHub_Response | |
| 294 | + */ | |
| 295 | +	public function response() { | |
| 296 | +		if ( ! $this->response ) { | |
| 297 | + $this->response = new Writing_On_GitHub_Response( $this ); | |
| 298 | + } | |
| 299 | + | |
| 300 | + return $this->response; | |
| 301 | + } | |
| 302 | + | |
| 303 | + /** | |
| 304 | + * Lazy-load the Api object. | |
| 305 | + * | |
| 306 | + * @return Writing_On_GitHub_Api | |
| 307 | + */ | |
| 308 | +	public function api() { | |
| 309 | +		if ( ! $this->api ) { | |
| 310 | + $this->api = new Writing_On_GitHub_Api( $this ); | |
| 311 | + } | |
| 312 | + | |
| 313 | + return $this->api; | |
| 314 | + } | |
| 315 | + | |
| 316 | + /** | |
| 317 | + * Lazy-load the Import object. | |
| 318 | + * | |
| 319 | + * @return Writing_On_GitHub_Import | |
| 320 | + */ | |
| 321 | +	public function import() { | |
| 322 | +		if ( ! $this->import ) { | |
| 323 | + $this->import = new Writing_On_GitHub_Import( $this ); | |
| 324 | + } | |
| 325 | + | |
| 326 | + return $this->import; | |
| 327 | + } | |
| 328 | + | |
| 329 | + /** | |
| 330 | + * Lazy-load the Export object. | |
| 331 | + * | |
| 332 | + * @return Writing_On_GitHub_Export | |
| 333 | + */ | |
| 334 | +	public function export() { | |
| 335 | +		if ( ! $this->export ) { | |
| 336 | + $this->export = new Writing_On_GitHub_Export( $this ); | |
| 337 | + } | |
| 338 | + | |
| 339 | + return $this->export; | |
| 340 | + } | |
| 341 | + | |
| 342 | + /** | |
| 343 | + * Lazy-load the Semaphore object. | |
| 344 | + * | |
| 345 | + * @return Writing_On_GitHub_Semaphore | |
| 346 | + */ | |
| 347 | +	public function semaphore() { | |
| 348 | +		if ( ! $this->semaphore ) { | |
| 349 | + $this->semaphore = new Writing_On_GitHub_Semaphore; | |
| 350 | + } | |
| 351 | + | |
| 352 | + return $this->semaphore; | |
| 353 | + } | |
| 354 | + | |
| 355 | + /** | |
| 356 | + * Lazy-load the Database object. | |
| 357 | + * | |
| 358 | + * @return Writing_On_GitHub_Database | |
| 359 | + */ | |
| 360 | +	public function database() { | |
| 361 | +		if ( ! $this->database ) { | |
| 362 | + $this->database = new Writing_On_GitHub_Database( $this ); | |
| 363 | + } | |
| 364 | + | |
| 365 | + return $this->database; | |
| 366 | + } | |
| 367 | + | |
| 368 | + /** | |
| 369 | + * Print to WP_CLI if in CLI environment or | |
| 370 | + * write to debug.log if WP_DEBUG is enabled | |
| 371 | + * @source http://www.stumiller.me/sending-output-to-the-wordpress-debug-log/ | |
| 372 | + * | |
| 373 | + * @param mixed $msg | |
| 374 | + * @param string $write | |
| 375 | + */ | |
| 376 | +	public static function write_log( $msg, $write = 'line' ) { | |
| 377 | +		if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
| 378 | +			if ( is_array( $msg ) || is_object( $msg ) ) { | |
| 379 | + WP_CLI::print_value( $msg ); | |
| 380 | +			} else { | |
| 381 | + WP_CLI::$write( $msg ); | |
| 382 | + } | |
| 383 | +		} elseif ( true === WP_DEBUG ) { | |
| 384 | +			if ( is_array( $msg ) || is_object( $msg ) ) { | |
| 385 | + error_log( print_r( $msg, true ) ); | |
| 386 | +			} else { | |
| 387 | + error_log( $msg ); | |
| 388 | + } | |
| 389 | + } | |
| 390 | + } | |
| 391 | + | |
| 392 | + /** | |
| 393 | + * Kicks of an import or export cronjob. | |
| 394 | + * | |
| 395 | + * @param bool $force | |
| 396 | + * @param string $type | |
| 397 | + */ | |
| 398 | +	protected function start_cron( $type, $force = false ) { | |
| 399 | + update_option( '_wogh_' . $type . '_started', 'yes' ); | |
| 400 | + $user_id = get_current_user_id(); | |
| 401 | + wp_schedule_single_event( time(), 'wogh_' . $type . '', array( $user_id, $force ) ); | |
| 402 | + spawn_cron(); | |
| 403 | + } | |
| 404 | 404 | } | 
| @@ -12,12 +12,12 @@ discard block | ||
| 12 | 12 | |
| 13 | 13 | // If the functions have already been autoloaded, don't reload. | 
| 14 | 14 | // This fixes function duplication during unit testing. | 
| 15 | -$path = dirname( __FILE__ ) . '/vendor/autoload.php'; | |
| 16 | -if ( file_exists( $path ) ) { | |
| 17 | - require_once( $path ); | |
| 15 | +$path = dirname(__FILE__) . '/vendor/autoload.php'; | |
| 16 | +if (file_exists($path)) { | |
| 17 | + require_once($path); | |
| 18 | 18 | } | 
| 19 | 19 | |
| 20 | -add_action( 'plugins_loaded', array( new Writing_On_GitHub, 'boot' ) ); | |
| 20 | +add_action('plugins_loaded', array(new Writing_On_GitHub, 'boot')); | |
| 21 | 21 | |
| 22 | 22 |  class Writing_On_GitHub { | 
| 23 | 23 | |
| @@ -107,14 +107,14 @@ discard block | ||
| 107 | 107 |      public function __construct() { | 
| 108 | 108 | self::$instance = $this; | 
| 109 | 109 | |
| 110 | -        if ( is_admin() ) { | |
| 111 | - $this->admin = new Writing_On_GitHub_Admin( plugin_basename( __FILE__ ) ); | |
| 110 | +        if (is_admin()) { | |
| 111 | + $this->admin = new Writing_On_GitHub_Admin(plugin_basename(__FILE__)); | |
| 112 | 112 | } | 
| 113 | 113 | |
| 114 | - $this->controller = new Writing_On_GitHub_Controller( $this ); | |
| 114 | + $this->controller = new Writing_On_GitHub_Controller($this); | |
| 115 | 115 | |
| 116 | -        if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
| 117 | - WP_CLI::add_command( 'wogh', $this->cli() ); | |
| 116 | +        if (defined('WP_CLI') && WP_CLI) { | |
| 117 | +            WP_CLI::add_command('wogh', $this->cli()); | |
| 118 | 118 | } | 
| 119 | 119 | } | 
| 120 | 120 | |
| @@ -122,30 +122,30 @@ discard block | ||
| 122 | 122 | * Attaches the plugin's hooks into WordPress. | 
| 123 | 123 | */ | 
| 124 | 124 |      public function boot() { | 
| 125 | - register_activation_hook( __FILE__, array( $this, 'activate' ) ); | |
| 126 | - add_action( 'admin_notices', array( $this, 'activation_notice' ) ); | |
| 125 | + register_activation_hook(__FILE__, array($this, 'activate')); | |
| 126 | +        add_action('admin_notices', array($this, 'activation_notice')); | |
| 127 | 127 | |
| 128 | - add_action( 'init', array( $this, 'l10n' ) ); | |
| 128 | +        add_action('init', array($this, 'l10n')); | |
| 129 | 129 | |
| 130 | 130 | // Controller actions. | 
| 131 | - add_action( 'save_post', array( $this->controller, 'export_post' ) ); | |
| 132 | - add_action( 'delete_post', array( $this->controller, 'delete_post' ) ); | |
| 133 | - add_action( 'wp_ajax_nopriv_wogh_push_request', array( $this->controller, 'pull_posts' ) ); | |
| 134 | - add_action( 'wogh_export', array( $this->controller, 'export_all' ), 10, 2 ); | |
| 135 | - add_action( 'wogh_import', array( $this->controller, 'import_master' ), 10, 2 ); | |
| 136 | - add_filter( 'get_edit_post_link', array( $this, 'edit_post_link' ), 10, 3 ); | |
| 131 | +        add_action('save_post', array($this->controller, 'export_post')); | |
| 132 | +        add_action('delete_post', array($this->controller, 'delete_post')); | |
| 133 | +        add_action('wp_ajax_nopriv_wogh_push_request', array($this->controller, 'pull_posts')); | |
| 134 | +        add_action('wogh_export', array($this->controller, 'export_all'), 10, 2); | |
| 135 | +        add_action('wogh_import', array($this->controller, 'import_master'), 10, 2); | |
| 136 | +        add_filter('get_edit_post_link', array($this, 'edit_post_link'), 10, 3); | |
| 137 | 137 | |
| 138 | 138 | // add_filter( 'wogh_post_meta', array( $this, 'ignore_post_meta' ), 10, 1 ); | 
| 139 | 139 | // add_filter( 'wogh_pre_import_meta', array( $this, 'ignore_post_meta' ), 10, 1 ); | 
| 140 | - add_filter( 'the_content', array( $this, 'the_content' ) ); | |
| 140 | +        add_filter('the_content', array($this, 'the_content')); | |
| 141 | 141 | |
| 142 | - do_action( 'wogh_boot', $this ); | |
| 142 | +        do_action('wogh_boot', $this); | |
| 143 | 143 | } | 
| 144 | 144 | |
| 145 | 145 |      public function edit_post_link($link, $postID, $context) { | 
| 146 | -        if ( ! wp_is_post_revision( $postID ) ) { | |
| 147 | - $post = new Writing_On_GitHub_Post( $postID, Writing_On_GitHub::$instance->api() ); | |
| 148 | -            if ( $post->is_on_github() ) { | |
| 146 | +        if ( ! wp_is_post_revision($postID)) { | |
| 147 | + $post = new Writing_On_GitHub_Post($postID, Writing_On_GitHub::$instance->api()); | |
| 148 | +            if ($post->is_on_github()) { | |
| 149 | 149 | return $post->github_edit_url(); | 
| 150 | 150 | } | 
| 151 | 151 | } | 
| @@ -204,29 +204,29 @@ discard block | ||
| 204 | 204 | * Init i18n files | 
| 205 | 205 | */ | 
| 206 | 206 |      public function l10n() { | 
| 207 | - load_plugin_textdomain( self::$text_domain ); | |
| 207 | + load_plugin_textdomain(self::$text_domain); | |
| 208 | 208 | } | 
| 209 | 209 | |
| 210 | 210 | /** | 
| 211 | 211 | * Sets and kicks off the export cronjob | 
| 212 | 212 | */ | 
| 213 | -    public function start_export( $force = false ) { | |
| 214 | - $this->start_cron( 'export', $force ); | |
| 213 | +    public function start_export($force = false) { | |
| 214 | +        $this->start_cron('export', $force); | |
| 215 | 215 | } | 
| 216 | 216 | |
| 217 | 217 | /** | 
| 218 | 218 | * Sets and kicks off the import cronjob | 
| 219 | 219 | */ | 
| 220 | -    public function start_import( $force = false ) { | |
| 221 | - $this->start_cron( 'import', $force ); | |
| 220 | +    public function start_import($force = false) { | |
| 221 | +        $this->start_cron('import', $force); | |
| 222 | 222 | } | 
| 223 | 223 | |
| 224 | 224 | /** | 
| 225 | 225 | * Enables the admin notice on initial activation | 
| 226 | 226 | */ | 
| 227 | 227 |      public function activate() { | 
| 228 | -        if ( 'yes' !== get_option( '_wogh_fully_exported' ) ) { | |
| 229 | - set_transient( '_wogh_activated', 'yes' ); | |
| 228 | +        if ('yes' !== get_option('_wogh_fully_exported')) { | |
| 229 | +            set_transient('_wogh_activated', 'yes'); | |
| 230 | 230 | } | 
| 231 | 231 | } | 
| 232 | 232 | |
| @@ -234,18 +234,18 @@ discard block | ||
| 234 | 234 | * Displays the activation admin notice | 
| 235 | 235 | */ | 
| 236 | 236 |      public function activation_notice() { | 
| 237 | -        if ( ! get_transient( '_wogh_activated' ) ) { | |
| 237 | +        if ( ! get_transient('_wogh_activated')) { | |
| 238 | 238 | return; | 
| 239 | 239 | } | 
| 240 | 240 | |
| 241 | - delete_transient( '_wogh_activated' ); | |
| 241 | +        delete_transient('_wogh_activated'); | |
| 242 | 242 | |
| 243 | 243 | ?><div class="updated"> | 
| 244 | 244 | <p> | 
| 245 | 245 | <?php | 
| 246 | 246 | printf( | 
| 247 | - __( 'To set up your site to sync with GitHub, update your <a href="%s">settings</a> and click "Export to GitHub."', 'writing-on-github' ), | |
| 248 | - admin_url( 'options-general.php?page=' . static::$text_domain) | |
| 247 | +                        __('To set up your site to sync with GitHub, update your <a href="%s">settings</a> and click "Export to GitHub."', 'writing-on-github'), | |
| 248 | +                        admin_url('options-general.php?page=' . static::$text_domain) | |
| 249 | 249 | ); | 
| 250 | 250 | ?> | 
| 251 | 251 | </p> | 
| @@ -267,7 +267,7 @@ discard block | ||
| 267 | 267 | * @return Writing_On_GitHub_CLI | 
| 268 | 268 | */ | 
| 269 | 269 |      public function cli() { | 
| 270 | -        if ( ! $this->cli ) { | |
| 270 | +        if ( ! $this->cli) { | |
| 271 | 271 | $this->cli = new Writing_On_GitHub_CLI; | 
| 272 | 272 | } | 
| 273 | 273 | |
| @@ -280,8 +280,8 @@ discard block | ||
| 280 | 280 | * @return Writing_On_GitHub_Request | 
| 281 | 281 | */ | 
| 282 | 282 |      public function request() { | 
| 283 | -        if ( ! $this->request ) { | |
| 284 | - $this->request = new Writing_On_GitHub_Request( $this ); | |
| 283 | +        if ( ! $this->request) { | |
| 284 | + $this->request = new Writing_On_GitHub_Request($this); | |
| 285 | 285 | } | 
| 286 | 286 | |
| 287 | 287 | return $this->request; | 
| @@ -293,8 +293,8 @@ discard block | ||
| 293 | 293 | * @return Writing_On_GitHub_Response | 
| 294 | 294 | */ | 
| 295 | 295 |      public function response() { | 
| 296 | -        if ( ! $this->response ) { | |
| 297 | - $this->response = new Writing_On_GitHub_Response( $this ); | |
| 296 | +        if ( ! $this->response) { | |
| 297 | + $this->response = new Writing_On_GitHub_Response($this); | |
| 298 | 298 | } | 
| 299 | 299 | |
| 300 | 300 | return $this->response; | 
| @@ -306,8 +306,8 @@ discard block | ||
| 306 | 306 | * @return Writing_On_GitHub_Api | 
| 307 | 307 | */ | 
| 308 | 308 |      public function api() { | 
| 309 | -        if ( ! $this->api ) { | |
| 310 | - $this->api = new Writing_On_GitHub_Api( $this ); | |
| 309 | +        if ( ! $this->api) { | |
| 310 | + $this->api = new Writing_On_GitHub_Api($this); | |
| 311 | 311 | } | 
| 312 | 312 | |
| 313 | 313 | return $this->api; | 
| @@ -319,8 +319,8 @@ discard block | ||
| 319 | 319 | * @return Writing_On_GitHub_Import | 
| 320 | 320 | */ | 
| 321 | 321 |      public function import() { | 
| 322 | -        if ( ! $this->import ) { | |
| 323 | - $this->import = new Writing_On_GitHub_Import( $this ); | |
| 322 | +        if ( ! $this->import) { | |
| 323 | + $this->import = new Writing_On_GitHub_Import($this); | |
| 324 | 324 | } | 
| 325 | 325 | |
| 326 | 326 | return $this->import; | 
| @@ -332,8 +332,8 @@ discard block | ||
| 332 | 332 | * @return Writing_On_GitHub_Export | 
| 333 | 333 | */ | 
| 334 | 334 |      public function export() { | 
| 335 | -        if ( ! $this->export ) { | |
| 336 | - $this->export = new Writing_On_GitHub_Export( $this ); | |
| 335 | +        if ( ! $this->export) { | |
| 336 | + $this->export = new Writing_On_GitHub_Export($this); | |
| 337 | 337 | } | 
| 338 | 338 | |
| 339 | 339 | return $this->export; | 
| @@ -345,7 +345,7 @@ discard block | ||
| 345 | 345 | * @return Writing_On_GitHub_Semaphore | 
| 346 | 346 | */ | 
| 347 | 347 |      public function semaphore() { | 
| 348 | -        if ( ! $this->semaphore ) { | |
| 348 | +        if ( ! $this->semaphore) { | |
| 349 | 349 | $this->semaphore = new Writing_On_GitHub_Semaphore; | 
| 350 | 350 | } | 
| 351 | 351 | |
| @@ -358,8 +358,8 @@ discard block | ||
| 358 | 358 | * @return Writing_On_GitHub_Database | 
| 359 | 359 | */ | 
| 360 | 360 |      public function database() { | 
| 361 | -        if ( ! $this->database ) { | |
| 362 | - $this->database = new Writing_On_GitHub_Database( $this ); | |
| 361 | +        if ( ! $this->database) { | |
| 362 | + $this->database = new Writing_On_GitHub_Database($this); | |
| 363 | 363 | } | 
| 364 | 364 | |
| 365 | 365 | return $this->database; | 
| @@ -373,18 +373,18 @@ discard block | ||
| 373 | 373 | * @param mixed $msg | 
| 374 | 374 | * @param string $write | 
| 375 | 375 | */ | 
| 376 | -    public static function write_log( $msg, $write = 'line' ) { | |
| 377 | -        if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
| 378 | -            if ( is_array( $msg ) || is_object( $msg ) ) { | |
| 379 | - WP_CLI::print_value( $msg ); | |
| 376 | +    public static function write_log($msg, $write = 'line') { | |
| 377 | +        if (defined('WP_CLI') && WP_CLI) { | |
| 378 | +            if (is_array($msg) || is_object($msg)) { | |
| 379 | + WP_CLI::print_value($msg); | |
| 380 | 380 |              } else { | 
| 381 | - WP_CLI::$write( $msg ); | |
| 381 | + WP_CLI::$write($msg); | |
| 382 | 382 | } | 
| 383 | -        } elseif ( true === WP_DEBUG ) { | |
| 384 | -            if ( is_array( $msg ) || is_object( $msg ) ) { | |
| 385 | - error_log( print_r( $msg, true ) ); | |
| 383 | +        } elseif (true === WP_DEBUG) { | |
| 384 | +            if (is_array($msg) || is_object($msg)) { | |
| 385 | + error_log(print_r($msg, true)); | |
| 386 | 386 |              } else { | 
| 387 | - error_log( $msg ); | |
| 387 | + error_log($msg); | |
| 388 | 388 | } | 
| 389 | 389 | } | 
| 390 | 390 | } | 
| @@ -395,10 +395,10 @@ discard block | ||
| 395 | 395 | * @param bool $force | 
| 396 | 396 | * @param string $type | 
| 397 | 397 | */ | 
| 398 | -    protected function start_cron( $type, $force = false ) { | |
| 399 | - update_option( '_wogh_' . $type . '_started', 'yes' ); | |
| 398 | +    protected function start_cron($type, $force = false) { | |
| 399 | +        update_option('_wogh_' . $type . '_started', 'yes'); | |
| 400 | 400 | $user_id = get_current_user_id(); | 
| 401 | - wp_schedule_single_event( time(), 'wogh_' . $type . '', array( $user_id, $force ) ); | |
| 401 | + wp_schedule_single_event(time(), 'wogh_' . $type . '', array($user_id, $force)); | |
| 402 | 402 | spawn_cron(); | 
| 403 | 403 | } | 
| 404 | 404 | } | 
| @@ -9,264 +9,264 @@ | ||
| 9 | 9 | */ | 
| 10 | 10 |  class Writing_On_GitHub_Admin { | 
| 11 | 11 | |
| 12 | - /** | |
| 13 | - * plugin file name rel plugin dir. | |
| 14 | - * @var string | |
| 15 | - */ | |
| 16 | - protected $plugin_file; | |
| 17 | - | |
| 18 | - /** | |
| 19 | - * Hook into GitHub API | |
| 20 | - */ | |
| 21 | -    public function __construct( $plugin_file ) { | |
| 22 | - $this->plugin_file = $plugin_file; | |
| 23 | - | |
| 24 | - add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); | |
| 25 | - add_action( 'admin_init', array( $this, 'register_settings' ) ); | |
| 26 | - add_action( 'current_screen', array( $this, 'trigger_cron' ) ); | |
| 27 | - add_filter( 'plugin_action_links', array($this, 'settings_links'), 10, 2 ); | |
| 28 | - } | |
| 29 | - | |
| 30 | - /** | |
| 31 | - * settings link | |
| 32 | - * @param string[] $links | |
| 33 | - * @param string $file | |
| 34 | - * @return string[] | |
| 35 | - */ | |
| 36 | -    public function settings_links( $links, $file ) { | |
| 37 | -        if ( $file != $this->plugin_file ) { | |
| 38 | - return $links; | |
| 39 | - } | |
| 40 | - | |
| 41 | - $settings_link = '<a href="options-general.php?page=' . | |
| 42 | - Writing_On_GitHub::$text_domain . '">' . __( 'Settings', 'writing-on-github' ) . '</a>'; | |
| 43 | - | |
| 44 | - array_push( $links, $settings_link ); | |
| 45 | - | |
| 46 | - return $links; | |
| 47 | - } | |
| 48 | - | |
| 49 | - /** | |
| 50 | - * Callback to render the settings page view | |
| 51 | - */ | |
| 52 | -    public function settings_page() { | |
| 53 | - include dirname( dirname( __FILE__ ) ) . '/views/options.php'; | |
| 54 | - } | |
| 55 | - | |
| 56 | - /** | |
| 57 | - * Callback to register the plugin's options | |
| 58 | - */ | |
| 59 | -    public function register_settings() { | |
| 60 | - add_settings_section( | |
| 61 | - 'general', | |
| 62 | - 'General Settings', | |
| 63 | - array( $this, 'section_callback' ), | |
| 64 | - Writing_On_GitHub::$text_domain | |
| 65 | - ); | |
| 66 | - | |
| 67 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_host' ); | |
| 68 | - add_settings_field( 'wogh_host', __( 'GitHub hostname', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 69 | - 'default' => 'https://api.github.com', | |
| 70 | - 'name' => 'wogh_host', | |
| 71 | - 'help_text' => __( 'The GitHub host to use. This only needs to be changed to support a GitHub Enterprise installation.', 'writing-on-github' ), | |
| 72 | - ) | |
| 73 | - ); | |
| 74 | - | |
| 75 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_repository' ); | |
| 76 | - add_settings_field( 'wogh_repository', __( 'Repository', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 77 | - 'default' => '', | |
| 78 | - 'name' => 'wogh_repository', | |
| 79 | - 'help_text' => __( 'The GitHub repository to commit to, with owner (<code>[OWNER]/[REPOSITORY]</code>), e.g., <code>github/hubot.github.com</code>. The repository should contain an initial commit, which is satisfied by including a README when you create the repository on GitHub.', 'writing-on-github' ), | |
| 80 | - ) | |
| 81 | - ); | |
| 82 | - | |
| 83 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_branch' ); | |
| 84 | - add_settings_field( 'wogh_branch', __( 'Branch', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 85 | - 'default' => 'master', | |
| 86 | - 'name' => 'wogh_branch', | |
| 87 | - 'help_text' => __( 'The GitHub branch to commit to, default is master.', 'writing-on-github' ), | |
| 88 | - ) | |
| 89 | - ); | |
| 90 | - | |
| 91 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_oauth_token' ); | |
| 92 | - add_settings_field( 'wogh_oauth_token', __( 'Oauth Token', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 93 | - 'default' => '', | |
| 94 | - 'name' => 'wogh_oauth_token', | |
| 95 | - 'help_text' => __( "A <a href='https://github.com/settings/tokens/new'>personal oauth token</a> with <code>public_repo</code> scope.", 'writing-on-github' ), | |
| 96 | - ) | |
| 97 | - ); | |
| 98 | - | |
| 99 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_secret' ); | |
| 100 | - add_settings_field( 'wogh_secret', __( 'Webhook Secret', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 101 | - 'default' => '', | |
| 102 | - 'name' => 'wogh_secret', | |
| 103 | - 'help_text' => __( "The webhook's secret phrase. This should be password strength, as it is used to verify the webhook's payload.", 'writing-on-github' ), | |
| 104 | - ) | |
| 105 | - ); | |
| 106 | - | |
| 107 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_default_user' ); | |
| 108 | - add_settings_field( 'wogh_default_user', __( 'Default Import User', 'writing-on-github' ), array( &$this, 'user_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 109 | - 'default' => '', | |
| 110 | - 'name' => 'wogh_default_user', | |
| 111 | - 'help_text' => __( 'The fallback user for import, in case Writing On GitHub cannot find the committer in the database.', 'writing-on-github' ), | |
| 112 | - ) | |
| 113 | - ); | |
| 114 | - | |
| 115 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_ignore_author' ); | |
| 116 | - add_settings_field( 'wogh_ignore_author', __( 'Ignore author', 'writing-on-github' ), array( &$this, 'checkbox_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 117 | - 'default' => '', | |
| 118 | - 'name' => 'wogh_ignore_author', | |
| 119 | - 'help_text' => __( 'Do not export author and do not use author info from GitHub.', 'writing-on-github' ), | |
| 120 | - ) | |
| 121 | - ); | |
| 122 | - | |
| 123 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_dont_export_content' ); | |
| 124 | - add_settings_field( 'wogh_dont_export_content', __( 'Don\'t export content', 'writing-on-github' ), array( &$this, 'checkbox_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 125 | - 'default' => '', | |
| 126 | - 'name' => 'wogh_dont_export_content', | |
| 127 | - 'help_text' => __( 'Do not export post content to github, only export meta.', 'writing-on-github' ), | |
| 128 | - ) | |
| 129 | - ); | |
| 130 | - | |
| 131 | - // register_setting( Writing_On_GitHub::$text_domain, 'wogh_ignore_metas' ); | |
| 132 | - // add_settings_field( 'wogh_ignore_metas', __( 'Ignore post metas', 'writing-on-github' ), array( &$this, 'textarea_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 133 | - // 'default' => '', | |
| 134 | - // 'name' => 'wogh_ignore_metas', | |
| 135 | - // 'help_text' => __( 'These meta keys will be ignored and cannot be imported and exported. One meta key per line.', 'writing-on-github' ), | |
| 136 | - // ) | |
| 137 | - // ); | |
| 138 | - } | |
| 139 | - | |
| 140 | - /** | |
| 141 | - * Callback to render an individual options field | |
| 142 | - * | |
| 143 | - * @param array $args Field arguments. | |
| 144 | - */ | |
| 145 | -    public function field_callback( $args ) { | |
| 146 | - include dirname( dirname( __FILE__ ) ) . '/views/setting-field.php'; | |
| 147 | - } | |
| 148 | - | |
| 149 | - /** | |
| 150 | - * Callback to render the default import user field. | |
| 151 | - * | |
| 152 | - * @param array $args Field arguments. | |
| 153 | - */ | |
| 154 | -    public function user_field_callback( $args ) { | |
| 155 | - include dirname( dirname( __FILE__ ) ) . '/views/user-setting-field.php'; | |
| 156 | - } | |
| 157 | - | |
| 158 | - /** | |
| 159 | - * Callback to render the textarea field. | |
| 160 | - * | |
| 161 | - * @param array $args Field arguments. | |
| 162 | - */ | |
| 163 | -    public function textarea_field_callback( $args ) { | |
| 164 | - include dirname( dirname( __FILE__ ) ) . '/views/textarea-setting-field.php'; | |
| 165 | - } | |
| 166 | - | |
| 167 | - /** | |
| 168 | - * Callback to render the checkbox field. | |
| 169 | - * | |
| 170 | - * @param array $args Field arguments. | |
| 171 | - */ | |
| 172 | -    public function checkbox_field_callback( $args ) { | |
| 173 | - include dirname( dirname( __FILE__ ) ) . '/views/checkbox-setting-field.php'; | |
| 174 | - } | |
| 175 | - | |
| 176 | - /** | |
| 177 | - * Displays settings messages from background processes | |
| 178 | - */ | |
| 179 | -    public function section_callback() { | |
| 180 | -        if ( get_current_screen()->id !== 'settings_page_' . Writing_On_GitHub::$text_domain ) { | |
| 181 | - return; | |
| 182 | - } | |
| 183 | - | |
| 184 | -        if ( 'yes' === get_option( '_wogh_export_started' ) ) { ?> | |
| 12 | + /** | |
| 13 | + * plugin file name rel plugin dir. | |
| 14 | + * @var string | |
| 15 | + */ | |
| 16 | + protected $plugin_file; | |
| 17 | + | |
| 18 | + /** | |
| 19 | + * Hook into GitHub API | |
| 20 | + */ | |
| 21 | +	public function __construct( $plugin_file ) { | |
| 22 | + $this->plugin_file = $plugin_file; | |
| 23 | + | |
| 24 | + add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); | |
| 25 | + add_action( 'admin_init', array( $this, 'register_settings' ) ); | |
| 26 | + add_action( 'current_screen', array( $this, 'trigger_cron' ) ); | |
| 27 | + add_filter( 'plugin_action_links', array($this, 'settings_links'), 10, 2 ); | |
| 28 | + } | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * settings link | |
| 32 | + * @param string[] $links | |
| 33 | + * @param string $file | |
| 34 | + * @return string[] | |
| 35 | + */ | |
| 36 | +	public function settings_links( $links, $file ) { | |
| 37 | +		if ( $file != $this->plugin_file ) { | |
| 38 | + return $links; | |
| 39 | + } | |
| 40 | + | |
| 41 | + $settings_link = '<a href="options-general.php?page=' . | |
| 42 | + Writing_On_GitHub::$text_domain . '">' . __( 'Settings', 'writing-on-github' ) . '</a>'; | |
| 43 | + | |
| 44 | + array_push( $links, $settings_link ); | |
| 45 | + | |
| 46 | + return $links; | |
| 47 | + } | |
| 48 | + | |
| 49 | + /** | |
| 50 | + * Callback to render the settings page view | |
| 51 | + */ | |
| 52 | +	public function settings_page() { | |
| 53 | + include dirname( dirname( __FILE__ ) ) . '/views/options.php'; | |
| 54 | + } | |
| 55 | + | |
| 56 | + /** | |
| 57 | + * Callback to register the plugin's options | |
| 58 | + */ | |
| 59 | +	public function register_settings() { | |
| 60 | + add_settings_section( | |
| 61 | + 'general', | |
| 62 | + 'General Settings', | |
| 63 | + array( $this, 'section_callback' ), | |
| 64 | + Writing_On_GitHub::$text_domain | |
| 65 | + ); | |
| 66 | + | |
| 67 | + register_setting( Writing_On_GitHub::$text_domain, 'wogh_host' ); | |
| 68 | + add_settings_field( 'wogh_host', __( 'GitHub hostname', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 69 | + 'default' => 'https://api.github.com', | |
| 70 | + 'name' => 'wogh_host', | |
| 71 | + 'help_text' => __( 'The GitHub host to use. This only needs to be changed to support a GitHub Enterprise installation.', 'writing-on-github' ), | |
| 72 | + ) | |
| 73 | + ); | |
| 74 | + | |
| 75 | + register_setting( Writing_On_GitHub::$text_domain, 'wogh_repository' ); | |
| 76 | + add_settings_field( 'wogh_repository', __( 'Repository', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 77 | + 'default' => '', | |
| 78 | + 'name' => 'wogh_repository', | |
| 79 | + 'help_text' => __( 'The GitHub repository to commit to, with owner (<code>[OWNER]/[REPOSITORY]</code>), e.g., <code>github/hubot.github.com</code>. The repository should contain an initial commit, which is satisfied by including a README when you create the repository on GitHub.', 'writing-on-github' ), | |
| 80 | + ) | |
| 81 | + ); | |
| 82 | + | |
| 83 | + register_setting( Writing_On_GitHub::$text_domain, 'wogh_branch' ); | |
| 84 | + add_settings_field( 'wogh_branch', __( 'Branch', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 85 | + 'default' => 'master', | |
| 86 | + 'name' => 'wogh_branch', | |
| 87 | + 'help_text' => __( 'The GitHub branch to commit to, default is master.', 'writing-on-github' ), | |
| 88 | + ) | |
| 89 | + ); | |
| 90 | + | |
| 91 | + register_setting( Writing_On_GitHub::$text_domain, 'wogh_oauth_token' ); | |
| 92 | + add_settings_field( 'wogh_oauth_token', __( 'Oauth Token', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 93 | + 'default' => '', | |
| 94 | + 'name' => 'wogh_oauth_token', | |
| 95 | + 'help_text' => __( "A <a href='https://github.com/settings/tokens/new'>personal oauth token</a> with <code>public_repo</code> scope.", 'writing-on-github' ), | |
| 96 | + ) | |
| 97 | + ); | |
| 98 | + | |
| 99 | + register_setting( Writing_On_GitHub::$text_domain, 'wogh_secret' ); | |
| 100 | + add_settings_field( 'wogh_secret', __( 'Webhook Secret', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 101 | + 'default' => '', | |
| 102 | + 'name' => 'wogh_secret', | |
| 103 | + 'help_text' => __( "The webhook's secret phrase. This should be password strength, as it is used to verify the webhook's payload.", 'writing-on-github' ), | |
| 104 | + ) | |
| 105 | + ); | |
| 106 | + | |
| 107 | + register_setting( Writing_On_GitHub::$text_domain, 'wogh_default_user' ); | |
| 108 | + add_settings_field( 'wogh_default_user', __( 'Default Import User', 'writing-on-github' ), array( &$this, 'user_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 109 | + 'default' => '', | |
| 110 | + 'name' => 'wogh_default_user', | |
| 111 | + 'help_text' => __( 'The fallback user for import, in case Writing On GitHub cannot find the committer in the database.', 'writing-on-github' ), | |
| 112 | + ) | |
| 113 | + ); | |
| 114 | + | |
| 115 | + register_setting( Writing_On_GitHub::$text_domain, 'wogh_ignore_author' ); | |
| 116 | + add_settings_field( 'wogh_ignore_author', __( 'Ignore author', 'writing-on-github' ), array( &$this, 'checkbox_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 117 | + 'default' => '', | |
| 118 | + 'name' => 'wogh_ignore_author', | |
| 119 | + 'help_text' => __( 'Do not export author and do not use author info from GitHub.', 'writing-on-github' ), | |
| 120 | + ) | |
| 121 | + ); | |
| 122 | + | |
| 123 | + register_setting( Writing_On_GitHub::$text_domain, 'wogh_dont_export_content' ); | |
| 124 | + add_settings_field( 'wogh_dont_export_content', __( 'Don\'t export content', 'writing-on-github' ), array( &$this, 'checkbox_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 125 | + 'default' => '', | |
| 126 | + 'name' => 'wogh_dont_export_content', | |
| 127 | + 'help_text' => __( 'Do not export post content to github, only export meta.', 'writing-on-github' ), | |
| 128 | + ) | |
| 129 | + ); | |
| 130 | + | |
| 131 | + // register_setting( Writing_On_GitHub::$text_domain, 'wogh_ignore_metas' ); | |
| 132 | + // add_settings_field( 'wogh_ignore_metas', __( 'Ignore post metas', 'writing-on-github' ), array( &$this, 'textarea_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 133 | + // 'default' => '', | |
| 134 | + // 'name' => 'wogh_ignore_metas', | |
| 135 | + // 'help_text' => __( 'These meta keys will be ignored and cannot be imported and exported. One meta key per line.', 'writing-on-github' ), | |
| 136 | + // ) | |
| 137 | + // ); | |
| 138 | + } | |
| 139 | + | |
| 140 | + /** | |
| 141 | + * Callback to render an individual options field | |
| 142 | + * | |
| 143 | + * @param array $args Field arguments. | |
| 144 | + */ | |
| 145 | +	public function field_callback( $args ) { | |
| 146 | + include dirname( dirname( __FILE__ ) ) . '/views/setting-field.php'; | |
| 147 | + } | |
| 148 | + | |
| 149 | + /** | |
| 150 | + * Callback to render the default import user field. | |
| 151 | + * | |
| 152 | + * @param array $args Field arguments. | |
| 153 | + */ | |
| 154 | +	public function user_field_callback( $args ) { | |
| 155 | + include dirname( dirname( __FILE__ ) ) . '/views/user-setting-field.php'; | |
| 156 | + } | |
| 157 | + | |
| 158 | + /** | |
| 159 | + * Callback to render the textarea field. | |
| 160 | + * | |
| 161 | + * @param array $args Field arguments. | |
| 162 | + */ | |
| 163 | +	public function textarea_field_callback( $args ) { | |
| 164 | + include dirname( dirname( __FILE__ ) ) . '/views/textarea-setting-field.php'; | |
| 165 | + } | |
| 166 | + | |
| 167 | + /** | |
| 168 | + * Callback to render the checkbox field. | |
| 169 | + * | |
| 170 | + * @param array $args Field arguments. | |
| 171 | + */ | |
| 172 | +	public function checkbox_field_callback( $args ) { | |
| 173 | + include dirname( dirname( __FILE__ ) ) . '/views/checkbox-setting-field.php'; | |
| 174 | + } | |
| 175 | + | |
| 176 | + /** | |
| 177 | + * Displays settings messages from background processes | |
| 178 | + */ | |
| 179 | +	public function section_callback() { | |
| 180 | +		if ( get_current_screen()->id !== 'settings_page_' . Writing_On_GitHub::$text_domain ) { | |
| 181 | + return; | |
| 182 | + } | |
| 183 | + | |
| 184 | +		if ( 'yes' === get_option( '_wogh_export_started' ) ) { ?> | |
| 185 | 185 | <div class="updated"> | 
| 186 | 186 | <p><?php esc_html_e( 'Export to GitHub started.', 'writing-on-github' ); ?></p> | 
| 187 | 187 | </div><?php | 
| 188 | - delete_option( '_wogh_export_started' ); | |
| 189 | - } | |
| 188 | + delete_option( '_wogh_export_started' ); | |
| 189 | + } | |
| 190 | 190 | |
| 191 | -        if ( $message = get_option( '_wogh_export_error' ) ) { ?> | |
| 191 | +		if ( $message = get_option( '_wogh_export_error' ) ) { ?> | |
| 192 | 192 | <div class="error"> | 
| 193 | 193 | <p><?php esc_html_e( 'Export to GitHub failed with error:', 'writing-on-github' ); ?> <?php echo esc_html( $message );?></p> | 
| 194 | 194 | </div><?php | 
| 195 | - delete_option( '_wogh_export_error' ); | |
| 196 | - } | |
| 195 | + delete_option( '_wogh_export_error' ); | |
| 196 | + } | |
| 197 | 197 | |
| 198 | -        if ( 'yes' === get_option( '_wogh_export_complete' ) ) { ?> | |
| 198 | +		if ( 'yes' === get_option( '_wogh_export_complete' ) ) { ?> | |
| 199 | 199 | <div class="updated"> | 
| 200 | 200 | <p><?php esc_html_e( 'Export to GitHub completed successfully.', 'writing-on-github' );?></p> | 
| 201 | 201 | </div><?php | 
| 202 | - delete_option( '_wogh_export_complete' ); | |
| 203 | - } | |
| 202 | + delete_option( '_wogh_export_complete' ); | |
| 203 | + } | |
| 204 | 204 | |
| 205 | -        if ( 'yes' === get_option( '_wogh_import_started' ) ) { ?> | |
| 205 | +		if ( 'yes' === get_option( '_wogh_import_started' ) ) { ?> | |
| 206 | 206 | <div class="updated"> | 
| 207 | 207 | <p><?php esc_html_e( 'Import from GitHub started.', 'writing-on-github' ); ?></p> | 
| 208 | 208 | </div><?php | 
| 209 | - delete_option( '_wogh_import_started' ); | |
| 210 | - } | |
| 209 | + delete_option( '_wogh_import_started' ); | |
| 210 | + } | |
| 211 | 211 | |
| 212 | -        if ( $message = get_option( '_wogh_import_error' ) ) { ?> | |
| 212 | +		if ( $message = get_option( '_wogh_import_error' ) ) { ?> | |
| 213 | 213 | <div class="error"> | 
| 214 | 214 | <p><?php esc_html_e( 'Import from GitHub failed with error:', 'writing-on-github' ); ?> <?php echo esc_html( $message );?></p> | 
| 215 | 215 | </div><?php | 
| 216 | - delete_option( '_wogh_import_error' ); | |
| 217 | - } | |
| 216 | + delete_option( '_wogh_import_error' ); | |
| 217 | + } | |
| 218 | 218 | |
| 219 | -        if ( 'yes' === get_option( '_wogh_import_complete' ) ) { ?> | |
| 219 | +		if ( 'yes' === get_option( '_wogh_import_complete' ) ) { ?> | |
| 220 | 220 | <div class="updated"> | 
| 221 | 221 | <p><?php esc_html_e( 'Import from GitHub completed successfully.', 'writing-on-github' );?></p> | 
| 222 | 222 | </div><?php | 
| 223 | - delete_option( '_wogh_import_complete' ); | |
| 224 | - } | |
| 225 | - } | |
| 226 | - | |
| 227 | - /** | |
| 228 | - * Add options menu to admin navbar | |
| 229 | - */ | |
| 230 | -    public function add_admin_menu() { | |
| 231 | - add_options_page( | |
| 232 | - __( 'Writing On GitHub', 'writing-on-github' ), | |
| 233 | - __( 'Writing On GitHub', 'writing-on-github' ), | |
| 234 | - 'manage_options', | |
| 235 | - Writing_On_GitHub::$text_domain, | |
| 236 | - array( $this, 'settings_page' ) | |
| 237 | - ); | |
| 238 | - } | |
| 239 | - | |
| 240 | - /** | |
| 241 | - * Admin callback to trigger import/export because WordPress admin routing lol | |
| 242 | - */ | |
| 243 | -    public function trigger_cron() { | |
| 244 | -        if ( ! current_user_can( 'manage_options' ) ) { | |
| 245 | - return; | |
| 246 | - } | |
| 247 | - | |
| 248 | -        if ( get_current_screen()->id !== 'settings_page_' . Writing_On_GitHub::$text_domain ) { | |
| 249 | - return; | |
| 250 | - } | |
| 251 | - | |
| 252 | -        if ( ! isset( $_GET['action'] ) ) { | |
| 253 | - return; | |
| 254 | - } | |
| 255 | - | |
| 256 | -        if ( 'export' === $_GET['action'] ) { | |
| 257 | - Writing_On_GitHub::$instance->start_export(); | |
| 258 | - } | |
| 259 | -        if ( 'force_export' === $_GET['action'] ) { | |
| 260 | - Writing_On_GitHub::$instance->start_export(true); | |
| 261 | - } | |
| 262 | -        if ( 'import' === $_GET['action'] ) { | |
| 263 | - Writing_On_GitHub::$instance->start_import(); | |
| 264 | - } | |
| 265 | -        if ( 'force_import' === $_GET['action'] ) { | |
| 266 | - Writing_On_GitHub::$instance->start_import(true); | |
| 267 | - } | |
| 268 | - | |
| 269 | - wp_redirect( admin_url( 'options-general.php?page=writing-on-github' ) ); | |
| 270 | - die; | |
| 271 | - } | |
| 223 | + delete_option( '_wogh_import_complete' ); | |
| 224 | + } | |
| 225 | + } | |
| 226 | + | |
| 227 | + /** | |
| 228 | + * Add options menu to admin navbar | |
| 229 | + */ | |
| 230 | +	public function add_admin_menu() { | |
| 231 | + add_options_page( | |
| 232 | + __( 'Writing On GitHub', 'writing-on-github' ), | |
| 233 | + __( 'Writing On GitHub', 'writing-on-github' ), | |
| 234 | + 'manage_options', | |
| 235 | + Writing_On_GitHub::$text_domain, | |
| 236 | + array( $this, 'settings_page' ) | |
| 237 | + ); | |
| 238 | + } | |
| 239 | + | |
| 240 | + /** | |
| 241 | + * Admin callback to trigger import/export because WordPress admin routing lol | |
| 242 | + */ | |
| 243 | +	public function trigger_cron() { | |
| 244 | +		if ( ! current_user_can( 'manage_options' ) ) { | |
| 245 | + return; | |
| 246 | + } | |
| 247 | + | |
| 248 | +		if ( get_current_screen()->id !== 'settings_page_' . Writing_On_GitHub::$text_domain ) { | |
| 249 | + return; | |
| 250 | + } | |
| 251 | + | |
| 252 | +		if ( ! isset( $_GET['action'] ) ) { | |
| 253 | + return; | |
| 254 | + } | |
| 255 | + | |
| 256 | +		if ( 'export' === $_GET['action'] ) { | |
| 257 | + Writing_On_GitHub::$instance->start_export(); | |
| 258 | + } | |
| 259 | +		if ( 'force_export' === $_GET['action'] ) { | |
| 260 | + Writing_On_GitHub::$instance->start_export(true); | |
| 261 | + } | |
| 262 | +		if ( 'import' === $_GET['action'] ) { | |
| 263 | + Writing_On_GitHub::$instance->start_import(); | |
| 264 | + } | |
| 265 | +		if ( 'force_import' === $_GET['action'] ) { | |
| 266 | + Writing_On_GitHub::$instance->start_import(true); | |
| 267 | + } | |
| 268 | + | |
| 269 | + wp_redirect( admin_url( 'options-general.php?page=writing-on-github' ) ); | |
| 270 | + die; | |
| 271 | + } | |
| 272 | 272 | } | 
| @@ -18,13 +18,13 @@ discard block | ||
| 18 | 18 | /** | 
| 19 | 19 | * Hook into GitHub API | 
| 20 | 20 | */ | 
| 21 | -    public function __construct( $plugin_file ) { | |
| 21 | +    public function __construct($plugin_file) { | |
| 22 | 22 | $this->plugin_file = $plugin_file; | 
| 23 | 23 | |
| 24 | - add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); | |
| 25 | - add_action( 'admin_init', array( $this, 'register_settings' ) ); | |
| 26 | - add_action( 'current_screen', array( $this, 'trigger_cron' ) ); | |
| 27 | - add_filter( 'plugin_action_links', array($this, 'settings_links'), 10, 2 ); | |
| 24 | +        add_action('admin_menu', array($this, 'add_admin_menu')); | |
| 25 | +        add_action('admin_init', array($this, 'register_settings')); | |
| 26 | +        add_action('current_screen', array($this, 'trigger_cron')); | |
| 27 | +        add_filter('plugin_action_links', array($this, 'settings_links'), 10, 2); | |
| 28 | 28 | } | 
| 29 | 29 | |
| 30 | 30 | /** | 
| @@ -33,15 +33,15 @@ discard block | ||
| 33 | 33 | * @param string $file | 
| 34 | 34 | * @return string[] | 
| 35 | 35 | */ | 
| 36 | -    public function settings_links( $links, $file ) { | |
| 37 | -        if ( $file != $this->plugin_file ) { | |
| 36 | +    public function settings_links($links, $file) { | |
| 37 | +        if ($file != $this->plugin_file) { | |
| 38 | 38 | return $links; | 
| 39 | 39 | } | 
| 40 | 40 | |
| 41 | 41 | $settings_link = '<a href="options-general.php?page=' . | 
| 42 | - Writing_On_GitHub::$text_domain . '">' . __( 'Settings', 'writing-on-github' ) . '</a>'; | |
| 42 | +        Writing_On_GitHub::$text_domain . '">' . __('Settings', 'writing-on-github') . '</a>'; | |
| 43 | 43 | |
| 44 | - array_push( $links, $settings_link ); | |
| 44 | + array_push($links, $settings_link); | |
| 45 | 45 | |
| 46 | 46 | return $links; | 
| 47 | 47 | } | 
| @@ -50,7 +50,7 @@ discard block | ||
| 50 | 50 | * Callback to render the settings page view | 
| 51 | 51 | */ | 
| 52 | 52 |      public function settings_page() { | 
| 53 | - include dirname( dirname( __FILE__ ) ) . '/views/options.php'; | |
| 53 | + include dirname(dirname(__FILE__)) . '/views/options.php'; | |
| 54 | 54 | } | 
| 55 | 55 | |
| 56 | 56 | /** | 
| @@ -60,71 +60,71 @@ discard block | ||
| 60 | 60 | add_settings_section( | 
| 61 | 61 | 'general', | 
| 62 | 62 | 'General Settings', | 
| 63 | - array( $this, 'section_callback' ), | |
| 63 | + array($this, 'section_callback'), | |
| 64 | 64 | Writing_On_GitHub::$text_domain | 
| 65 | 65 | ); | 
| 66 | 66 | |
| 67 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_host' ); | |
| 68 | - add_settings_field( 'wogh_host', __( 'GitHub hostname', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 67 | + register_setting(Writing_On_GitHub::$text_domain, 'wogh_host'); | |
| 68 | +        add_settings_field('wogh_host', __('GitHub hostname', 'writing-on-github'), array($this, 'field_callback'), Writing_On_GitHub::$text_domain, 'general', array( | |
| 69 | 69 | 'default' => 'https://api.github.com', | 
| 70 | 70 | 'name' => 'wogh_host', | 
| 71 | - 'help_text' => __( 'The GitHub host to use. This only needs to be changed to support a GitHub Enterprise installation.', 'writing-on-github' ), | |
| 71 | +                'help_text' => __('The GitHub host to use. This only needs to be changed to support a GitHub Enterprise installation.', 'writing-on-github'), | |
| 72 | 72 | ) | 
| 73 | 73 | ); | 
| 74 | 74 | |
| 75 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_repository' ); | |
| 76 | - add_settings_field( 'wogh_repository', __( 'Repository', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 75 | + register_setting(Writing_On_GitHub::$text_domain, 'wogh_repository'); | |
| 76 | +        add_settings_field('wogh_repository', __('Repository', 'writing-on-github'), array($this, 'field_callback'), Writing_On_GitHub::$text_domain, 'general', array( | |
| 77 | 77 | 'default' => '', | 
| 78 | 78 | 'name' => 'wogh_repository', | 
| 79 | - 'help_text' => __( 'The GitHub repository to commit to, with owner (<code>[OWNER]/[REPOSITORY]</code>), e.g., <code>github/hubot.github.com</code>. The repository should contain an initial commit, which is satisfied by including a README when you create the repository on GitHub.', 'writing-on-github' ), | |
| 79 | +                'help_text' => __('The GitHub repository to commit to, with owner (<code>[OWNER]/[REPOSITORY]</code>), e.g., <code>github/hubot.github.com</code>. The repository should contain an initial commit, which is satisfied by including a README when you create the repository on GitHub.', 'writing-on-github'), | |
| 80 | 80 | ) | 
| 81 | 81 | ); | 
| 82 | 82 | |
| 83 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_branch' ); | |
| 84 | - add_settings_field( 'wogh_branch', __( 'Branch', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 83 | + register_setting(Writing_On_GitHub::$text_domain, 'wogh_branch'); | |
| 84 | +        add_settings_field('wogh_branch', __('Branch', 'writing-on-github'), array($this, 'field_callback'), Writing_On_GitHub::$text_domain, 'general', array( | |
| 85 | 85 | 'default' => 'master', | 
| 86 | 86 | 'name' => 'wogh_branch', | 
| 87 | - 'help_text' => __( 'The GitHub branch to commit to, default is master.', 'writing-on-github' ), | |
| 87 | +                'help_text' => __('The GitHub branch to commit to, default is master.', 'writing-on-github'), | |
| 88 | 88 | ) | 
| 89 | 89 | ); | 
| 90 | 90 | |
| 91 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_oauth_token' ); | |
| 92 | - add_settings_field( 'wogh_oauth_token', __( 'Oauth Token', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 91 | + register_setting(Writing_On_GitHub::$text_domain, 'wogh_oauth_token'); | |
| 92 | +        add_settings_field('wogh_oauth_token', __('Oauth Token', 'writing-on-github'), array($this, 'field_callback'), Writing_On_GitHub::$text_domain, 'general', array( | |
| 93 | 93 | 'default' => '', | 
| 94 | 94 | 'name' => 'wogh_oauth_token', | 
| 95 | - 'help_text' => __( "A <a href='https://github.com/settings/tokens/new'>personal oauth token</a> with <code>public_repo</code> scope.", 'writing-on-github' ), | |
| 95 | +                'help_text' => __("A <a href='https://github.com/settings/tokens/new'>personal oauth token</a> with <code>public_repo</code> scope.", 'writing-on-github'), | |
| 96 | 96 | ) | 
| 97 | 97 | ); | 
| 98 | 98 | |
| 99 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_secret' ); | |
| 100 | - add_settings_field( 'wogh_secret', __( 'Webhook Secret', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 99 | + register_setting(Writing_On_GitHub::$text_domain, 'wogh_secret'); | |
| 100 | +        add_settings_field('wogh_secret', __('Webhook Secret', 'writing-on-github'), array($this, 'field_callback'), Writing_On_GitHub::$text_domain, 'general', array( | |
| 101 | 101 | 'default' => '', | 
| 102 | 102 | 'name' => 'wogh_secret', | 
| 103 | - 'help_text' => __( "The webhook's secret phrase. This should be password strength, as it is used to verify the webhook's payload.", 'writing-on-github' ), | |
| 103 | +                'help_text' => __("The webhook's secret phrase. This should be password strength, as it is used to verify the webhook's payload.", 'writing-on-github'), | |
| 104 | 104 | ) | 
| 105 | 105 | ); | 
| 106 | 106 | |
| 107 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_default_user' ); | |
| 108 | - add_settings_field( 'wogh_default_user', __( 'Default Import User', 'writing-on-github' ), array( &$this, 'user_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 107 | + register_setting(Writing_On_GitHub::$text_domain, 'wogh_default_user'); | |
| 108 | +        add_settings_field('wogh_default_user', __('Default Import User', 'writing-on-github'), array(&$this, 'user_field_callback'), Writing_On_GitHub::$text_domain, 'general', array( | |
| 109 | 109 | 'default' => '', | 
| 110 | 110 | 'name' => 'wogh_default_user', | 
| 111 | - 'help_text' => __( 'The fallback user for import, in case Writing On GitHub cannot find the committer in the database.', 'writing-on-github' ), | |
| 111 | +                'help_text' => __('The fallback user for import, in case Writing On GitHub cannot find the committer in the database.', 'writing-on-github'), | |
| 112 | 112 | ) | 
| 113 | 113 | ); | 
| 114 | 114 | |
| 115 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_ignore_author' ); | |
| 116 | - add_settings_field( 'wogh_ignore_author', __( 'Ignore author', 'writing-on-github' ), array( &$this, 'checkbox_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 115 | + register_setting(Writing_On_GitHub::$text_domain, 'wogh_ignore_author'); | |
| 116 | +        add_settings_field('wogh_ignore_author', __('Ignore author', 'writing-on-github'), array(&$this, 'checkbox_field_callback'), Writing_On_GitHub::$text_domain, 'general', array( | |
| 117 | 117 | 'default' => '', | 
| 118 | 118 | 'name' => 'wogh_ignore_author', | 
| 119 | - 'help_text' => __( 'Do not export author and do not use author info from GitHub.', 'writing-on-github' ), | |
| 119 | +                'help_text' => __('Do not export author and do not use author info from GitHub.', 'writing-on-github'), | |
| 120 | 120 | ) | 
| 121 | 121 | ); | 
| 122 | 122 | |
| 123 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_dont_export_content' ); | |
| 124 | - add_settings_field( 'wogh_dont_export_content', __( 'Don\'t export content', 'writing-on-github' ), array( &$this, 'checkbox_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( | |
| 123 | + register_setting(Writing_On_GitHub::$text_domain, 'wogh_dont_export_content'); | |
| 124 | +        add_settings_field('wogh_dont_export_content', __('Don\'t export content', 'writing-on-github'), array(&$this, 'checkbox_field_callback'), Writing_On_GitHub::$text_domain, 'general', array( | |
| 125 | 125 | 'default' => '', | 
| 126 | 126 | 'name' => 'wogh_dont_export_content', | 
| 127 | - 'help_text' => __( 'Do not export post content to github, only export meta.', 'writing-on-github' ), | |
| 127 | +                'help_text' => __('Do not export post content to github, only export meta.', 'writing-on-github'), | |
| 128 | 128 | ) | 
| 129 | 129 | ); | 
| 130 | 130 | |
| @@ -142,8 +142,8 @@ discard block | ||
| 142 | 142 | * | 
| 143 | 143 | * @param array $args Field arguments. | 
| 144 | 144 | */ | 
| 145 | -    public function field_callback( $args ) { | |
| 146 | - include dirname( dirname( __FILE__ ) ) . '/views/setting-field.php'; | |
| 145 | +    public function field_callback($args) { | |
| 146 | + include dirname(dirname(__FILE__)) . '/views/setting-field.php'; | |
| 147 | 147 | } | 
| 148 | 148 | |
| 149 | 149 | /** | 
| @@ -151,8 +151,8 @@ discard block | ||
| 151 | 151 | * | 
| 152 | 152 | * @param array $args Field arguments. | 
| 153 | 153 | */ | 
| 154 | -    public function user_field_callback( $args ) { | |
| 155 | - include dirname( dirname( __FILE__ ) ) . '/views/user-setting-field.php'; | |
| 154 | +    public function user_field_callback($args) { | |
| 155 | + include dirname(dirname(__FILE__)) . '/views/user-setting-field.php'; | |
| 156 | 156 | } | 
| 157 | 157 | |
| 158 | 158 | /** | 
| @@ -160,8 +160,8 @@ discard block | ||
| 160 | 160 | * | 
| 161 | 161 | * @param array $args Field arguments. | 
| 162 | 162 | */ | 
| 163 | -    public function textarea_field_callback( $args ) { | |
| 164 | - include dirname( dirname( __FILE__ ) ) . '/views/textarea-setting-field.php'; | |
| 163 | +    public function textarea_field_callback($args) { | |
| 164 | + include dirname(dirname(__FILE__)) . '/views/textarea-setting-field.php'; | |
| 165 | 165 | } | 
| 166 | 166 | |
| 167 | 167 | /** | 
| @@ -169,58 +169,58 @@ discard block | ||
| 169 | 169 | * | 
| 170 | 170 | * @param array $args Field arguments. | 
| 171 | 171 | */ | 
| 172 | -    public function checkbox_field_callback( $args ) { | |
| 173 | - include dirname( dirname( __FILE__ ) ) . '/views/checkbox-setting-field.php'; | |
| 172 | +    public function checkbox_field_callback($args) { | |
| 173 | + include dirname(dirname(__FILE__)) . '/views/checkbox-setting-field.php'; | |
| 174 | 174 | } | 
| 175 | 175 | |
| 176 | 176 | /** | 
| 177 | 177 | * Displays settings messages from background processes | 
| 178 | 178 | */ | 
| 179 | 179 |      public function section_callback() { | 
| 180 | -        if ( get_current_screen()->id !== 'settings_page_' . Writing_On_GitHub::$text_domain ) { | |
| 180 | +        if (get_current_screen()->id !== 'settings_page_' . Writing_On_GitHub::$text_domain) { | |
| 181 | 181 | return; | 
| 182 | 182 | } | 
| 183 | 183 | |
| 184 | -        if ( 'yes' === get_option( '_wogh_export_started' ) ) { ?> | |
| 184 | +        if ('yes' === get_option('_wogh_export_started')) { ?> | |
| 185 | 185 | <div class="updated"> | 
| 186 | - <p><?php esc_html_e( 'Export to GitHub started.', 'writing-on-github' ); ?></p> | |
| 186 | +                <p><?php esc_html_e('Export to GitHub started.', 'writing-on-github'); ?></p> | |
| 187 | 187 | </div><?php | 
| 188 | - delete_option( '_wogh_export_started' ); | |
| 188 | +            delete_option('_wogh_export_started'); | |
| 189 | 189 | } | 
| 190 | 190 | |
| 191 | -        if ( $message = get_option( '_wogh_export_error' ) ) { ?> | |
| 191 | +        if ($message = get_option('_wogh_export_error')) { ?> | |
| 192 | 192 | <div class="error"> | 
| 193 | - <p><?php esc_html_e( 'Export to GitHub failed with error:', 'writing-on-github' ); ?> <?php echo esc_html( $message );?></p> | |
| 193 | +                <p><?php esc_html_e('Export to GitHub failed with error:', 'writing-on-github'); ?> <?php echo esc_html($message); ?></p> | |
| 194 | 194 | </div><?php | 
| 195 | - delete_option( '_wogh_export_error' ); | |
| 195 | +            delete_option('_wogh_export_error'); | |
| 196 | 196 | } | 
| 197 | 197 | |
| 198 | -        if ( 'yes' === get_option( '_wogh_export_complete' ) ) { ?> | |
| 198 | +        if ('yes' === get_option('_wogh_export_complete')) { ?> | |
| 199 | 199 | <div class="updated"> | 
| 200 | - <p><?php esc_html_e( 'Export to GitHub completed successfully.', 'writing-on-github' );?></p> | |
| 200 | +                <p><?php esc_html_e('Export to GitHub completed successfully.', 'writing-on-github'); ?></p> | |
| 201 | 201 | </div><?php | 
| 202 | - delete_option( '_wogh_export_complete' ); | |
| 202 | +            delete_option('_wogh_export_complete'); | |
| 203 | 203 | } | 
| 204 | 204 | |
| 205 | -        if ( 'yes' === get_option( '_wogh_import_started' ) ) { ?> | |
| 205 | +        if ('yes' === get_option('_wogh_import_started')) { ?> | |
| 206 | 206 | <div class="updated"> | 
| 207 | - <p><?php esc_html_e( 'Import from GitHub started.', 'writing-on-github' ); ?></p> | |
| 207 | +            <p><?php esc_html_e('Import from GitHub started.', 'writing-on-github'); ?></p> | |
| 208 | 208 | </div><?php | 
| 209 | - delete_option( '_wogh_import_started' ); | |
| 209 | +            delete_option('_wogh_import_started'); | |
| 210 | 210 | } | 
| 211 | 211 | |
| 212 | -        if ( $message = get_option( '_wogh_import_error' ) ) { ?> | |
| 212 | +        if ($message = get_option('_wogh_import_error')) { ?> | |
| 213 | 213 | <div class="error"> | 
| 214 | - <p><?php esc_html_e( 'Import from GitHub failed with error:', 'writing-on-github' ); ?> <?php echo esc_html( $message );?></p> | |
| 214 | +            <p><?php esc_html_e('Import from GitHub failed with error:', 'writing-on-github'); ?> <?php echo esc_html($message); ?></p> | |
| 215 | 215 | </div><?php | 
| 216 | - delete_option( '_wogh_import_error' ); | |
| 216 | +            delete_option('_wogh_import_error'); | |
| 217 | 217 | } | 
| 218 | 218 | |
| 219 | -        if ( 'yes' === get_option( '_wogh_import_complete' ) ) { ?> | |
| 219 | +        if ('yes' === get_option('_wogh_import_complete')) { ?> | |
| 220 | 220 | <div class="updated"> | 
| 221 | - <p><?php esc_html_e( 'Import from GitHub completed successfully.', 'writing-on-github' );?></p> | |
| 221 | +            <p><?php esc_html_e('Import from GitHub completed successfully.', 'writing-on-github'); ?></p> | |
| 222 | 222 | </div><?php | 
| 223 | - delete_option( '_wogh_import_complete' ); | |
| 223 | +            delete_option('_wogh_import_complete'); | |
| 224 | 224 | } | 
| 225 | 225 | } | 
| 226 | 226 | |
| @@ -229,11 +229,11 @@ discard block | ||
| 229 | 229 | */ | 
| 230 | 230 |      public function add_admin_menu() { | 
| 231 | 231 | add_options_page( | 
| 232 | - __( 'Writing On GitHub', 'writing-on-github' ), | |
| 233 | - __( 'Writing On GitHub', 'writing-on-github' ), | |
| 232 | +            __('Writing On GitHub', 'writing-on-github'), | |
| 233 | +            __('Writing On GitHub', 'writing-on-github'), | |
| 234 | 234 | 'manage_options', | 
| 235 | 235 | Writing_On_GitHub::$text_domain, | 
| 236 | - array( $this, 'settings_page' ) | |
| 236 | + array($this, 'settings_page') | |
| 237 | 237 | ); | 
| 238 | 238 | } | 
| 239 | 239 | |
| @@ -241,32 +241,32 @@ discard block | ||
| 241 | 241 | * Admin callback to trigger import/export because WordPress admin routing lol | 
| 242 | 242 | */ | 
| 243 | 243 |      public function trigger_cron() { | 
| 244 | -        if ( ! current_user_can( 'manage_options' ) ) { | |
| 244 | +        if ( ! current_user_can('manage_options')) { | |
| 245 | 245 | return; | 
| 246 | 246 | } | 
| 247 | 247 | |
| 248 | -        if ( get_current_screen()->id !== 'settings_page_' . Writing_On_GitHub::$text_domain ) { | |
| 248 | +        if (get_current_screen()->id !== 'settings_page_' . Writing_On_GitHub::$text_domain) { | |
| 249 | 249 | return; | 
| 250 | 250 | } | 
| 251 | 251 | |
| 252 | -        if ( ! isset( $_GET['action'] ) ) { | |
| 252 | +        if ( ! isset($_GET['action'])) { | |
| 253 | 253 | return; | 
| 254 | 254 | } | 
| 255 | 255 | |
| 256 | -        if ( 'export' === $_GET['action'] ) { | |
| 256 | +        if ('export' === $_GET['action']) { | |
| 257 | 257 | Writing_On_GitHub::$instance->start_export(); | 
| 258 | 258 | } | 
| 259 | -        if ( 'force_export' === $_GET['action'] ) { | |
| 259 | +        if ('force_export' === $_GET['action']) { | |
| 260 | 260 | Writing_On_GitHub::$instance->start_export(true); | 
| 261 | 261 | } | 
| 262 | -        if ( 'import' === $_GET['action'] ) { | |
| 262 | +        if ('import' === $_GET['action']) { | |
| 263 | 263 | Writing_On_GitHub::$instance->start_import(); | 
| 264 | 264 | } | 
| 265 | -        if ( 'force_import' === $_GET['action'] ) { | |
| 265 | +        if ('force_import' === $_GET['action']) { | |
| 266 | 266 | Writing_On_GitHub::$instance->start_import(true); | 
| 267 | 267 | } | 
| 268 | 268 | |
| 269 | - wp_redirect( admin_url( 'options-general.php?page=writing-on-github' ) ); | |
| 269 | +        wp_redirect(admin_url('options-general.php?page=writing-on-github')); | |
| 270 | 270 | die; | 
| 271 | 271 | } | 
| 272 | 272 | } | 
| @@ -9,235 +9,235 @@ | ||
| 9 | 9 | */ | 
| 10 | 10 |  class Writing_On_GitHub_Controller { | 
| 11 | 11 | |
| 12 | - /** | |
| 13 | - * Application container. | |
| 14 | - * | |
| 15 | - * @var Writing_On_GitHub | |
| 16 | - */ | |
| 17 | - public $app; | |
| 18 | - | |
| 19 | - /** | |
| 20 | - * Instantiates a new Controller object | |
| 21 | - * | |
| 22 | - * @param Writing_On_GitHub $app Applicatio container. | |
| 23 | - */ | |
| 24 | -    public function __construct( Writing_On_GitHub $app ) { | |
| 25 | - $this->app = $app; | |
| 26 | - } | |
| 27 | - | |
| 28 | - /** | |
| 29 | - * Webhook callback as triggered from GitHub push. | |
| 30 | - * | |
| 31 | - * Reads the Webhook payload and syncs posts as necessary. | |
| 32 | - * | |
| 33 | - * @return boolean | |
| 34 | - */ | |
| 35 | -    public function pull_posts() { | |
| 36 | - $this->set_ajax(); | |
| 37 | -        if ( ! $this->app->semaphore()->is_open() ) { | |
| 38 | - return $this->app->response()->error( new WP_Error( | |
| 39 | - 'semaphore_locked', | |
| 40 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::pull_posts()' ) | |
| 41 | - ) ); | |
| 42 | - } | |
| 43 | - | |
| 44 | -        if ( ! $this->app->request()->is_secret_valid() ) { | |
| 45 | - return $this->app->response()->error( new WP_Error( | |
| 46 | - 'invalid_headers', | |
| 47 | - __( 'Failed to validate secret.', 'writing-on-github' ) | |
| 48 | - ) ); | |
| 49 | - } | |
| 50 | - | |
| 51 | - // ping | |
| 52 | -        if ( $this->app->request()->is_ping() ) { | |
| 53 | - return $this->app->response()->success( __( 'Wordpress is ready.', 'writing-on-github' ) ); | |
| 54 | - } | |
| 55 | - | |
| 56 | - // push | |
| 57 | -        if ( ! $this->app->request()->is_push() ) { | |
| 58 | - return $this->app->response()->error( new WP_Error( | |
| 59 | - 'invalid_headers', | |
| 60 | - __( 'Failed to validate webhook event.', 'writing-on-github' ) | |
| 61 | - ) ); | |
| 62 | - } | |
| 63 | - $payload = $this->app->request()->payload(); | |
| 64 | - | |
| 65 | -        if ( ! $payload->should_import() ) { | |
| 66 | - return $this->app->response()->error( new WP_Error( | |
| 67 | - 'invalid_payload', | |
| 68 | - sprintf( | |
| 69 | - __( "%s won't be imported.", 'writing-on-github' ), | |
| 70 | - strtolower( $payload->get_commit_id() ) ? : '[Missing Commit ID]' | |
| 71 | - ) | |
| 72 | - ) ); | |
| 73 | - } | |
| 74 | - | |
| 75 | - $this->app->semaphore()->lock(); | |
| 76 | - remove_action( 'save_post', array( $this, 'export_post' ) ); | |
| 77 | - remove_action( 'delete_post', array( $this, 'delete_post' ) ); | |
| 78 | - | |
| 79 | - $result = $this->app->import()->payload( $payload ); | |
| 80 | - | |
| 81 | - $this->app->semaphore()->unlock(); | |
| 82 | - | |
| 83 | -        if ( is_wp_error( $result ) ) { | |
| 84 | - /* @var WP_Error $result */ | |
| 85 | - return $this->app->response()->error( $result ); | |
| 86 | - } | |
| 87 | - | |
| 88 | - return $this->app->response()->success( $result ); | |
| 89 | - } | |
| 90 | - | |
| 91 | - /** | |
| 92 | - * Imports posts from the current master branch. | |
| 93 | - * @param integer $user_id | |
| 94 | - * @param boolean $force | |
| 95 | - * @return boolean | |
| 96 | - */ | |
| 97 | -    public function import_master( $user_id = 0, $force = false ) { | |
| 98 | -        if ( ! $this->app->semaphore()->is_open() ) { | |
| 99 | - return $this->app->response()->error( new WP_Error( | |
| 100 | - 'semaphore_locked', | |
| 101 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::import_master()' ) | |
| 102 | - ) ); | |
| 103 | - } | |
| 104 | - | |
| 105 | - $this->app->semaphore()->lock(); | |
| 106 | - remove_action( 'save_post', array( $this, 'export_post' ) ); | |
| 107 | - remove_action( 'save_post', array( $this, 'delete_post' ) ); | |
| 108 | - | |
| 109 | -        if ( $user_id ) { | |
| 110 | - wp_set_current_user( $user_id ); | |
| 111 | - } | |
| 112 | - | |
| 113 | - $result = $this->app->import()->master( $force ); | |
| 114 | - | |
| 115 | - $this->app->semaphore()->unlock(); | |
| 116 | - | |
| 117 | -        if ( is_wp_error( $result ) ) { | |
| 118 | - /* @var WP_Error $result */ | |
| 119 | - update_option( '_wogh_import_error', $result->get_error_message() ); | |
| 120 | - | |
| 121 | - return $this->app->response()->error( $result ); | |
| 122 | - } | |
| 123 | - | |
| 124 | - update_option( '_wogh_import_complete', 'yes' ); | |
| 125 | - | |
| 126 | - return $this->app->response()->success( $result ); | |
| 127 | - } | |
| 128 | - | |
| 129 | - /** | |
| 130 | - * Export all the posts in the database to GitHub. | |
| 131 | - * | |
| 132 | - * @param int $user_id | |
| 133 | - * @param boolean $force | |
| 134 | - * @return boolean | |
| 135 | - */ | |
| 136 | -    public function export_all( $user_id = 0, $force = false ) { | |
| 137 | -        if ( ! $this->app->semaphore()->is_open() ) { | |
| 138 | - return $this->app->response()->error( new WP_Error( | |
| 139 | - 'semaphore_locked', | |
| 140 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_all()' ) | |
| 141 | - ) ); | |
| 142 | - } | |
| 143 | - | |
| 144 | - $this->app->semaphore()->lock(); | |
| 145 | - | |
| 146 | -        if ( $user_id ) { | |
| 147 | - wp_set_current_user( $user_id ); | |
| 148 | - } | |
| 149 | - | |
| 150 | - $result = $this->app->export()->full($force); | |
| 151 | - $this->app->semaphore()->unlock(); | |
| 152 | - | |
| 153 | - // Maybe move option updating out of this class/upgrade message display? | |
| 154 | -        if ( is_wp_error( $result ) ) { | |
| 155 | - /* @var WP_Error $result */ | |
| 156 | - update_option( '_wogh_export_error', $result->get_error_message() ); | |
| 157 | - | |
| 158 | - return $this->app->response()->error( $result ); | |
| 159 | -        } else { | |
| 160 | - update_option( '_wogh_export_complete', 'yes' ); | |
| 161 | - update_option( '_wogh_fully_exported', 'yes' ); | |
| 162 | - | |
| 163 | - return $this->app->response()->success( $result ); | |
| 164 | - } | |
| 165 | - } | |
| 166 | - | |
| 167 | - /** | |
| 168 | - * Exports a single post to GitHub by ID. | |
| 169 | - * | |
| 170 | - * Called on the save_post hook. | |
| 171 | - * | |
| 172 | - * @param int $post_id Post ID. | |
| 173 | - * | |
| 174 | - * @return boolean | |
| 175 | - */ | |
| 176 | -    public function export_post( $post_id ) { | |
| 177 | -        if ( wp_is_post_revision( $post_id ) ) { | |
| 178 | - return; | |
| 179 | - } | |
| 180 | - | |
| 181 | -        if ( ! $this->app->semaphore()->is_open() ) { | |
| 182 | - return $this->app->response()->error( new WP_Error( | |
| 183 | - 'semaphore_locked', | |
| 184 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_post()' ) | |
| 185 | - ) ); | |
| 186 | - } | |
| 187 | - | |
| 188 | - $this->app->semaphore()->lock(); | |
| 189 | - $result = $this->app->export()->update( $post_id ); | |
| 190 | - $this->app->semaphore()->unlock(); | |
| 191 | - | |
| 192 | -        if ( is_wp_error( $result ) ) { | |
| 193 | - /* @var WP_Error $result */ | |
| 194 | - return $this->app->response()->error( $result ); | |
| 195 | - } | |
| 196 | - | |
| 197 | - return $this->app->response()->success( $result ); | |
| 198 | - } | |
| 199 | - | |
| 200 | - /** | |
| 201 | - * Removes the post from the tree. | |
| 202 | - * | |
| 203 | - * Called the delete_post hook. | |
| 204 | - * | |
| 205 | - * @param int $post_id Post ID. | |
| 206 | - * | |
| 207 | - * @return boolean | |
| 208 | - */ | |
| 209 | -    public function delete_post( $post_id ) { | |
| 210 | -        if ( wp_is_post_revision( $post_id ) ) { | |
| 211 | - return; | |
| 212 | - } | |
| 213 | - | |
| 214 | -        if ( ! $this->app->semaphore()->is_open() ) { | |
| 215 | - return $this->app->response()->error( new WP_Error( | |
| 216 | - 'semaphore_locked', | |
| 217 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::delete_post()' ) | |
| 218 | - ) ); | |
| 219 | - } | |
| 220 | - | |
| 221 | - $this->app->semaphore()->lock(); | |
| 222 | - $result = $this->app->export()->delete( $post_id ); | |
| 223 | - $this->app->semaphore()->unlock(); | |
| 224 | - | |
| 225 | -        if ( is_wp_error( $result ) ) { | |
| 226 | - /* @var WP_Error $result */ | |
| 227 | - return $this->app->response()->error( $result ); | |
| 228 | - } | |
| 229 | - | |
| 230 | - return $this->app->response()->success( $result ); | |
| 231 | - } | |
| 232 | - | |
| 233 | - /** | |
| 234 | - * Indicates we're running our own AJAX hook | |
| 235 | - * and thus should respond with JSON, rather | |
| 236 | - * than just returning data. | |
| 237 | - */ | |
| 238 | -    protected function set_ajax() { | |
| 239 | -        if ( ! defined( 'WOGH_AJAX' ) ) { | |
| 240 | - define( 'WOGH_AJAX', true ); | |
| 241 | - } | |
| 242 | - } | |
| 12 | + /** | |
| 13 | + * Application container. | |
| 14 | + * | |
| 15 | + * @var Writing_On_GitHub | |
| 16 | + */ | |
| 17 | + public $app; | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * Instantiates a new Controller object | |
| 21 | + * | |
| 22 | + * @param Writing_On_GitHub $app Applicatio container. | |
| 23 | + */ | |
| 24 | +	public function __construct( Writing_On_GitHub $app ) { | |
| 25 | + $this->app = $app; | |
| 26 | + } | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * Webhook callback as triggered from GitHub push. | |
| 30 | + * | |
| 31 | + * Reads the Webhook payload and syncs posts as necessary. | |
| 32 | + * | |
| 33 | + * @return boolean | |
| 34 | + */ | |
| 35 | +	public function pull_posts() { | |
| 36 | + $this->set_ajax(); | |
| 37 | +		if ( ! $this->app->semaphore()->is_open() ) { | |
| 38 | + return $this->app->response()->error( new WP_Error( | |
| 39 | + 'semaphore_locked', | |
| 40 | + sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::pull_posts()' ) | |
| 41 | + ) ); | |
| 42 | + } | |
| 43 | + | |
| 44 | +		if ( ! $this->app->request()->is_secret_valid() ) { | |
| 45 | + return $this->app->response()->error( new WP_Error( | |
| 46 | + 'invalid_headers', | |
| 47 | + __( 'Failed to validate secret.', 'writing-on-github' ) | |
| 48 | + ) ); | |
| 49 | + } | |
| 50 | + | |
| 51 | + // ping | |
| 52 | +		if ( $this->app->request()->is_ping() ) { | |
| 53 | + return $this->app->response()->success( __( 'Wordpress is ready.', 'writing-on-github' ) ); | |
| 54 | + } | |
| 55 | + | |
| 56 | + // push | |
| 57 | +		if ( ! $this->app->request()->is_push() ) { | |
| 58 | + return $this->app->response()->error( new WP_Error( | |
| 59 | + 'invalid_headers', | |
| 60 | + __( 'Failed to validate webhook event.', 'writing-on-github' ) | |
| 61 | + ) ); | |
| 62 | + } | |
| 63 | + $payload = $this->app->request()->payload(); | |
| 64 | + | |
| 65 | +		if ( ! $payload->should_import() ) { | |
| 66 | + return $this->app->response()->error( new WP_Error( | |
| 67 | + 'invalid_payload', | |
| 68 | + sprintf( | |
| 69 | + __( "%s won't be imported.", 'writing-on-github' ), | |
| 70 | + strtolower( $payload->get_commit_id() ) ? : '[Missing Commit ID]' | |
| 71 | + ) | |
| 72 | + ) ); | |
| 73 | + } | |
| 74 | + | |
| 75 | + $this->app->semaphore()->lock(); | |
| 76 | + remove_action( 'save_post', array( $this, 'export_post' ) ); | |
| 77 | + remove_action( 'delete_post', array( $this, 'delete_post' ) ); | |
| 78 | + | |
| 79 | + $result = $this->app->import()->payload( $payload ); | |
| 80 | + | |
| 81 | + $this->app->semaphore()->unlock(); | |
| 82 | + | |
| 83 | +		if ( is_wp_error( $result ) ) { | |
| 84 | + /* @var WP_Error $result */ | |
| 85 | + return $this->app->response()->error( $result ); | |
| 86 | + } | |
| 87 | + | |
| 88 | + return $this->app->response()->success( $result ); | |
| 89 | + } | |
| 90 | + | |
| 91 | + /** | |
| 92 | + * Imports posts from the current master branch. | |
| 93 | + * @param integer $user_id | |
| 94 | + * @param boolean $force | |
| 95 | + * @return boolean | |
| 96 | + */ | |
| 97 | +	public function import_master( $user_id = 0, $force = false ) { | |
| 98 | +		if ( ! $this->app->semaphore()->is_open() ) { | |
| 99 | + return $this->app->response()->error( new WP_Error( | |
| 100 | + 'semaphore_locked', | |
| 101 | + sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::import_master()' ) | |
| 102 | + ) ); | |
| 103 | + } | |
| 104 | + | |
| 105 | + $this->app->semaphore()->lock(); | |
| 106 | + remove_action( 'save_post', array( $this, 'export_post' ) ); | |
| 107 | + remove_action( 'save_post', array( $this, 'delete_post' ) ); | |
| 108 | + | |
| 109 | +		if ( $user_id ) { | |
| 110 | + wp_set_current_user( $user_id ); | |
| 111 | + } | |
| 112 | + | |
| 113 | + $result = $this->app->import()->master( $force ); | |
| 114 | + | |
| 115 | + $this->app->semaphore()->unlock(); | |
| 116 | + | |
| 117 | +		if ( is_wp_error( $result ) ) { | |
| 118 | + /* @var WP_Error $result */ | |
| 119 | + update_option( '_wogh_import_error', $result->get_error_message() ); | |
| 120 | + | |
| 121 | + return $this->app->response()->error( $result ); | |
| 122 | + } | |
| 123 | + | |
| 124 | + update_option( '_wogh_import_complete', 'yes' ); | |
| 125 | + | |
| 126 | + return $this->app->response()->success( $result ); | |
| 127 | + } | |
| 128 | + | |
| 129 | + /** | |
| 130 | + * Export all the posts in the database to GitHub. | |
| 131 | + * | |
| 132 | + * @param int $user_id | |
| 133 | + * @param boolean $force | |
| 134 | + * @return boolean | |
| 135 | + */ | |
| 136 | +	public function export_all( $user_id = 0, $force = false ) { | |
| 137 | +		if ( ! $this->app->semaphore()->is_open() ) { | |
| 138 | + return $this->app->response()->error( new WP_Error( | |
| 139 | + 'semaphore_locked', | |
| 140 | + sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_all()' ) | |
| 141 | + ) ); | |
| 142 | + } | |
| 143 | + | |
| 144 | + $this->app->semaphore()->lock(); | |
| 145 | + | |
| 146 | +		if ( $user_id ) { | |
| 147 | + wp_set_current_user( $user_id ); | |
| 148 | + } | |
| 149 | + | |
| 150 | + $result = $this->app->export()->full($force); | |
| 151 | + $this->app->semaphore()->unlock(); | |
| 152 | + | |
| 153 | + // Maybe move option updating out of this class/upgrade message display? | |
| 154 | +		if ( is_wp_error( $result ) ) { | |
| 155 | + /* @var WP_Error $result */ | |
| 156 | + update_option( '_wogh_export_error', $result->get_error_message() ); | |
| 157 | + | |
| 158 | + return $this->app->response()->error( $result ); | |
| 159 | +		} else { | |
| 160 | + update_option( '_wogh_export_complete', 'yes' ); | |
| 161 | + update_option( '_wogh_fully_exported', 'yes' ); | |
| 162 | + | |
| 163 | + return $this->app->response()->success( $result ); | |
| 164 | + } | |
| 165 | + } | |
| 166 | + | |
| 167 | + /** | |
| 168 | + * Exports a single post to GitHub by ID. | |
| 169 | + * | |
| 170 | + * Called on the save_post hook. | |
| 171 | + * | |
| 172 | + * @param int $post_id Post ID. | |
| 173 | + * | |
| 174 | + * @return boolean | |
| 175 | + */ | |
| 176 | +	public function export_post( $post_id ) { | |
| 177 | +		if ( wp_is_post_revision( $post_id ) ) { | |
| 178 | + return; | |
| 179 | + } | |
| 180 | + | |
| 181 | +		if ( ! $this->app->semaphore()->is_open() ) { | |
| 182 | + return $this->app->response()->error( new WP_Error( | |
| 183 | + 'semaphore_locked', | |
| 184 | + sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_post()' ) | |
| 185 | + ) ); | |
| 186 | + } | |
| 187 | + | |
| 188 | + $this->app->semaphore()->lock(); | |
| 189 | + $result = $this->app->export()->update( $post_id ); | |
| 190 | + $this->app->semaphore()->unlock(); | |
| 191 | + | |
| 192 | +		if ( is_wp_error( $result ) ) { | |
| 193 | + /* @var WP_Error $result */ | |
| 194 | + return $this->app->response()->error( $result ); | |
| 195 | + } | |
| 196 | + | |
| 197 | + return $this->app->response()->success( $result ); | |
| 198 | + } | |
| 199 | + | |
| 200 | + /** | |
| 201 | + * Removes the post from the tree. | |
| 202 | + * | |
| 203 | + * Called the delete_post hook. | |
| 204 | + * | |
| 205 | + * @param int $post_id Post ID. | |
| 206 | + * | |
| 207 | + * @return boolean | |
| 208 | + */ | |
| 209 | +	public function delete_post( $post_id ) { | |
| 210 | +		if ( wp_is_post_revision( $post_id ) ) { | |
| 211 | + return; | |
| 212 | + } | |
| 213 | + | |
| 214 | +		if ( ! $this->app->semaphore()->is_open() ) { | |
| 215 | + return $this->app->response()->error( new WP_Error( | |
| 216 | + 'semaphore_locked', | |
| 217 | + sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::delete_post()' ) | |
| 218 | + ) ); | |
| 219 | + } | |
| 220 | + | |
| 221 | + $this->app->semaphore()->lock(); | |
| 222 | + $result = $this->app->export()->delete( $post_id ); | |
| 223 | + $this->app->semaphore()->unlock(); | |
| 224 | + | |
| 225 | +		if ( is_wp_error( $result ) ) { | |
| 226 | + /* @var WP_Error $result */ | |
| 227 | + return $this->app->response()->error( $result ); | |
| 228 | + } | |
| 229 | + | |
| 230 | + return $this->app->response()->success( $result ); | |
| 231 | + } | |
| 232 | + | |
| 233 | + /** | |
| 234 | + * Indicates we're running our own AJAX hook | |
| 235 | + * and thus should respond with JSON, rather | |
| 236 | + * than just returning data. | |
| 237 | + */ | |
| 238 | +	protected function set_ajax() { | |
| 239 | +		if ( ! defined( 'WOGH_AJAX' ) ) { | |
| 240 | + define( 'WOGH_AJAX', true ); | |
| 241 | + } | |
| 242 | + } | |
| 243 | 243 | } | 
| @@ -21,7 +21,7 @@ discard block | ||
| 21 | 21 | * | 
| 22 | 22 | * @param Writing_On_GitHub $app Applicatio container. | 
| 23 | 23 | */ | 
| 24 | -    public function __construct( Writing_On_GitHub $app ) { | |
| 24 | +    public function __construct(Writing_On_GitHub $app) { | |
| 25 | 25 | $this->app = $app; | 
| 26 | 26 | } | 
| 27 | 27 | |
| @@ -34,58 +34,58 @@ discard block | ||
| 34 | 34 | */ | 
| 35 | 35 |      public function pull_posts() { | 
| 36 | 36 | $this->set_ajax(); | 
| 37 | -        if ( ! $this->app->semaphore()->is_open() ) { | |
| 38 | - return $this->app->response()->error( new WP_Error( | |
| 37 | +        if ( ! $this->app->semaphore()->is_open()) { | |
| 38 | + return $this->app->response()->error(new WP_Error( | |
| 39 | 39 | 'semaphore_locked', | 
| 40 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::pull_posts()' ) | |
| 41 | - ) ); | |
| 40 | +                sprintf(__('%s : Semaphore is locked, import/export already in progress.', 'writing-on-github'), 'Controller::pull_posts()') | |
| 41 | + )); | |
| 42 | 42 | } | 
| 43 | 43 | |
| 44 | -        if ( ! $this->app->request()->is_secret_valid() ) { | |
| 45 | - return $this->app->response()->error( new WP_Error( | |
| 44 | +        if ( ! $this->app->request()->is_secret_valid()) { | |
| 45 | + return $this->app->response()->error(new WP_Error( | |
| 46 | 46 | 'invalid_headers', | 
| 47 | - __( 'Failed to validate secret.', 'writing-on-github' ) | |
| 48 | - ) ); | |
| 47 | +                __('Failed to validate secret.', 'writing-on-github') | |
| 48 | + )); | |
| 49 | 49 | } | 
| 50 | 50 | |
| 51 | 51 | // ping | 
| 52 | -        if ( $this->app->request()->is_ping() ) { | |
| 53 | - return $this->app->response()->success( __( 'Wordpress is ready.', 'writing-on-github' ) ); | |
| 52 | +        if ($this->app->request()->is_ping()) { | |
| 53 | +            return $this->app->response()->success(__('Wordpress is ready.', 'writing-on-github')); | |
| 54 | 54 | } | 
| 55 | 55 | |
| 56 | 56 | // push | 
| 57 | -        if ( ! $this->app->request()->is_push() ) { | |
| 58 | - return $this->app->response()->error( new WP_Error( | |
| 57 | +        if ( ! $this->app->request()->is_push()) { | |
| 58 | + return $this->app->response()->error(new WP_Error( | |
| 59 | 59 | 'invalid_headers', | 
| 60 | - __( 'Failed to validate webhook event.', 'writing-on-github' ) | |
| 61 | - ) ); | |
| 60 | +                __('Failed to validate webhook event.', 'writing-on-github') | |
| 61 | + )); | |
| 62 | 62 | } | 
| 63 | 63 | $payload = $this->app->request()->payload(); | 
| 64 | 64 | |
| 65 | -        if ( ! $payload->should_import() ) { | |
| 66 | - return $this->app->response()->error( new WP_Error( | |
| 65 | +        if ( ! $payload->should_import()) { | |
| 66 | + return $this->app->response()->error(new WP_Error( | |
| 67 | 67 | 'invalid_payload', | 
| 68 | 68 | sprintf( | 
| 69 | - __( "%s won't be imported.", 'writing-on-github' ), | |
| 70 | - strtolower( $payload->get_commit_id() ) ? : '[Missing Commit ID]' | |
| 69 | +                    __("%s won't be imported.", 'writing-on-github'), | |
| 70 | + strtolower($payload->get_commit_id()) ?: '[Missing Commit ID]' | |
| 71 | 71 | ) | 
| 72 | - ) ); | |
| 72 | + )); | |
| 73 | 73 | } | 
| 74 | 74 | |
| 75 | 75 | $this->app->semaphore()->lock(); | 
| 76 | - remove_action( 'save_post', array( $this, 'export_post' ) ); | |
| 77 | - remove_action( 'delete_post', array( $this, 'delete_post' ) ); | |
| 76 | +        remove_action('save_post', array($this, 'export_post')); | |
| 77 | +        remove_action('delete_post', array($this, 'delete_post')); | |
| 78 | 78 | |
| 79 | - $result = $this->app->import()->payload( $payload ); | |
| 79 | + $result = $this->app->import()->payload($payload); | |
| 80 | 80 | |
| 81 | 81 | $this->app->semaphore()->unlock(); | 
| 82 | 82 | |
| 83 | -        if ( is_wp_error( $result ) ) { | |
| 83 | +        if (is_wp_error($result)) { | |
| 84 | 84 | /* @var WP_Error $result */ | 
| 85 | - return $this->app->response()->error( $result ); | |
| 85 | + return $this->app->response()->error($result); | |
| 86 | 86 | } | 
| 87 | 87 | |
| 88 | - return $this->app->response()->success( $result ); | |
| 88 | + return $this->app->response()->success($result); | |
| 89 | 89 | } | 
| 90 | 90 | |
| 91 | 91 | /** | 
| @@ -94,36 +94,36 @@ discard block | ||
| 94 | 94 | * @param boolean $force | 
| 95 | 95 | * @return boolean | 
| 96 | 96 | */ | 
| 97 | -    public function import_master( $user_id = 0, $force = false ) { | |
| 98 | -        if ( ! $this->app->semaphore()->is_open() ) { | |
| 99 | - return $this->app->response()->error( new WP_Error( | |
| 97 | +    public function import_master($user_id = 0, $force = false) { | |
| 98 | +        if ( ! $this->app->semaphore()->is_open()) { | |
| 99 | + return $this->app->response()->error(new WP_Error( | |
| 100 | 100 | 'semaphore_locked', | 
| 101 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::import_master()' ) | |
| 102 | - ) ); | |
| 101 | +                sprintf(__('%s : Semaphore is locked, import/export already in progress.', 'writing-on-github'), 'Controller::import_master()') | |
| 102 | + )); | |
| 103 | 103 | } | 
| 104 | 104 | |
| 105 | 105 | $this->app->semaphore()->lock(); | 
| 106 | - remove_action( 'save_post', array( $this, 'export_post' ) ); | |
| 107 | - remove_action( 'save_post', array( $this, 'delete_post' ) ); | |
| 106 | +        remove_action('save_post', array($this, 'export_post')); | |
| 107 | +        remove_action('save_post', array($this, 'delete_post')); | |
| 108 | 108 | |
| 109 | -        if ( $user_id ) { | |
| 110 | - wp_set_current_user( $user_id ); | |
| 109 | +        if ($user_id) { | |
| 110 | + wp_set_current_user($user_id); | |
| 111 | 111 | } | 
| 112 | 112 | |
| 113 | - $result = $this->app->import()->master( $force ); | |
| 113 | + $result = $this->app->import()->master($force); | |
| 114 | 114 | |
| 115 | 115 | $this->app->semaphore()->unlock(); | 
| 116 | 116 | |
| 117 | -        if ( is_wp_error( $result ) ) { | |
| 117 | +        if (is_wp_error($result)) { | |
| 118 | 118 | /* @var WP_Error $result */ | 
| 119 | - update_option( '_wogh_import_error', $result->get_error_message() ); | |
| 119 | +            update_option('_wogh_import_error', $result->get_error_message()); | |
| 120 | 120 | |
| 121 | - return $this->app->response()->error( $result ); | |
| 121 | + return $this->app->response()->error($result); | |
| 122 | 122 | } | 
| 123 | 123 | |
| 124 | - update_option( '_wogh_import_complete', 'yes' ); | |
| 124 | +        update_option('_wogh_import_complete', 'yes'); | |
| 125 | 125 | |
| 126 | - return $this->app->response()->success( $result ); | |
| 126 | + return $this->app->response()->success($result); | |
| 127 | 127 | } | 
| 128 | 128 | |
| 129 | 129 | /** | 
| @@ -133,34 +133,34 @@ discard block | ||
| 133 | 133 | * @param boolean $force | 
| 134 | 134 | * @return boolean | 
| 135 | 135 | */ | 
| 136 | -    public function export_all( $user_id = 0, $force = false ) { | |
| 137 | -        if ( ! $this->app->semaphore()->is_open() ) { | |
| 138 | - return $this->app->response()->error( new WP_Error( | |
| 136 | +    public function export_all($user_id = 0, $force = false) { | |
| 137 | +        if ( ! $this->app->semaphore()->is_open()) { | |
| 138 | + return $this->app->response()->error(new WP_Error( | |
| 139 | 139 | 'semaphore_locked', | 
| 140 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_all()' ) | |
| 141 | - ) ); | |
| 140 | +                sprintf(__('%s : Semaphore is locked, import/export already in progress.', 'writing-on-github'), 'Controller::export_all()') | |
| 141 | + )); | |
| 142 | 142 | } | 
| 143 | 143 | |
| 144 | 144 | $this->app->semaphore()->lock(); | 
| 145 | 145 | |
| 146 | -        if ( $user_id ) { | |
| 147 | - wp_set_current_user( $user_id ); | |
| 146 | +        if ($user_id) { | |
| 147 | + wp_set_current_user($user_id); | |
| 148 | 148 | } | 
| 149 | 149 | |
| 150 | 150 | $result = $this->app->export()->full($force); | 
| 151 | 151 | $this->app->semaphore()->unlock(); | 
| 152 | 152 | |
| 153 | 153 | // Maybe move option updating out of this class/upgrade message display? | 
| 154 | -        if ( is_wp_error( $result ) ) { | |
| 154 | +        if (is_wp_error($result)) { | |
| 155 | 155 | /* @var WP_Error $result */ | 
| 156 | - update_option( '_wogh_export_error', $result->get_error_message() ); | |
| 156 | +            update_option('_wogh_export_error', $result->get_error_message()); | |
| 157 | 157 | |
| 158 | - return $this->app->response()->error( $result ); | |
| 158 | + return $this->app->response()->error($result); | |
| 159 | 159 |          } else { | 
| 160 | - update_option( '_wogh_export_complete', 'yes' ); | |
| 161 | - update_option( '_wogh_fully_exported', 'yes' ); | |
| 160 | +            update_option('_wogh_export_complete', 'yes'); | |
| 161 | +            update_option('_wogh_fully_exported', 'yes'); | |
| 162 | 162 | |
| 163 | - return $this->app->response()->success( $result ); | |
| 163 | + return $this->app->response()->success($result); | |
| 164 | 164 | } | 
| 165 | 165 | } | 
| 166 | 166 | |
| @@ -173,28 +173,28 @@ discard block | ||
| 173 | 173 | * | 
| 174 | 174 | * @return boolean | 
| 175 | 175 | */ | 
| 176 | -    public function export_post( $post_id ) { | |
| 177 | -        if ( wp_is_post_revision( $post_id ) ) { | |
| 176 | +    public function export_post($post_id) { | |
| 177 | +        if (wp_is_post_revision($post_id)) { | |
| 178 | 178 | return; | 
| 179 | 179 | } | 
| 180 | 180 | |
| 181 | -        if ( ! $this->app->semaphore()->is_open() ) { | |
| 182 | - return $this->app->response()->error( new WP_Error( | |
| 181 | +        if ( ! $this->app->semaphore()->is_open()) { | |
| 182 | + return $this->app->response()->error(new WP_Error( | |
| 183 | 183 | 'semaphore_locked', | 
| 184 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_post()' ) | |
| 185 | - ) ); | |
| 184 | +                sprintf(__('%s : Semaphore is locked, import/export already in progress.', 'writing-on-github'), 'Controller::export_post()') | |
| 185 | + )); | |
| 186 | 186 | } | 
| 187 | 187 | |
| 188 | 188 | $this->app->semaphore()->lock(); | 
| 189 | - $result = $this->app->export()->update( $post_id ); | |
| 189 | + $result = $this->app->export()->update($post_id); | |
| 190 | 190 | $this->app->semaphore()->unlock(); | 
| 191 | 191 | |
| 192 | -        if ( is_wp_error( $result ) ) { | |
| 192 | +        if (is_wp_error($result)) { | |
| 193 | 193 | /* @var WP_Error $result */ | 
| 194 | - return $this->app->response()->error( $result ); | |
| 194 | + return $this->app->response()->error($result); | |
| 195 | 195 | } | 
| 196 | 196 | |
| 197 | - return $this->app->response()->success( $result ); | |
| 197 | + return $this->app->response()->success($result); | |
| 198 | 198 | } | 
| 199 | 199 | |
| 200 | 200 | /** | 
| @@ -206,28 +206,28 @@ discard block | ||
| 206 | 206 | * | 
| 207 | 207 | * @return boolean | 
| 208 | 208 | */ | 
| 209 | -    public function delete_post( $post_id ) { | |
| 210 | -        if ( wp_is_post_revision( $post_id ) ) { | |
| 209 | +    public function delete_post($post_id) { | |
| 210 | +        if (wp_is_post_revision($post_id)) { | |
| 211 | 211 | return; | 
| 212 | 212 | } | 
| 213 | 213 | |
| 214 | -        if ( ! $this->app->semaphore()->is_open() ) { | |
| 215 | - return $this->app->response()->error( new WP_Error( | |
| 214 | +        if ( ! $this->app->semaphore()->is_open()) { | |
| 215 | + return $this->app->response()->error(new WP_Error( | |
| 216 | 216 | 'semaphore_locked', | 
| 217 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::delete_post()' ) | |
| 218 | - ) ); | |
| 217 | +                sprintf(__('%s : Semaphore is locked, import/export already in progress.', 'writing-on-github'), 'Controller::delete_post()') | |
| 218 | + )); | |
| 219 | 219 | } | 
| 220 | 220 | |
| 221 | 221 | $this->app->semaphore()->lock(); | 
| 222 | - $result = $this->app->export()->delete( $post_id ); | |
| 222 | + $result = $this->app->export()->delete($post_id); | |
| 223 | 223 | $this->app->semaphore()->unlock(); | 
| 224 | 224 | |
| 225 | -        if ( is_wp_error( $result ) ) { | |
| 225 | +        if (is_wp_error($result)) { | |
| 226 | 226 | /* @var WP_Error $result */ | 
| 227 | - return $this->app->response()->error( $result ); | |
| 227 | + return $this->app->response()->error($result); | |
| 228 | 228 | } | 
| 229 | 229 | |
| 230 | - return $this->app->response()->success( $result ); | |
| 230 | + return $this->app->response()->success($result); | |
| 231 | 231 | } | 
| 232 | 232 | |
| 233 | 233 | /** | 
| @@ -236,8 +236,8 @@ discard block | ||
| 236 | 236 | * than just returning data. | 
| 237 | 237 | */ | 
| 238 | 238 |      protected function set_ajax() { | 
| 239 | -        if ( ! defined( 'WOGH_AJAX' ) ) { | |
| 240 | - define( 'WOGH_AJAX', true ); | |
| 239 | +        if ( ! defined('WOGH_AJAX')) { | |
| 240 | +            define('WOGH_AJAX', true); | |
| 241 | 241 | } | 
| 242 | 242 | } | 
| 243 | 243 | } | 
| @@ -10,321 +10,321 @@ | ||
| 10 | 10 | */ | 
| 11 | 11 |  class Writing_On_GitHub_Import { | 
| 12 | 12 | |
| 13 | - /** | |
| 14 | - * Application container. | |
| 15 | - * | |
| 16 | - * @var Writing_On_GitHub | |
| 17 | - */ | |
| 18 | - protected $app; | |
| 19 | - | |
| 20 | - /** | |
| 21 | - * Initializes a new import manager. | |
| 22 | - * | |
| 23 | - * @param Writing_On_GitHub $app Application container. | |
| 24 | - */ | |
| 25 | -    public function __construct( Writing_On_GitHub $app ) { | |
| 26 | - $this->app = $app; | |
| 27 | - } | |
| 28 | - | |
| 29 | - /** | |
| 30 | - * Imports a payload. | |
| 31 | - * @param Writing_On_GitHub_Payload $payload | |
| 32 | - * | |
| 33 | - * @return string|WP_Error | |
| 34 | - */ | |
| 35 | -    public function payload( Writing_On_GitHub_Payload $payload ) { | |
| 36 | - | |
| 37 | - $result = $this->app->api()->fetch()->compare( $payload->get_before_commit_id() ); | |
| 38 | - | |
| 39 | -        if ( is_wp_error( $result ) ) { | |
| 40 | - /* @var WP_Error $result */ | |
| 41 | - return $result; | |
| 42 | - } | |
| 43 | - | |
| 44 | -        if ( is_array( $result ) ) { | |
| 45 | - $result = $this->import_files( $result ); | |
| 46 | - } | |
| 47 | - | |
| 48 | -        if ( is_wp_error( $result ) ) { | |
| 49 | - return $files; | |
| 50 | - } | |
| 51 | - | |
| 52 | - return __( 'Payload processed', 'writing-on-github' ); | |
| 53 | - } | |
| 54 | - | |
| 55 | - /** | |
| 56 | - * import blob by files | |
| 57 | - * @param Writing_On_GitHub_File_Info[] $files | |
| 58 | - * @param boolean $force | |
| 59 | - * | |
| 60 | - * @return true|WP_Error | |
| 61 | - */ | |
| 62 | -    protected function import_files( $files, $force = false ) { | |
| 63 | - | |
| 64 | - $error = true; | |
| 65 | - | |
| 66 | -        foreach ( $files as $file ) { | |
| 67 | -            if ( ! $this->importable_file( $file ) ) { | |
| 68 | - continue; | |
| 69 | - } | |
| 70 | - | |
| 71 | - $blob = $this->app->api()->fetch()->blob( $file ); | |
| 72 | - // network error ? | |
| 73 | -            if ( ! $blob instanceof Writing_On_GitHub_Blob ) { | |
| 74 | - continue; | |
| 75 | - } | |
| 76 | - | |
| 77 | - $is_remove = 'removed' == $file->status; | |
| 78 | - | |
| 79 | - $result = false; | |
| 80 | -            if ( $this->importable_raw_file( $blob ) ) { | |
| 81 | - $result = $this->import_raw_file( $blob, $is_remove ); | |
| 82 | -            } elseif ( $this->importable_post( $blob ) ) { | |
| 83 | -                if ( $is_remove ) { | |
| 84 | - $result = $this->delete_post( $blob ); | |
| 85 | -                } else { | |
| 86 | - $result = $this->import_post( $blob, $force ); | |
| 87 | - } | |
| 88 | - } | |
| 89 | - | |
| 90 | -            if ( is_wp_error( $result ) ) { | |
| 91 | - /* @var WP_Error $result */ | |
| 92 | - $error = wogh_append_error( $error, $result ); | |
| 93 | - } | |
| 94 | - } | |
| 95 | - | |
| 96 | - return $error; | |
| 97 | - } | |
| 98 | - | |
| 99 | - /** | |
| 100 | - * Imports the latest commit on the master branch. | |
| 101 | - * | |
| 102 | - * @param boolean $force | |
| 103 | - * @return string|WP_Error | |
| 104 | - */ | |
| 105 | -    public function master( $force = false ) { | |
| 106 | - $result = $this->app->api()->fetch()->tree_recursive(); | |
| 107 | - | |
| 108 | -        if ( is_wp_error( $result ) ) { | |
| 109 | - /* @var WP_Error $result */ | |
| 110 | - return $result; | |
| 111 | - } | |
| 112 | - | |
| 113 | -        if ( is_array( $result ) ) { | |
| 114 | - $result = $this->import_files( $result, $force ); | |
| 115 | - } | |
| 116 | - | |
| 117 | -        if ( is_wp_error( $result ) ) { | |
| 118 | - /* @var WP_Error $result */ | |
| 119 | - return $result; | |
| 120 | - } | |
| 121 | - | |
| 122 | - return __( 'Payload processed', 'writing-on-github' ); | |
| 123 | - } | |
| 124 | - | |
| 125 | - /** | |
| 126 | - * Checks whether the provided blob should be imported. | |
| 127 | - * | |
| 128 | - * @param Writing_On_GitHub_File_Info $file | |
| 129 | - * | |
| 130 | - * @return bool | |
| 131 | - */ | |
| 132 | -    protected function importable_file( Writing_On_GitHub_File_Info $file ) { | |
| 133 | - | |
| 134 | - $path = $file->path; | |
| 135 | - | |
| 136 | - // only _pages, _posts and images | |
| 137 | - $prefixs = array( '_pages/', '_posts/', 'images/'); | |
| 138 | -        foreach ($prefixs as $prefix) { | |
| 139 | -            if ( ! strncasecmp($path, $prefix, strlen( $prefix ) ) ) { | |
| 140 | - return true; | |
| 141 | - } | |
| 142 | - } | |
| 143 | - return false; | |
| 144 | - } | |
| 145 | - | |
| 146 | - /** | |
| 147 | - * Checks whether the provided blob should be imported. | |
| 148 | - * | |
| 149 | - * @param Writing_On_GitHub_Blob $blob Blob to validate. | |
| 150 | - * | |
| 151 | - * @return bool | |
| 152 | - */ | |
| 153 | -    protected function importable_post( Writing_On_GitHub_Blob $blob ) { | |
| 154 | - // global $wpdb; | |
| 155 | - | |
| 156 | - // // Skip the repo's readme. | |
| 157 | -        // if ( 'readme' === strtolower( substr( $blob->path(), 0, 6 ) ) ) { | |
| 158 | - // return false; | |
| 159 | - // } | |
| 160 | - | |
| 161 | - // // If the blob sha already matches a post, then move on. | |
| 162 | -        // if ( ! is_wp_error( $this->app->database()->fetch_by_sha( $blob->sha() ) ) ) { | |
| 163 | - // return false; | |
| 164 | - // } | |
| 165 | - | |
| 166 | -        if ( ! $blob->has_frontmatter() ) { | |
| 167 | - return false; | |
| 168 | - } | |
| 169 | - | |
| 170 | - return true; | |
| 171 | - } | |
| 172 | - | |
| 173 | - /** | |
| 174 | - * Delete post | |
| 175 | - * @param Writing_On_GitHub_Blob $blob | |
| 176 | - * @return WP_Error|bool | |
| 177 | - */ | |
| 178 | -    protected function delete_post( Writing_On_GitHub_Blob $blob ) { | |
| 179 | - $id = $blob->id(); | |
| 180 | -        if ( empty( $id ) ) { | |
| 181 | - return false; | |
| 182 | - } | |
| 183 | - $result = $this->app->database()->delete_post( $id ); | |
| 184 | -        if ( is_wp_error( $result ) ) { | |
| 185 | - /* @var WP_Error $result */ | |
| 186 | - return $result; | |
| 187 | - } | |
| 188 | - return true; | |
| 189 | - } | |
| 190 | - | |
| 191 | - /** | |
| 192 | - * Imports a post into wordpress | |
| 193 | - * @param Writing_On_GitHub_Blob $blob | |
| 194 | - * @param boolean $force | |
| 195 | - * @return WP_Error|bool | |
| 196 | - */ | |
| 197 | -    protected function import_post( Writing_On_GitHub_Blob $blob, $force = false ) { | |
| 198 | - $post = $this->blob_to_post( $blob, $force ); | |
| 199 | - | |
| 200 | -        if ( ! $post instanceof Writing_On_GitHub_Post ) { | |
| 201 | - return false; | |
| 202 | - } | |
| 203 | - | |
| 204 | - $result = $this->app->database()->save_post( $post ); | |
| 205 | -        if ( is_wp_error( $result ) ) { | |
| 206 | - /** @var WP_Error $result */ | |
| 207 | - return $result; | |
| 208 | - } | |
| 209 | - | |
| 210 | - if ( $post->is_new() || | |
| 211 | -                ! wogh_equal_front_matter( $post, $blob ) ) { | |
| 212 | - | |
| 213 | - $result = $this->app->export()->export_post( $post ); | |
| 214 | - | |
| 215 | -            if ( is_wp_error( $result ) ) { | |
| 216 | - /** @var WP_Error $result */ | |
| 217 | - return $result; | |
| 218 | - } | |
| 219 | - } | |
| 220 | - | |
| 221 | - clean_post_cache( $post->id() ); | |
| 222 | - | |
| 223 | - return true; | |
| 224 | - } | |
| 225 | - | |
| 226 | - /** | |
| 227 | - * import raw file | |
| 228 | - * @param Writing_On_GitHub_Blob $blob | |
| 229 | - * @return bool | |
| 230 | - */ | |
| 231 | -    protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) { | |
| 232 | -        if ( $blob->has_frontmatter() ) { | |
| 233 | - return false; | |
| 234 | - } | |
| 235 | - | |
| 236 | - // only images | |
| 237 | -        if ( strncasecmp($blob->path(), 'images/', strlen('images/') ) != 0) { | |
| 238 | - return false; | |
| 239 | - } | |
| 240 | - | |
| 241 | - return true; | |
| 242 | - } | |
| 243 | - | |
| 244 | - /** | |
| 245 | - * Imports a raw file content into file system. | |
| 246 | - * @param Writing_On_GitHub_Blob $blob | |
| 247 | - * @param bool $is_remove | |
| 248 | - */ | |
| 249 | -    protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) { | |
| 250 | - $arr = wp_upload_dir(); | |
| 251 | - $path = $arr['basedir'] . '/writing-on-github/' . $blob->path(); | |
| 252 | -        if ( $is_remove ) { | |
| 253 | -            if ( file_exists($path) ) { | |
| 254 | - unlink($path); | |
| 255 | - } | |
| 256 | -        } else { | |
| 257 | - $dirname = dirname($path); | |
| 258 | -            if ( ! file_exists($dirname) ) { | |
| 259 | - wp_mkdir_p($dirname); | |
| 260 | - } | |
| 261 | - | |
| 262 | - file_put_contents($path, $blob->content()); | |
| 263 | - } | |
| 264 | - return true; | |
| 265 | - } | |
| 266 | - | |
| 267 | - /** | |
| 268 | - * Imports a single blob content into matching post. | |
| 269 | - * | |
| 270 | - * @param Writing_On_GitHub_Blob $blob Blob to transform into a Post. | |
| 271 | - * @param boolean $force | |
| 272 | - * | |
| 273 | - * @return Writing_On_GitHub_Post|false | |
| 274 | - */ | |
| 275 | -    protected function blob_to_post( Writing_On_GitHub_Blob $blob, $force = false ) { | |
| 276 | - $args = array( 'post_content' => $blob->content_import() ); | |
| 277 | - $meta = $blob->meta(); | |
| 278 | - | |
| 279 | - $id = false; | |
| 280 | - | |
| 281 | -        if ( ! empty( $meta ) ) { | |
| 282 | -            if ( array_key_exists( 'layout', $meta ) ) { | |
| 283 | - $args['post_type'] = $meta['layout']; | |
| 284 | - unset( $meta['layout'] ); | |
| 285 | - } | |
| 286 | - | |
| 287 | -            if ( array_key_exists( 'published', $meta ) ) { | |
| 288 | - $args['post_status'] = true === $meta['published'] ? 'publish' : 'draft'; | |
| 289 | - unset( $meta['published'] ); | |
| 290 | - } | |
| 291 | - | |
| 292 | -            if ( array_key_exists( 'post_title', $meta ) ) { | |
| 293 | - $args['post_title'] = $meta['post_title']; | |
| 294 | - unset( $meta['post_title'] ); | |
| 295 | - } | |
| 296 | - | |
| 297 | -            if ( array_key_exists( 'post_name', $meta ) ) { | |
| 298 | - $args['post_name'] = $meta['post_name']; | |
| 299 | - unset( $meta['post_name'] ); | |
| 300 | - } | |
| 301 | - | |
| 302 | -            if ( array_key_exists( 'ID', $meta ) ) { | |
| 303 | - $id = $args['ID'] = $meta['ID']; | |
| 304 | - $blob->set_id($id); | |
| 305 | - unset( $meta['ID'] ); | |
| 306 | - } | |
| 307 | - } | |
| 308 | - | |
| 309 | - $meta['_wogh_sha'] = $blob->sha(); | |
| 310 | - | |
| 311 | -        if ( ! $force && $id ) { | |
| 312 | - $old_sha = get_post_meta( $id, '_wogh_sha', true ); | |
| 313 | - $old_github_path = get_post_meta( $id, '_wogh_github_path', true ); | |
| 314 | - | |
| 315 | - // dont save post when has same sha | |
| 316 | - if ( $old_sha && $old_sha == $meta['_wogh_sha'] && | |
| 317 | -                 $old_github_path && $old_github_path == $blob->path() ) { | |
| 318 | - return false; | |
| 319 | - } | |
| 320 | - } | |
| 321 | - | |
| 322 | - $post = new Writing_On_GitHub_Post( $args, $this->app->api() ); | |
| 323 | - $post->set_old_github_path( $blob->path() ); | |
| 324 | - $post->set_meta( $meta ); | |
| 325 | - $post->set_blob( $blob ); | |
| 326 | - $blob->set_id( $post->id() ); | |
| 327 | - | |
| 328 | - return $post; | |
| 329 | - } | |
| 13 | + /** | |
| 14 | + * Application container. | |
| 15 | + * | |
| 16 | + * @var Writing_On_GitHub | |
| 17 | + */ | |
| 18 | + protected $app; | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * Initializes a new import manager. | |
| 22 | + * | |
| 23 | + * @param Writing_On_GitHub $app Application container. | |
| 24 | + */ | |
| 25 | +	public function __construct( Writing_On_GitHub $app ) { | |
| 26 | + $this->app = $app; | |
| 27 | + } | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * Imports a payload. | |
| 31 | + * @param Writing_On_GitHub_Payload $payload | |
| 32 | + * | |
| 33 | + * @return string|WP_Error | |
| 34 | + */ | |
| 35 | +	public function payload( Writing_On_GitHub_Payload $payload ) { | |
| 36 | + | |
| 37 | + $result = $this->app->api()->fetch()->compare( $payload->get_before_commit_id() ); | |
| 38 | + | |
| 39 | +		if ( is_wp_error( $result ) ) { | |
| 40 | + /* @var WP_Error $result */ | |
| 41 | + return $result; | |
| 42 | + } | |
| 43 | + | |
| 44 | +		if ( is_array( $result ) ) { | |
| 45 | + $result = $this->import_files( $result ); | |
| 46 | + } | |
| 47 | + | |
| 48 | +		if ( is_wp_error( $result ) ) { | |
| 49 | + return $files; | |
| 50 | + } | |
| 51 | + | |
| 52 | + return __( 'Payload processed', 'writing-on-github' ); | |
| 53 | + } | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * import blob by files | |
| 57 | + * @param Writing_On_GitHub_File_Info[] $files | |
| 58 | + * @param boolean $force | |
| 59 | + * | |
| 60 | + * @return true|WP_Error | |
| 61 | + */ | |
| 62 | +	protected function import_files( $files, $force = false ) { | |
| 63 | + | |
| 64 | + $error = true; | |
| 65 | + | |
| 66 | +		foreach ( $files as $file ) { | |
| 67 | +			if ( ! $this->importable_file( $file ) ) { | |
| 68 | + continue; | |
| 69 | + } | |
| 70 | + | |
| 71 | + $blob = $this->app->api()->fetch()->blob( $file ); | |
| 72 | + // network error ? | |
| 73 | +			if ( ! $blob instanceof Writing_On_GitHub_Blob ) { | |
| 74 | + continue; | |
| 75 | + } | |
| 76 | + | |
| 77 | + $is_remove = 'removed' == $file->status; | |
| 78 | + | |
| 79 | + $result = false; | |
| 80 | +			if ( $this->importable_raw_file( $blob ) ) { | |
| 81 | + $result = $this->import_raw_file( $blob, $is_remove ); | |
| 82 | +			} elseif ( $this->importable_post( $blob ) ) { | |
| 83 | +				if ( $is_remove ) { | |
| 84 | + $result = $this->delete_post( $blob ); | |
| 85 | +				} else { | |
| 86 | + $result = $this->import_post( $blob, $force ); | |
| 87 | + } | |
| 88 | + } | |
| 89 | + | |
| 90 | +			if ( is_wp_error( $result ) ) { | |
| 91 | + /* @var WP_Error $result */ | |
| 92 | + $error = wogh_append_error( $error, $result ); | |
| 93 | + } | |
| 94 | + } | |
| 95 | + | |
| 96 | + return $error; | |
| 97 | + } | |
| 98 | + | |
| 99 | + /** | |
| 100 | + * Imports the latest commit on the master branch. | |
| 101 | + * | |
| 102 | + * @param boolean $force | |
| 103 | + * @return string|WP_Error | |
| 104 | + */ | |
| 105 | +	public function master( $force = false ) { | |
| 106 | + $result = $this->app->api()->fetch()->tree_recursive(); | |
| 107 | + | |
| 108 | +		if ( is_wp_error( $result ) ) { | |
| 109 | + /* @var WP_Error $result */ | |
| 110 | + return $result; | |
| 111 | + } | |
| 112 | + | |
| 113 | +		if ( is_array( $result ) ) { | |
| 114 | + $result = $this->import_files( $result, $force ); | |
| 115 | + } | |
| 116 | + | |
| 117 | +		if ( is_wp_error( $result ) ) { | |
| 118 | + /* @var WP_Error $result */ | |
| 119 | + return $result; | |
| 120 | + } | |
| 121 | + | |
| 122 | + return __( 'Payload processed', 'writing-on-github' ); | |
| 123 | + } | |
| 124 | + | |
| 125 | + /** | |
| 126 | + * Checks whether the provided blob should be imported. | |
| 127 | + * | |
| 128 | + * @param Writing_On_GitHub_File_Info $file | |
| 129 | + * | |
| 130 | + * @return bool | |
| 131 | + */ | |
| 132 | +	protected function importable_file( Writing_On_GitHub_File_Info $file ) { | |
| 133 | + | |
| 134 | + $path = $file->path; | |
| 135 | + | |
| 136 | + // only _pages, _posts and images | |
| 137 | + $prefixs = array( '_pages/', '_posts/', 'images/'); | |
| 138 | +		foreach ($prefixs as $prefix) { | |
| 139 | +			if ( ! strncasecmp($path, $prefix, strlen( $prefix ) ) ) { | |
| 140 | + return true; | |
| 141 | + } | |
| 142 | + } | |
| 143 | + return false; | |
| 144 | + } | |
| 145 | + | |
| 146 | + /** | |
| 147 | + * Checks whether the provided blob should be imported. | |
| 148 | + * | |
| 149 | + * @param Writing_On_GitHub_Blob $blob Blob to validate. | |
| 150 | + * | |
| 151 | + * @return bool | |
| 152 | + */ | |
| 153 | +	protected function importable_post( Writing_On_GitHub_Blob $blob ) { | |
| 154 | + // global $wpdb; | |
| 155 | + | |
| 156 | + // // Skip the repo's readme. | |
| 157 | +		// if ( 'readme' === strtolower( substr( $blob->path(), 0, 6 ) ) ) { | |
| 158 | + // return false; | |
| 159 | + // } | |
| 160 | + | |
| 161 | + // // If the blob sha already matches a post, then move on. | |
| 162 | +		// if ( ! is_wp_error( $this->app->database()->fetch_by_sha( $blob->sha() ) ) ) { | |
| 163 | + // return false; | |
| 164 | + // } | |
| 165 | + | |
| 166 | +		if ( ! $blob->has_frontmatter() ) { | |
| 167 | + return false; | |
| 168 | + } | |
| 169 | + | |
| 170 | + return true; | |
| 171 | + } | |
| 172 | + | |
| 173 | + /** | |
| 174 | + * Delete post | |
| 175 | + * @param Writing_On_GitHub_Blob $blob | |
| 176 | + * @return WP_Error|bool | |
| 177 | + */ | |
| 178 | +	protected function delete_post( Writing_On_GitHub_Blob $blob ) { | |
| 179 | + $id = $blob->id(); | |
| 180 | +		if ( empty( $id ) ) { | |
| 181 | + return false; | |
| 182 | + } | |
| 183 | + $result = $this->app->database()->delete_post( $id ); | |
| 184 | +		if ( is_wp_error( $result ) ) { | |
| 185 | + /* @var WP_Error $result */ | |
| 186 | + return $result; | |
| 187 | + } | |
| 188 | + return true; | |
| 189 | + } | |
| 190 | + | |
| 191 | + /** | |
| 192 | + * Imports a post into wordpress | |
| 193 | + * @param Writing_On_GitHub_Blob $blob | |
| 194 | + * @param boolean $force | |
| 195 | + * @return WP_Error|bool | |
| 196 | + */ | |
| 197 | +	protected function import_post( Writing_On_GitHub_Blob $blob, $force = false ) { | |
| 198 | + $post = $this->blob_to_post( $blob, $force ); | |
| 199 | + | |
| 200 | +		if ( ! $post instanceof Writing_On_GitHub_Post ) { | |
| 201 | + return false; | |
| 202 | + } | |
| 203 | + | |
| 204 | + $result = $this->app->database()->save_post( $post ); | |
| 205 | +		if ( is_wp_error( $result ) ) { | |
| 206 | + /** @var WP_Error $result */ | |
| 207 | + return $result; | |
| 208 | + } | |
| 209 | + | |
| 210 | + if ( $post->is_new() || | |
| 211 | +				! wogh_equal_front_matter( $post, $blob ) ) { | |
| 212 | + | |
| 213 | + $result = $this->app->export()->export_post( $post ); | |
| 214 | + | |
| 215 | +			if ( is_wp_error( $result ) ) { | |
| 216 | + /** @var WP_Error $result */ | |
| 217 | + return $result; | |
| 218 | + } | |
| 219 | + } | |
| 220 | + | |
| 221 | + clean_post_cache( $post->id() ); | |
| 222 | + | |
| 223 | + return true; | |
| 224 | + } | |
| 225 | + | |
| 226 | + /** | |
| 227 | + * import raw file | |
| 228 | + * @param Writing_On_GitHub_Blob $blob | |
| 229 | + * @return bool | |
| 230 | + */ | |
| 231 | +	protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) { | |
| 232 | +		if ( $blob->has_frontmatter() ) { | |
| 233 | + return false; | |
| 234 | + } | |
| 235 | + | |
| 236 | + // only images | |
| 237 | +		if ( strncasecmp($blob->path(), 'images/', strlen('images/') ) != 0) { | |
| 238 | + return false; | |
| 239 | + } | |
| 240 | + | |
| 241 | + return true; | |
| 242 | + } | |
| 243 | + | |
| 244 | + /** | |
| 245 | + * Imports a raw file content into file system. | |
| 246 | + * @param Writing_On_GitHub_Blob $blob | |
| 247 | + * @param bool $is_remove | |
| 248 | + */ | |
| 249 | +	protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) { | |
| 250 | + $arr = wp_upload_dir(); | |
| 251 | + $path = $arr['basedir'] . '/writing-on-github/' . $blob->path(); | |
| 252 | +		if ( $is_remove ) { | |
| 253 | +			if ( file_exists($path) ) { | |
| 254 | + unlink($path); | |
| 255 | + } | |
| 256 | +		} else { | |
| 257 | + $dirname = dirname($path); | |
| 258 | +			if ( ! file_exists($dirname) ) { | |
| 259 | + wp_mkdir_p($dirname); | |
| 260 | + } | |
| 261 | + | |
| 262 | + file_put_contents($path, $blob->content()); | |
| 263 | + } | |
| 264 | + return true; | |
| 265 | + } | |
| 266 | + | |
| 267 | + /** | |
| 268 | + * Imports a single blob content into matching post. | |
| 269 | + * | |
| 270 | + * @param Writing_On_GitHub_Blob $blob Blob to transform into a Post. | |
| 271 | + * @param boolean $force | |
| 272 | + * | |
| 273 | + * @return Writing_On_GitHub_Post|false | |
| 274 | + */ | |
| 275 | +	protected function blob_to_post( Writing_On_GitHub_Blob $blob, $force = false ) { | |
| 276 | + $args = array( 'post_content' => $blob->content_import() ); | |
| 277 | + $meta = $blob->meta(); | |
| 278 | + | |
| 279 | + $id = false; | |
| 280 | + | |
| 281 | +		if ( ! empty( $meta ) ) { | |
| 282 | +			if ( array_key_exists( 'layout', $meta ) ) { | |
| 283 | + $args['post_type'] = $meta['layout']; | |
| 284 | + unset( $meta['layout'] ); | |
| 285 | + } | |
| 286 | + | |
| 287 | +			if ( array_key_exists( 'published', $meta ) ) { | |
| 288 | + $args['post_status'] = true === $meta['published'] ? 'publish' : 'draft'; | |
| 289 | + unset( $meta['published'] ); | |
| 290 | + } | |
| 291 | + | |
| 292 | +			if ( array_key_exists( 'post_title', $meta ) ) { | |
| 293 | + $args['post_title'] = $meta['post_title']; | |
| 294 | + unset( $meta['post_title'] ); | |
| 295 | + } | |
| 296 | + | |
| 297 | +			if ( array_key_exists( 'post_name', $meta ) ) { | |
| 298 | + $args['post_name'] = $meta['post_name']; | |
| 299 | + unset( $meta['post_name'] ); | |
| 300 | + } | |
| 301 | + | |
| 302 | +			if ( array_key_exists( 'ID', $meta ) ) { | |
| 303 | + $id = $args['ID'] = $meta['ID']; | |
| 304 | + $blob->set_id($id); | |
| 305 | + unset( $meta['ID'] ); | |
| 306 | + } | |
| 307 | + } | |
| 308 | + | |
| 309 | + $meta['_wogh_sha'] = $blob->sha(); | |
| 310 | + | |
| 311 | +		if ( ! $force && $id ) { | |
| 312 | + $old_sha = get_post_meta( $id, '_wogh_sha', true ); | |
| 313 | + $old_github_path = get_post_meta( $id, '_wogh_github_path', true ); | |
| 314 | + | |
| 315 | + // dont save post when has same sha | |
| 316 | + if ( $old_sha && $old_sha == $meta['_wogh_sha'] && | |
| 317 | +				 $old_github_path && $old_github_path == $blob->path() ) { | |
| 318 | + return false; | |
| 319 | + } | |
| 320 | + } | |
| 321 | + | |
| 322 | + $post = new Writing_On_GitHub_Post( $args, $this->app->api() ); | |
| 323 | + $post->set_old_github_path( $blob->path() ); | |
| 324 | + $post->set_meta( $meta ); | |
| 325 | + $post->set_blob( $blob ); | |
| 326 | + $blob->set_id( $post->id() ); | |
| 327 | + | |
| 328 | + return $post; | |
| 329 | + } | |
| 330 | 330 | } | 
| @@ -22,7 +22,7 @@ discard block | ||
| 22 | 22 | * | 
| 23 | 23 | * @param Writing_On_GitHub $app Application container. | 
| 24 | 24 | */ | 
| 25 | -    public function __construct( Writing_On_GitHub $app ) { | |
| 25 | +    public function __construct(Writing_On_GitHub $app) { | |
| 26 | 26 | $this->app = $app; | 
| 27 | 27 | } | 
| 28 | 28 | |
| @@ -32,24 +32,24 @@ discard block | ||
| 32 | 32 | * | 
| 33 | 33 | * @return string|WP_Error | 
| 34 | 34 | */ | 
| 35 | -    public function payload( Writing_On_GitHub_Payload $payload ) { | |
| 35 | +    public function payload(Writing_On_GitHub_Payload $payload) { | |
| 36 | 36 | |
| 37 | - $result = $this->app->api()->fetch()->compare( $payload->get_before_commit_id() ); | |
| 37 | + $result = $this->app->api()->fetch()->compare($payload->get_before_commit_id()); | |
| 38 | 38 | |
| 39 | -        if ( is_wp_error( $result ) ) { | |
| 39 | +        if (is_wp_error($result)) { | |
| 40 | 40 | /* @var WP_Error $result */ | 
| 41 | 41 | return $result; | 
| 42 | 42 | } | 
| 43 | 43 | |
| 44 | -        if ( is_array( $result ) ) { | |
| 45 | - $result = $this->import_files( $result ); | |
| 44 | +        if (is_array($result)) { | |
| 45 | + $result = $this->import_files($result); | |
| 46 | 46 | } | 
| 47 | 47 | |
| 48 | -        if ( is_wp_error( $result ) ) { | |
| 48 | +        if (is_wp_error($result)) { | |
| 49 | 49 | return $files; | 
| 50 | 50 | } | 
| 51 | 51 | |
| 52 | - return __( 'Payload processed', 'writing-on-github' ); | |
| 52 | +        return __('Payload processed', 'writing-on-github'); | |
| 53 | 53 | } | 
| 54 | 54 | |
| 55 | 55 | /** | 
| @@ -59,37 +59,37 @@ discard block | ||
| 59 | 59 | * | 
| 60 | 60 | * @return true|WP_Error | 
| 61 | 61 | */ | 
| 62 | -    protected function import_files( $files, $force = false ) { | |
| 62 | +    protected function import_files($files, $force = false) { | |
| 63 | 63 | |
| 64 | 64 | $error = true; | 
| 65 | 65 | |
| 66 | -        foreach ( $files as $file ) { | |
| 67 | -            if ( ! $this->importable_file( $file ) ) { | |
| 66 | +        foreach ($files as $file) { | |
| 67 | +            if ( ! $this->importable_file($file)) { | |
| 68 | 68 | continue; | 
| 69 | 69 | } | 
| 70 | 70 | |
| 71 | - $blob = $this->app->api()->fetch()->blob( $file ); | |
| 71 | + $blob = $this->app->api()->fetch()->blob($file); | |
| 72 | 72 | // network error ? | 
| 73 | -            if ( ! $blob instanceof Writing_On_GitHub_Blob ) { | |
| 73 | +            if ( ! $blob instanceof Writing_On_GitHub_Blob) { | |
| 74 | 74 | continue; | 
| 75 | 75 | } | 
| 76 | 76 | |
| 77 | 77 | $is_remove = 'removed' == $file->status; | 
| 78 | 78 | |
| 79 | 79 | $result = false; | 
| 80 | -            if ( $this->importable_raw_file( $blob ) ) { | |
| 81 | - $result = $this->import_raw_file( $blob, $is_remove ); | |
| 82 | -            } elseif ( $this->importable_post( $blob ) ) { | |
| 83 | -                if ( $is_remove ) { | |
| 84 | - $result = $this->delete_post( $blob ); | |
| 80 | +            if ($this->importable_raw_file($blob)) { | |
| 81 | + $result = $this->import_raw_file($blob, $is_remove); | |
| 82 | +            } elseif ($this->importable_post($blob)) { | |
| 83 | +                if ($is_remove) { | |
| 84 | + $result = $this->delete_post($blob); | |
| 85 | 85 |                  } else { | 
| 86 | - $result = $this->import_post( $blob, $force ); | |
| 86 | + $result = $this->import_post($blob, $force); | |
| 87 | 87 | } | 
| 88 | 88 | } | 
| 89 | 89 | |
| 90 | -            if ( is_wp_error( $result ) ) { | |
| 90 | +            if (is_wp_error($result)) { | |
| 91 | 91 | /* @var WP_Error $result */ | 
| 92 | - $error = wogh_append_error( $error, $result ); | |
| 92 | + $error = wogh_append_error($error, $result); | |
| 93 | 93 | } | 
| 94 | 94 | } | 
| 95 | 95 | |
| @@ -102,24 +102,24 @@ discard block | ||
| 102 | 102 | * @param boolean $force | 
| 103 | 103 | * @return string|WP_Error | 
| 104 | 104 | */ | 
| 105 | -    public function master( $force = false ) { | |
| 105 | +    public function master($force = false) { | |
| 106 | 106 | $result = $this->app->api()->fetch()->tree_recursive(); | 
| 107 | 107 | |
| 108 | -        if ( is_wp_error( $result ) ) { | |
| 108 | +        if (is_wp_error($result)) { | |
| 109 | 109 | /* @var WP_Error $result */ | 
| 110 | 110 | return $result; | 
| 111 | 111 | } | 
| 112 | 112 | |
| 113 | -        if ( is_array( $result ) ) { | |
| 114 | - $result = $this->import_files( $result, $force ); | |
| 113 | +        if (is_array($result)) { | |
| 114 | + $result = $this->import_files($result, $force); | |
| 115 | 115 | } | 
| 116 | 116 | |
| 117 | -        if ( is_wp_error( $result ) ) { | |
| 117 | +        if (is_wp_error($result)) { | |
| 118 | 118 | /* @var WP_Error $result */ | 
| 119 | 119 | return $result; | 
| 120 | 120 | } | 
| 121 | 121 | |
| 122 | - return __( 'Payload processed', 'writing-on-github' ); | |
| 122 | +        return __('Payload processed', 'writing-on-github'); | |
| 123 | 123 | } | 
| 124 | 124 | |
| 125 | 125 | /** | 
| @@ -129,14 +129,14 @@ discard block | ||
| 129 | 129 | * | 
| 130 | 130 | * @return bool | 
| 131 | 131 | */ | 
| 132 | -    protected function importable_file( Writing_On_GitHub_File_Info $file ) { | |
| 132 | +    protected function importable_file(Writing_On_GitHub_File_Info $file) { | |
| 133 | 133 | |
| 134 | 134 | $path = $file->path; | 
| 135 | 135 | |
| 136 | 136 | // only _pages, _posts and images | 
| 137 | - $prefixs = array( '_pages/', '_posts/', 'images/'); | |
| 137 | +        $prefixs = array('_pages/', '_posts/', 'images/'); | |
| 138 | 138 |          foreach ($prefixs as $prefix) { | 
| 139 | -            if ( ! strncasecmp($path, $prefix, strlen( $prefix ) ) ) { | |
| 139 | +            if ( ! strncasecmp($path, $prefix, strlen($prefix))) { | |
| 140 | 140 | return true; | 
| 141 | 141 | } | 
| 142 | 142 | } | 
| @@ -150,7 +150,7 @@ discard block | ||
| 150 | 150 | * | 
| 151 | 151 | * @return bool | 
| 152 | 152 | */ | 
| 153 | -    protected function importable_post( Writing_On_GitHub_Blob $blob ) { | |
| 153 | +    protected function importable_post(Writing_On_GitHub_Blob $blob) { | |
| 154 | 154 | // global $wpdb; | 
| 155 | 155 | |
| 156 | 156 | // // Skip the repo's readme. | 
| @@ -163,7 +163,7 @@ discard block | ||
| 163 | 163 | // return false; | 
| 164 | 164 | // } | 
| 165 | 165 | |
| 166 | -        if ( ! $blob->has_frontmatter() ) { | |
| 166 | +        if ( ! $blob->has_frontmatter()) { | |
| 167 | 167 | return false; | 
| 168 | 168 | } | 
| 169 | 169 | |
| @@ -175,13 +175,13 @@ discard block | ||
| 175 | 175 | * @param Writing_On_GitHub_Blob $blob | 
| 176 | 176 | * @return WP_Error|bool | 
| 177 | 177 | */ | 
| 178 | -    protected function delete_post( Writing_On_GitHub_Blob $blob ) { | |
| 178 | +    protected function delete_post(Writing_On_GitHub_Blob $blob) { | |
| 179 | 179 | $id = $blob->id(); | 
| 180 | -        if ( empty( $id ) ) { | |
| 180 | +        if (empty($id)) { | |
| 181 | 181 | return false; | 
| 182 | 182 | } | 
| 183 | - $result = $this->app->database()->delete_post( $id ); | |
| 184 | -        if ( is_wp_error( $result ) ) { | |
| 183 | + $result = $this->app->database()->delete_post($id); | |
| 184 | +        if (is_wp_error($result)) { | |
| 185 | 185 | /* @var WP_Error $result */ | 
| 186 | 186 | return $result; | 
| 187 | 187 | } | 
| @@ -194,31 +194,31 @@ discard block | ||
| 194 | 194 | * @param boolean $force | 
| 195 | 195 | * @return WP_Error|bool | 
| 196 | 196 | */ | 
| 197 | -    protected function import_post( Writing_On_GitHub_Blob $blob, $force = false ) { | |
| 198 | - $post = $this->blob_to_post( $blob, $force ); | |
| 197 | +    protected function import_post(Writing_On_GitHub_Blob $blob, $force = false) { | |
| 198 | + $post = $this->blob_to_post($blob, $force); | |
| 199 | 199 | |
| 200 | -        if ( ! $post instanceof Writing_On_GitHub_Post ) { | |
| 200 | +        if ( ! $post instanceof Writing_On_GitHub_Post) { | |
| 201 | 201 | return false; | 
| 202 | 202 | } | 
| 203 | 203 | |
| 204 | - $result = $this->app->database()->save_post( $post ); | |
| 205 | -        if ( is_wp_error( $result ) ) { | |
| 204 | + $result = $this->app->database()->save_post($post); | |
| 205 | +        if (is_wp_error($result)) { | |
| 206 | 206 | /** @var WP_Error $result */ | 
| 207 | 207 | return $result; | 
| 208 | 208 | } | 
| 209 | 209 | |
| 210 | - if ( $post->is_new() || | |
| 211 | -                ! wogh_equal_front_matter( $post, $blob ) ) { | |
| 210 | + if ($post->is_new() || | |
| 211 | +                ! wogh_equal_front_matter($post, $blob)) { | |
| 212 | 212 | |
| 213 | - $result = $this->app->export()->export_post( $post ); | |
| 213 | + $result = $this->app->export()->export_post($post); | |
| 214 | 214 | |
| 215 | -            if ( is_wp_error( $result ) ) { | |
| 215 | +            if (is_wp_error($result)) { | |
| 216 | 216 | /** @var WP_Error $result */ | 
| 217 | 217 | return $result; | 
| 218 | 218 | } | 
| 219 | 219 | } | 
| 220 | 220 | |
| 221 | - clean_post_cache( $post->id() ); | |
| 221 | + clean_post_cache($post->id()); | |
| 222 | 222 | |
| 223 | 223 | return true; | 
| 224 | 224 | } | 
| @@ -228,13 +228,13 @@ discard block | ||
| 228 | 228 | * @param Writing_On_GitHub_Blob $blob | 
| 229 | 229 | * @return bool | 
| 230 | 230 | */ | 
| 231 | -    protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) { | |
| 232 | -        if ( $blob->has_frontmatter() ) { | |
| 231 | +    protected function importable_raw_file(Writing_On_GitHub_Blob $blob) { | |
| 232 | +        if ($blob->has_frontmatter()) { | |
| 233 | 233 | return false; | 
| 234 | 234 | } | 
| 235 | 235 | |
| 236 | 236 | // only images | 
| 237 | -        if ( strncasecmp($blob->path(), 'images/', strlen('images/') ) != 0) { | |
| 237 | +        if (strncasecmp($blob->path(), 'images/', strlen('images/')) != 0) { | |
| 238 | 238 | return false; | 
| 239 | 239 | } | 
| 240 | 240 | |
| @@ -246,16 +246,16 @@ discard block | ||
| 246 | 246 | * @param Writing_On_GitHub_Blob $blob | 
| 247 | 247 | * @param bool $is_remove | 
| 248 | 248 | */ | 
| 249 | -    protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) { | |
| 249 | +    protected function import_raw_file(Writing_On_GitHub_Blob $blob, $is_remove) { | |
| 250 | 250 | $arr = wp_upload_dir(); | 
| 251 | 251 | $path = $arr['basedir'] . '/writing-on-github/' . $blob->path(); | 
| 252 | -        if ( $is_remove ) { | |
| 253 | -            if ( file_exists($path) ) { | |
| 252 | +        if ($is_remove) { | |
| 253 | +            if (file_exists($path)) { | |
| 254 | 254 | unlink($path); | 
| 255 | 255 | } | 
| 256 | 256 |          } else { | 
| 257 | 257 | $dirname = dirname($path); | 
| 258 | -            if ( ! file_exists($dirname) ) { | |
| 258 | +            if ( ! file_exists($dirname)) { | |
| 259 | 259 | wp_mkdir_p($dirname); | 
| 260 | 260 | } | 
| 261 | 261 | |
| @@ -272,58 +272,58 @@ discard block | ||
| 272 | 272 | * | 
| 273 | 273 | * @return Writing_On_GitHub_Post|false | 
| 274 | 274 | */ | 
| 275 | -    protected function blob_to_post( Writing_On_GitHub_Blob $blob, $force = false ) { | |
| 276 | - $args = array( 'post_content' => $blob->content_import() ); | |
| 275 | +    protected function blob_to_post(Writing_On_GitHub_Blob $blob, $force = false) { | |
| 276 | +        $args = array('post_content' => $blob->content_import()); | |
| 277 | 277 | $meta = $blob->meta(); | 
| 278 | 278 | |
| 279 | 279 | $id = false; | 
| 280 | 280 | |
| 281 | -        if ( ! empty( $meta ) ) { | |
| 282 | -            if ( array_key_exists( 'layout', $meta ) ) { | |
| 281 | +        if ( ! empty($meta)) { | |
| 282 | +            if (array_key_exists('layout', $meta)) { | |
| 283 | 283 | $args['post_type'] = $meta['layout']; | 
| 284 | - unset( $meta['layout'] ); | |
| 284 | + unset($meta['layout']); | |
| 285 | 285 | } | 
| 286 | 286 | |
| 287 | -            if ( array_key_exists( 'published', $meta ) ) { | |
| 287 | +            if (array_key_exists('published', $meta)) { | |
| 288 | 288 | $args['post_status'] = true === $meta['published'] ? 'publish' : 'draft'; | 
| 289 | - unset( $meta['published'] ); | |
| 289 | + unset($meta['published']); | |
| 290 | 290 | } | 
| 291 | 291 | |
| 292 | -            if ( array_key_exists( 'post_title', $meta ) ) { | |
| 292 | +            if (array_key_exists('post_title', $meta)) { | |
| 293 | 293 | $args['post_title'] = $meta['post_title']; | 
| 294 | - unset( $meta['post_title'] ); | |
| 294 | + unset($meta['post_title']); | |
| 295 | 295 | } | 
| 296 | 296 | |
| 297 | -            if ( array_key_exists( 'post_name', $meta ) ) { | |
| 297 | +            if (array_key_exists('post_name', $meta)) { | |
| 298 | 298 | $args['post_name'] = $meta['post_name']; | 
| 299 | - unset( $meta['post_name'] ); | |
| 299 | + unset($meta['post_name']); | |
| 300 | 300 | } | 
| 301 | 301 | |
| 302 | -            if ( array_key_exists( 'ID', $meta ) ) { | |
| 302 | +            if (array_key_exists('ID', $meta)) { | |
| 303 | 303 | $id = $args['ID'] = $meta['ID']; | 
| 304 | 304 | $blob->set_id($id); | 
| 305 | - unset( $meta['ID'] ); | |
| 305 | + unset($meta['ID']); | |
| 306 | 306 | } | 
| 307 | 307 | } | 
| 308 | 308 | |
| 309 | 309 | $meta['_wogh_sha'] = $blob->sha(); | 
| 310 | 310 | |
| 311 | -        if ( ! $force && $id ) { | |
| 312 | - $old_sha = get_post_meta( $id, '_wogh_sha', true ); | |
| 313 | - $old_github_path = get_post_meta( $id, '_wogh_github_path', true ); | |
| 311 | +        if ( ! $force && $id) { | |
| 312 | + $old_sha = get_post_meta($id, '_wogh_sha', true); | |
| 313 | + $old_github_path = get_post_meta($id, '_wogh_github_path', true); | |
| 314 | 314 | |
| 315 | 315 | // dont save post when has same sha | 
| 316 | - if ( $old_sha && $old_sha == $meta['_wogh_sha'] && | |
| 317 | -                 $old_github_path && $old_github_path == $blob->path() ) { | |
| 316 | + if ($old_sha && $old_sha == $meta['_wogh_sha'] && | |
| 317 | +                 $old_github_path && $old_github_path == $blob->path()) { | |
| 318 | 318 | return false; | 
| 319 | 319 | } | 
| 320 | 320 | } | 
| 321 | 321 | |
| 322 | - $post = new Writing_On_GitHub_Post( $args, $this->app->api() ); | |
| 323 | - $post->set_old_github_path( $blob->path() ); | |
| 324 | - $post->set_meta( $meta ); | |
| 325 | - $post->set_blob( $blob ); | |
| 326 | - $blob->set_id( $post->id() ); | |
| 322 | + $post = new Writing_On_GitHub_Post($args, $this->app->api()); | |
| 323 | + $post->set_old_github_path($blob->path()); | |
| 324 | + $post->set_meta($meta); | |
| 325 | + $post->set_blob($blob); | |
| 326 | + $blob->set_id($post->id()); | |
| 327 | 327 | |
| 328 | 328 | return $post; | 
| 329 | 329 | } | 
| @@ -6,35 +6,35 @@ | ||
| 6 | 6 | |
| 7 | 7 | ?> | 
| 8 | 8 | <div class="wrap"> | 
| 9 | - <h2><?php esc_html_e( 'Writing On GitHub', 'writing-on-github' ); ?></h2> | |
| 9 | +	<h2><?php esc_html_e('Writing On GitHub', 'writing-on-github'); ?></h2> | |
| 10 | 10 | |
| 11 | 11 | <form method="post" action="options.php"> | 
| 12 | - <?php settings_fields( Writing_On_GitHub::$text_domain ); ?> | |
| 13 | - <?php do_settings_sections( Writing_On_GitHub::$text_domain ); ?> | |
| 12 | + <?php settings_fields(Writing_On_GitHub::$text_domain); ?> | |
| 13 | + <?php do_settings_sections(Writing_On_GitHub::$text_domain); ?> | |
| 14 | 14 | <table class="form-table"> | 
| 15 | 15 | <tr> | 
| 16 | - <th scope="row"><?php esc_html_e( 'Webhook callback', 'writing-on-github' ); ?></th> | |
| 17 | - <td><code><?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>?action=wogh_push_request</code></td> | |
| 16 | +				<th scope="row"><?php esc_html_e('Webhook callback', 'writing-on-github'); ?></th> | |
| 17 | +				<td><code><?php echo esc_url(admin_url('admin-ajax.php')); ?>?action=wogh_push_request</code></td> | |
| 18 | 18 | </tr> | 
| 19 | 19 | <tr> | 
| 20 | - <th scope="row"><?php esc_html_e( 'Export', 'writing-on-github' ); ?></th> | |
| 20 | +                <th scope="row"><?php esc_html_e('Export', 'writing-on-github'); ?></th> | |
| 21 | 21 | <td> | 
| 22 | - <p><a href="<?php echo esc_url( add_query_arg( array( 'action' => 'export' ) ) ); ?>"> | |
| 23 | - <input type="button" class="button button-secondary" value="<?php esc_html_e( 'Export to GitHub', 'writing-on-github' ); ?>" /> | |
| 22 | +                    <p><a href="<?php echo esc_url(add_query_arg(array('action' => 'export'))); ?>"> | |
| 23 | +                        <input type="button" class="button button-secondary" value="<?php esc_html_e('Export to GitHub', 'writing-on-github'); ?>" /> | |
| 24 | 24 | </a></p> | 
| 25 | - <p><a href="<?php echo esc_url( add_query_arg( array( 'action' => 'force_export' ) ) ); ?>"> | |
| 26 | - <input type="button" class="button button-secondary" value="<?php esc_html_e( 'Force export to GitHub', 'writing-on-github' ); ?>" /> | |
| 25 | +                    <p><a href="<?php echo esc_url(add_query_arg(array('action' => 'force_export'))); ?>"> | |
| 26 | +                        <input type="button" class="button button-secondary" value="<?php esc_html_e('Force export to GitHub', 'writing-on-github'); ?>" /> | |
| 27 | 27 | </a></p> | 
| 28 | 28 | </td> | 
| 29 | 29 | </tr> | 
| 30 | 30 | <tr> | 
| 31 | - <th scope="row"><?php esc_html_e( 'Import', 'writing-on-github' ); ?></th> | |
| 31 | +                <th scope="row"><?php esc_html_e('Import', 'writing-on-github'); ?></th> | |
| 32 | 32 | <td> | 
| 33 | - <p><a href="<?php echo esc_url( add_query_arg( array( 'action' => 'import' ) ) ); ?>"> | |
| 34 | - <input type="button" class="button button-secondary" value="<?php esc_html_e( 'Import from GitHub', 'writing-on-github' ); ?>" /> | |
| 33 | +                    <p><a href="<?php echo esc_url(add_query_arg(array('action' => 'import'))); ?>"> | |
| 34 | +                        <input type="button" class="button button-secondary" value="<?php esc_html_e('Import from GitHub', 'writing-on-github'); ?>" /> | |
| 35 | 35 | </a></p> | 
| 36 | - <p><a href="<?php echo esc_url( add_query_arg( array( 'action' => 'force_import' ) ) ); ?>"> | |
| 37 | - <input type="button" class="button button-secondary" value="<?php esc_html_e( 'Force Import from GitHub', 'writing-on-github' ); ?>" /> | |
| 36 | +                     <p><a href="<?php echo esc_url(add_query_arg(array('action' => 'force_import'))); ?>"> | |
| 37 | +                        <input type="button" class="button button-secondary" value="<?php esc_html_e('Force Import from GitHub', 'writing-on-github'); ?>" /> | |
| 38 | 38 | </a></p> | 
| 39 | 39 | </td> | 
| 40 | 40 | </tr> |