Skip to content

Commit

Permalink
Overhaul Plugin for Embedded Artistry Needs
Browse files Browse the repository at this point in the history
This commit contains a number of changs that make this plugin useful for Embedded Artistry's purposes. Changes include:

- Removing the _ directory prefix
- Supporting additional post types (courses, lessons, newsletters, field atlas entries, glossary entries), including specific file organization schemes for each type
- Set the user to the configured user in the import/export paths
    + This addresses a problem we had where the initial export would differ from the import/export from GitHub and Wordpress. By setting the same user, we resolved the problems the difference in user permissions incurred
- Modified commit messages to our preferred format
- Removed option to delete posts on import (should happen from wordpress front-end)
- Removing the ability to file new posts by adding a file in GitHub (expected workflow is to go through the website to create posts)
- Removed support for drafts and images
- Disabled the "Edit" link taking you to GitHub - we need the Wordpress front-end for our custom post types and their metadata

In addition, other cleanup was performed:

- Update version to 1.2
- Adjusted some details from litefeel->embeddedartistry
- Refactoring filename/directory logic in post.php
- Removing commented out code
- Adding error log statements for different failure conditions so that we have transparency when an import or export fails
- Ignore composer.lock file, since it's generated during the plugin setup process
- Removed function in CLI causing an error - app->export->set_user() referenced an unused option name
    + Instead, this CLI path calls the wordpress set user functionality
  • Loading branch information
phillipjohnston committed May 26, 2021
1 parent 3e27b16 commit 505fc6a
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 229 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ build

# Ignore temp files
\#*\#
composer.lock
*~
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Changelog ##

### [1.2][1.2] ###

* Adjusted output directories to match Embedded Artistry requirements
* Added support for custom Embedded Artistry post types

### [1.11][1.11] ###

* Fixed correct the url of files in the images directory
Expand Down
39 changes: 30 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
# Writing On GitHub #
# Embedded Artistry: Writing On GitHub #

