Skip to content

Commit

Permalink
Merge pull request #139 from jonocarroll/jonocarroll-patch-1-shortcode
Browse files Browse the repository at this point in the history
added shortcodes for direct use in posts
  • Loading branch information
mAAdhaTTah committed Jun 5, 2016
2 parents 948fbfa + f4bdf52 commit e3d8432
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
6 changes: 5 additions & 1 deletion .gitignore
Expand Up @@ -3,4 +3,8 @@ vendor/
# Ignore IDE settings
.idea
build
phpunit.xml
phpunit.xml

# Ignore temp files
\#*\#
*~
25 changes: 25 additions & 0 deletions README.md
Expand Up @@ -161,6 +161,31 @@ If you'd like to include an edit link without modifying your theme directly, you
return $content;
}, 1000 );

#### Shortcodes (v >= XXXX) ####

If you wish to add either the bare URL or a link referencing the URL to an individual post, without editing themes, you can add a [shortcode](https://codex.wordpress.org/Shortcode_API) anywhere in your post;

`[wpghs]`

The following optional attributes can also be included in the shortcode
* `target=`
+ `'view'` (default) the url used will be the *view* URL (`/blob/`).
+ `'edit'` the url used will be the *edit* URL (`/edit/`).
* `type=`
+ `'link'` (default) an anchor tag (`<a>`) with href set to the requested URL will be inserted.
+ `'url'` the the bare requested URL will be inserted.
* `text=`
+ `''` (default) link text (where `type='link'`, ignored otherwise) will be set to 'View this post on GitHub'.
+ `'text'` link text (where `type='link'`, ignored otherwise) will be set to 'text' (the supplied text).

For example,

`[wpghs target='view' type='link' text='Here is my post on GitHub']` will produce a HTML anchor tag with href set to the 'view' URL of the post on GitHub, and the link text set to 'Here is my post on GitHub', i.e.

`<a href="https://github.com/USERNAME/REPO/blob/master/_posts/YOURPOST.md">Here is my post on GitHub</a>`

Any or all of the attributes can be left out; defaults will take their place.

### Additional Customizations ###

There are a number of other customizations available in WordPress <--> GitHub Sync, including the commit message and YAML front-matter. Want more detail? Check out the [wiki](https://github.com/mAAdhaTTah/wordpress-github-sync/wiki).
Expand Down
73 changes: 73 additions & 0 deletions helpers.php
Expand Up @@ -44,3 +44,76 @@ function get_the_github_edit_url() {

return $wpghs_post->github_edit_url();
}


/**
* Common WPGHS function with attributes and shortcode
* - type: 'link' (default) to return a HTML anchor tag with text, or 'url' for bare URL.
* - target: 'view' (default) or 'edit' to return the respective link/url.
* - text: text to be included in the link. Ignored if type='url'.
*
* Returns either a HTML formatted anchor tag or the bare URL of the current post on GitHub.
*
* @return string
*/
function write_wpghs_link( $atts ) {

$args = shortcode_atts(
array(
'type' => 'link',
'target' => 'view',
'text' => '',
),
$atts
);
$type = esc_attr( $args['type'] );
$target = esc_attr( $args['target'] );
$text = esc_attr( $args['text'] );

$output = '';

switch ( $target ) {
case 'view': {
$getter = get_the_github_view_url();
if ( ! empty( $text ) ) {
$linktext = $text;
} else {
$linktext = 'View this post on GitHub';
}
break;
}
case 'edit': {
$getter = get_the_github_edit_url();
if ( ! empty( $text ) ) {
$linktext = $text;
} else {
$linktext = 'Edit this post on GitHub';
}
break;
}
default: {
$getter = get_the_github_view_url();
$linktext = 'View this post on GitHub';
break;
}
}

switch ( $type ) {
case 'link': {
$output .= '<a href="' . $getter . '">' . $linktext . '</a>';
break;
}
case 'url': {
$output .= $getter;
break;
}
default: {
$output .= '<a href="' . $getter . '">' . $linktext . '</a>';
break;
}
}

return $output;

}
add_shortcode( 'wpghs', 'write_wpghs_link' );

0 comments on commit e3d8432

Please sign in to comment.