Skip to content

Commit

Permalink
v1.4 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
vaakash committed Dec 31, 2021
1 parent 4ec15d1 commit 87e5121
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 12 deletions.
15 changes: 15 additions & 0 deletions admin/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,21 @@ public static function general_settings(){
echo '</td>';
echo '</tr>';

echo '<tr><td colspan="2"><h4>Authentication</h4></td></tr>';

echo '<tr>';
echo '<td>Github Username</td>';
echo '<td><input type="text" name="giw_github_username" value="' . $values[ 'github_username' ] . '" />';
echo '<p class="description">Your Github username for authenticating API calls.</p></td>';
echo '</tr>';

echo '<tr>';
echo '<td>Github Access token</td>';
echo '<td><input type="text" name="giw_github_access_token" value="' . $values[ 'github_access_token' ] . '" />';
echo '<p class="description">Create an access token by following <a href="https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token#creating-a-token" target="_blank">the instructions here.</a> Select <code>repo -> public_repo</code> under scopes and generate a token.
</p></td>';
echo '</tr>';

echo '</tbody>';
echo '</table>';

Expand Down
4 changes: 4 additions & 0 deletions admin/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ table.widefat td {
margin-right: 0;
}

.form-table h4{
margin: 0;
}

/* Sidebar */
#sidebar{
flex: 1;
Expand Down
8 changes: 5 additions & 3 deletions git-it-write.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
Description: Publish markdown files present in a Github repository as posts to WordPress automatically
Author: Aakash Chakravarthy
Author URI: https://www.aakashweb.com/
Version: 1.3
Version: 1.4
*/

define( 'GIW_VERSION', '1.3' );
define( 'GIW_VERSION', '1.4' );
define( 'GIW_PATH', plugin_dir_path( __FILE__ ) ); // All have trailing slash
define( 'GIW_ADMIN_URL', trailingslashit( plugin_dir_url( __FILE__ ) . 'admin' ) );

Expand Down Expand Up @@ -51,7 +51,9 @@ public static function default_config(){

public static function default_general_settings(){
return array(
'webhook_secret' => ''
'webhook_secret' => '',
'github_username' => '',
'github_access_token' => ''
);
}

Expand Down
10 changes: 6 additions & 4 deletions includes/publisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function create_post( $post_id, $item_slug, $item_props, $parent ){
GIW_Utils::log( sprintf( '---------- Checking post [%s] under parent [%s] ----------', $post_id, $parent ) );

// If post exists, check if it has changed and proceed further
if( $post_id ){
if( $post_id && $item_props ){

$post_meta = $this->get_post_meta( $post_id );

Expand Down Expand Up @@ -231,7 +231,8 @@ public function create_posts( $repo_structure, $parent ){
continue;
}

$post_id = array_key_exists( $item_slug, $existing_posts ) ? $existing_posts[ $item_slug ][ 'id' ] : 0;
$item_slug_clean = sanitize_title( $item_slug );
$post_id = array_key_exists( $item_slug_clean, $existing_posts ) ? $existing_posts[ $item_slug_clean ][ 'id' ] : 0;

$this->create_post( $post_id, $item_slug, $item_props, $parent );

Expand All @@ -240,9 +241,10 @@ public function create_posts( $repo_structure, $parent ){
if( $item_props[ 'type' ] == 'directory' ){

$directory_post = false;
$item_slug_clean = sanitize_title( $item_slug );

if( array_key_exists( $item_slug, $existing_posts ) ){
$directory_post = $existing_posts[ $item_slug ][ 'id' ];
if( array_key_exists( $item_slug_clean, $existing_posts ) ){
$directory_post = $existing_posts[ $item_slug_clean ][ 'id' ];

$index_props = array_key_exists( 'index', $item_props[ 'items' ] ) ? $item_props[ 'items' ][ 'index' ] : false;
$this->create_post( $directory_post, $item_slug, $index_props, $parent );
Expand Down
16 changes: 13 additions & 3 deletions includes/repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,23 @@ public function __construct( $user, $repo, $branch ){

public function get( $url ){

$request = wp_remote_get( $url );
$general_settings = Git_It_Write::general_settings();

if( is_wp_error( $request ) ) {
$username = $general_settings[ 'github_username' ];
$access_token = $general_settings[ 'github_access_token' ];
$args = array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode($username . ':' . $access_token),
),
);

$response = wp_remote_get( $url, $args );

if( is_wp_error( $response ) ) {
return false;
}

$body = wp_remote_retrieve_body( $request );
$body = wp_remote_retrieve_body( $response );

return $body;

Expand Down
9 changes: 7 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Donate link: https://www.paypal.me/vaakash/
License: GPLv2 or later
Requires PHP: 5.3
Requires at least: 4.4
Tested up to: 5.8
Stable tag: 1.3
Tested up to: 5.8.2
Stable tag: 1.4

Publish markdown files present in a Github repository as posts to WordPress automatically

Expand Down Expand Up @@ -140,6 +140,11 @@ Yes, if you want to pull posts from a folder in a repository then you can specif

## Changelog

### 1.4
* Fix: Repository not found issue by adding Github authentication.
* Fix: Duplicate posts when filename has special characters.
* Fix: PHP warning when directory has no index.md file.

### 1.3
* New: Support for git branches (Thanks to https://github.com/AppalachiaInteractive for the contribution)
* New: Logs directory has been changed to the uploads directory.
Expand Down

0 comments on commit 87e5121

Please sign in to comment.