[![Build Status](https://travis-ci.com/litefeel/writing-on-github.svg?branch=master)](https://travis-ci.com/litefeel/writing-on-github)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/litefeel/writing-on-github/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/litefeel/writing-on-github/?branch=master)
TODO: Update litefeel refeernces to embedded artistry
- lib/client/fetch.php
- test data
- change log
- Readme

**Contributors:** litefeel
**Tags:** github, git, version control, content, collaboration, publishing, writing
**Donate link:** https://www.paypal.me/litefeel
**Requires at least:** 3.9
**Tested up to:** 5.4
**Stable tag:** 1.11
**License:** GPLv2
**License URI:** http://www.gnu.org/licenses/gpl-2.0.html

Expand All @@ -29,6 +26,8 @@ Well, now you can! Introducing [Writing On GitHub](https://github.com/litefeel/w
1. Allows content publishers to version their content in GitHub
2. Allows readers to submit proposed improvements to WordPress-served content via GitHub's Pull Request model

Note that it does not, however, allow for deleting posts on the front-end by removing the file in the repository. This prevents production websites from being taken down by a thoughtless commit.

### Writing On GitHub might be able to do some other cool things: ###

* Allow teams to collaboratively write and edit posts using GitHub (e.g., pull requests, issues, comments)
Expand Down Expand Up @@ -75,6 +74,28 @@ The sync action is based on two hooks:
3. Create a WebHook within your repository with the provided callback URL and callback secret, using `application/json` as the content type. To set up a webhook on GitHub, head over to the **Settings** page of your repository, and click on **Webhooks & services**. After that, click on **Add webhook**.
4. Click `Export to GitHub`

## Building the Plugin for Release

You cannot clone this repository directly to the server - it needs to be prepared for release with composer, or you need to run the command below inside the git repository on the server.

Run this command from the plugin root:

```
$ composer install
```

Settings and dependencies are defined in [`composer.json`](composer.json).

## Debugging

```
$ wp wogh export all 0 --debug
```

## Running Tests

TODO

## Frequently Asked Questions ##

### Markdown Support ###
Expand Down
Empty file modified bin/install-wp-tests.sh
100644 → 100755
Empty file.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "litefeel/writing-on-github",
"description": "A WordPress plugin to allow you writing on GitHub (or Jekyll site).",
"name": "embeddedartistry/writing-on-github",
"description": "A WordPress plugin to support synchronization between Embedded Artistry Wordpress and GitHub.",
"type": "wordpress-plugin",
"minimum-stability": "stable",
"license": "GPL",
"authors": [
{
"name": "litefeel",
"email": "litefeel@gmail.com"
"name": "embeddedartistry",
"email": "contact@embeddedartistry.com"
}
],
"autoload": {
Expand Down
3 changes: 0 additions & 3 deletions lib/blob.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,6 @@ public function meta() {
if ( 'yes' == get_option('wogh_ignore_author') ) {
unset($meta['author']);
}
// if ( isset( $meta['link'] ) ) {
// $meta['link'] = str_replace( home_url(), '', $meta['link'] );
// }
}

return $meta;
Expand Down
10 changes: 9 additions & 1 deletion lib/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ public function export( $args ) {
WP_CLI::error( __( 'Invalid user ID', 'writing-on-github' ) );
}

$this->app->export()->set_user( $user_id );

if( $user_id == 0 )
{
wp_set_current_user( get_option( 'wogh_default_user' ) );
}
else
{
wp_set_current_user( $user_id );
}

if ( 'all' === $post_id ) {
WP_CLI::line( __( 'Starting full export to GitHub.', 'writing-on-github' ) );
Expand Down
12 changes: 5 additions & 7 deletions lib/client/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,19 @@ protected function call( $method, $endpoint, $body = array() ) {
$args['body'] = json_encode( $body );
}

// $tmpbody = isset( $args['body'] ) ? $args['body'] : '';
// error_log( "writing-on-github-call $method $endpoint $tmpbody" );

$response = wp_remote_request( $endpoint, $args );
$status = wp_remote_retrieve_header( $response, 'status' );
$body = json_decode( wp_remote_retrieve_body( $response ) );
$body = json_decode( wp_remote_retrieve_body( $response ) );
$status = wp_remote_retrieve_response_code($response);

if ( '2' !== substr( $status, 0, 1 ) && '3' !== substr( $status, 0, 1 ) ) {
return new WP_Error(
strtolower( str_replace( ' ', '_', $status ) ),
sprintf(
__( 'Method %s to endpoint %s failed with error: %s', 'writing-on-github' ),
__( 'Method %s to endpoint %s failed with error: %s; status: %d', 'writing-on-github' ),
$method,
$endpoint,
( $body && $body->message ) ? $body->message : 'Unknown error'
( $body && isset($body->message) ) ? $body->message : 'Unknown error',
$status
)
);
}
Expand Down
14 changes: 13 additions & 1 deletion lib/controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ public function pull_posts() {
remove_action( 'save_post', array( $this, 'export_post' ) );
remove_action( 'delete_post', array( $this, 'delete_post' ) );

// Here we set the user ID to the configured default so that when we
// import the post, it is done with the same permissions as the initial export.
// This prevents problems with things like Heapless C++ course modules
// working in a forced export, but not when we export the lesson page.
wp_set_current_user( get_option( 'wogh_default_user' ) );

$result = $this->app->import()->payload( $payload );

$this->app->semaphore()->unlock();
Expand Down Expand Up @@ -101,7 +107,7 @@ public function import_master( $user_id = 0, $force = false ) {

$this->app->semaphore()->lock();
remove_action( 'save_post', array( $this, 'export_post' ) );
remove_action( 'save_post', array( $this, 'delete_post' ) );
remove_action( 'delete_post', array( $this, 'delete_post' ) );

if ( $user_id ) {
wp_set_current_user( $user_id );
Expand Down Expand Up @@ -182,6 +188,12 @@ public function export_post( $post_id ) {
) );
}

// Here we set the user ID to the configured default so that when we
// export the post, it is done with the same permissions as the initial export.
// This prevents problems with things like Heapless C++ course modules
// working in a forced export, but not when we export the lesson page.
wp_set_current_user( get_option( 'wogh_default_user' ) );

$this->app->semaphore()->lock();
$result = $this->app->export()->update( $post_id );
$this->app->semaphore()->unlock();
Expand Down
133 changes: 22 additions & 111 deletions lib/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ class Writing_On_GitHub_Database {
*
* @var array
*/
protected $whitelisted_post_types = array( 'post', 'page' );
protected $whitelisted_post_types = array(
'post',
'page',
'glossary',
'newsletters',
'course',
'lesson',
'fieldatlas'
);

/**
* Currently whitelisted post statuses.
Expand Down Expand Up @@ -94,10 +102,11 @@ public function fetch_by_id( $post_id ) {
'unsupported_post',
sprintf(
__(
'Post ID %s is not supported by WOGH. See wiki to find out how to add support.',
'Post ID %s (name %s) is not supported at this time.',
'writing-on-github'
),
$post_id
$post_id,
get_the_title($post_id)
)
);
}
Expand All @@ -115,28 +124,20 @@ public function fetch_by_id( $post_id ) {
public function save_post( Writing_On_GitHub_Post $post ) {
$args = apply_filters( 'wogh_pre_import_args', $this->post_args( $post ), $post );

if ( $post->is_new() ) {
error_log('This plugin does not support adding posts from GitHub. Please initially create content with the Wordpress front-end.');
return false;
}

remove_filter( 'content_save_pre', 'wp_filter_post_kses' );
$post_id = $post->is_new() ?
wp_insert_post( $args, true ) :
wp_update_post( $args, true );
$post_id = wp_update_post( $args, true );
add_filter( 'content_save_pre', 'wp_filter_post_kses' );

if ( is_wp_error( $post_id ) ) {
/* @var WP_Error $post_id */
return $post_id;
}

if ( $post->is_new() ) {
$author = false;
$meta = $post->get_meta();
if ( ! empty( $meta ) && ! empty( $meta['author'] ) ) {
$author = $meta['author'];
}
$user = $this->fetch_commit_user( $author );
$user_id = is_wp_error( $user ) ? 0 : $user->ID;
$this->set_post_author( $post_id, $user_id );
}

$post->set_post( get_post( $post_id ) );

$meta = apply_filters( 'wogh_pre_import_meta', $post->get_meta(), $post );
Expand Down Expand Up @@ -226,100 +227,6 @@ private function get_post_id_by_filename( $filename, $pattern ) {
return $post_id;
}

/**
* Deletes a post from the database based on its GitHub path.
*
* @param string $path Path of Post to delete.
*
* @return string|WP_Error
*/
public function delete_post_by_path( $path ) {
$query = new WP_Query( array(
'meta_key' => '_wogh_github_path',
'meta_value' => $path,
'meta_compare' => '=',
'posts_per_page' => 1,
'fields' => 'ids',
) );

$post_id = $query->get_posts();
$post_id = array_pop( $post_id );

if ( ! $post_id ) {
$parts = explode( '/', $path );
$filename = array_pop( $parts );
$directory = $parts ? array_shift( $parts ) : '';

if ( false !== strpos( $directory, 'post' ) ) {
$post_id = get_post_id_by_filename( $filename, '/([0-9]{4})-([0-9]{2})-([0-9]{2})-(.*)\.md/' );
}

if ( ! $post_id ) {
$post_id = get_post_id_by_filename( $filename, '/(.*)\.md/' );
}
}

if ( ! $post_id ) {
return new WP_Error(
'path_not_found',
sprintf(
__( 'Post not found for path %s.', 'writing-on-github' ),
$path
)
);
}

$result = wp_delete_post( $post_id );

// If deleting fails...
if ( false === $result ) {
$post = get_post( $post_id );

// ...and the post both exists and isn't in the trash...
if ( $post && 'trash' !== $post->post_status ) {
// ... then something went wrong.
return new WP_Error(
'db_error',
sprintf(
__( 'Failed to delete post ID %d.', 'writing-on-github' ),
$post_id
)
);
}
}

return sprintf(
__( 'Successfully deleted post ID %d.', 'writing-on-github' ),
$post_id
);
}

public function delete_post( $post_id ) {
$result = wp_delete_post( $post_id );

// If deleting fails...
if ( false === $result ) {
$post = get_post( $post_id );

// ...and the post both exists and isn't in the trash...
if ( $post && 'trash' !== $post->post_status ) {
// ... then something went wrong.
return new WP_Error(
'db_error',
sprintf(
__( 'Failed to delete post ID %d.', 'writing-on-github' ),
$post_id
)
);
}
}

return sprintf(
__( 'Successfully deleted post ID %d.', 'writing-on-github' ),
$post_id
);
}

/**
* Returns the list of post type permitted.
*
Expand Down Expand Up @@ -363,19 +270,23 @@ protected function format_for_query( $whitelist ) {
*/
protected function is_post_supported( Writing_On_GitHub_Post $post ) {
if ( wp_is_post_revision( $post->id ) ) {
error_log(sprintf(__('Post ID %d is not post revision'), $post->id));
return false;
}

// We need to allow trashed posts to be queried, but they are not whitelisted for export.
if ( ! in_array( $post->status(), $this->get_whitelisted_post_statuses() ) && 'trash' !== $post->status() ) {
error_log(sprintf(__('Post ID %d has status %s, which is not whitelisted'), $post->id, $post->status()));
return false;
}

if ( ! in_array( $post->type(), $this->get_whitelisted_post_types() ) ) {
error_log(sprintf(__('Post ID %d has type %s, which is not whitelisted'), $post->id, $post->type()));
return false;
}

if ( $post->has_password() ) {
error_log(sprintf(__('Post ID %d has a password'), $post->id));
return false;
}

Expand Down
Loading

0 comments on commit 505fc6a

Please sign in to comment.