Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added enhancement to create user instantly if not exists. #413

Open
wants to merge 54 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
461e0ca
Sync helper from wpcom
May 12, 2016
581f21b
Updating readme etc for v3.2
May 12, 2016
8b98ad0
Hotfix: closing tag being caught in esc_attr
mjangda May 16, 2016
69f58e2
Bump to 3.2.1
mjangda May 16, 2016
2742fcb
Better formatting for metabox_manage_guest_author_bio(), use printf i…
rinatkhaziev May 17, 2016
1ca3ab7
Fix no moderation email if guest author has no email address
RobjS Jun 24, 2016
ebb9de5
Merge pull request #363 from RobjS/fix-comment-notification-emails
mjangda Jun 24, 2016
5db7d5f
Merge pull request #358 from rinatkhaziev/master
mjangda Jun 24, 2016
e0b8abc
Update CLI to use cached functions
jasonbahl Jul 19, 2016
af1caec
Merge pull request #367 from jasonbahl/patch-1
mjangda Jul 20, 2016
a1b3375
Rename test to be more specific
mjangda Sep 1, 2016
ba486a0
Move setup / teardown from base class to test
mjangda Sep 1, 2016
ba23fb5
Add some basic WP_Query tests
mjangda Sep 1, 2016
a7209e1
Update Travis config with newer versions
mjangda Sep 1, 2016
9228687
Call `add_coauthors` for WP_Query tests
mjangda Sep 2, 2016
54b84e7
Disable author_name+tax_query+coauthor test
mjangda Sep 2, 2016
c438333
Add more builds to matrix
mjangda Sep 2, 2016
786777a
4.6+ uses a LEFT JOIN for tax queries
mjangda Sep 2, 2016
9b6d95f
Don't bother running latest on travis twice
mjangda Sep 2, 2016
c8d7893
Merge pull request #376 from Automattic/fix/4.6
mattoperry Sep 2, 2016
e1bda76
Join term_relationships with alias
trepmal Nov 10, 2016
7372bde
Make sure to echo wp_kses()
trepmal Nov 10, 2016
5f7c779
Merge pull request #382 from trepmal/milestone/kl-373
sboisvert Nov 11, 2016
26941de
Let guest authors query args (admin-side) be filtered
trepmal Nov 16, 2016
efc193f
Include PHP 7
Nov 20, 2016
c835d7f
Run PHPCS on WP 4.6 with PHP 5.6
Nov 20, 2016
0f7ae5d
Add the sniff and also check for PHP syntax errors
Nov 20, 2016
52a08f4
Merge pull request #385 from Automattic/phpcs-ci
philipjohn Nov 20, 2016
d4a2cb6
Merge branch 'master' into milestone/kl-371
trepmal Nov 21, 2016
309ff4d
Merge pull request #387 from trepmal/milestone/kl-371
sboisvert Nov 21, 2016
6dbcd3c
Merge pull request #381 from trepmal/milestone/kl
sboisvert Nov 28, 2016
a16a135
Tested tag to 4.7
Dec 9, 2016
bd3d8b5
get the coauthors by term_order
Dec 10, 2016
fb28394
added caching
Dec 10, 2016
c9af2d7
phpDoc and spaces
Dec 10, 2016
52da0aa
Merge pull request #391 from mslinnea/fix-coauthor-ordering-bug
philipjohn Mar 11, 2017
1bcb21e
Add missing group parameter to wp_cache_delete() call. See #391.#
Mar 11, 2017
bd13386
Sync helper updates from wpcom
Mar 11, 2017
8cd994d
Prepare v3.2.2 release
Mar 11, 2017
2086458
Introduce helper function for grabbing cached list of coauthor terms.
Mar 20, 2017
4aea6b1
Use the new helper function for grabbing cached co-author terms lists.
Mar 20, 2017
0a640ad
Explicitly check for a non-empty array and tidy up the code a little.
Mar 20, 2017
8101750
Must. Remember. Semicolons.
Mar 20, 2017
d5305b5
Bust the coauthors terms order cache when the objects terms are updat…
Mar 20, 2017
d58d3ae
Cache empty arrays when wp_get_object_terms() returns a WP_Error
Mar 21, 2017
b1fe8c2
Move cap_get_coauthor_terms_for_post() logic into the CoAuthors_Plus …
Mar 21, 2017
934a261
Don't create test tables as temporary
trepmal Mar 23, 2017
fe48900
Don't cache when get_object_terms fails
mjangda Mar 28, 2017
3248dfc
Update travis config to handle phpunit + php7
mjangda Mar 28, 2017
f53d8f7
Update Travis environment matrix
mjangda Mar 28, 2017
ed38119
Merge pull request #406 from Automattic/fix/get_the_terms-ordering
philipjohn Apr 3, 2017
82444c3
Merge pull request #404 from Automattic/release/3.2.2
philipjohn Apr 3, 2017
525f211
Update version constant
philipjohn Apr 3, 2017
0f8d435
Added enhancement to create user instantly if not exists.
emgk May 23, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 60 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,70 @@
language: php

php:
- 5.3
- 5.5
- 7.1

env:
- WP_VERSION=latest
- WP_VERSION=4.1
- WP_VERSION=4.0

matrix:
matrix:
include:
- php: "5.2"
env: WP_VERSION=latest
- php: "5.2"
env: WP_VERSION=4.6
- php: "5.6"
env:
- WP_VERSION=latest
- SNIFF=1
- php: "5.6"
env: WP_VERSION=4.6
- php: "7.0"
env: WP_VERSION=latest
- php: "7.0"
env: WP_VERSION=4.6
# 7.1 / latest already included above as first build.
- php: "7.1"
env: WP_VERSION=4.6

before_script:
- bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
- bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
# PHPCS
- export PHPCS_DIR=/tmp/phpcs
- export SNIFFS_DIR=/tmp/sniffs
# Install CodeSniffer for WordPress Coding Standards checks.
- if [[ "$SNIFF" == "1" ]]; then git clone -b master --depth 1 https://github.com/squizlabs/PHP_CodeSniffer.git $PHPCS_DIR; fi
# Install WordPress Coding Standards.
- if [[ "$SNIFF" == "1" ]]; then git clone -b master --depth 1 https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git $SNIFFS_DIR; fi
# Install PHP Compatibility sniffs.
- if [[ "$SNIFF" == "1" ]]; then git clone -b master --depth 1 https://github.com/wimg/PHPCompatibility.git $SNIFFS_DIR/PHPCompatibility; fi
# Set install path for PHPCS sniffs.
# @link https://github.com/squizlabs/PHP_CodeSniffer/blob/4237c2fc98cc838730b76ee9cee316f99286a2a7/CodeSniffer.php#L1941
- if [[ "$SNIFF" == "1" ]]; then $PHPCS_DIR/scripts/phpcs --config-set installed_paths $SNIFFS_DIR; fi
# After CodeSniffer install you should refresh your path.
- if [[ "$SNIFF" == "1" ]]; then phpenv rehash; fi
# Properly handle PHPunit versions
- export PATH="$HOME/.composer/vendor/bin:$PATH"
- |
if [[ ${TRAVIS_PHP_VERSION:0:2} == "7." ]]; then
composer global require "phpunit/phpunit=5.7.*"
elif [[ ${TRAVIS_PHP_VERSION:0:3} != "5.2" ]]; then
composer global require "phpunit/phpunit=4.8.*"
fi
- phpunit --version

script:
- make lint
- make phpunit
# Search for PHP syntax errors.
- find -L . -name '*.php' -print0 | xargs -0 -n 1 -P 4 php -l
# WordPress Coding Standards.
# @link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards
# @link http://pear.php.net/package/PHP_CodeSniffer/
# -p flag: Show progress of the run.
# -s flag: Show sniff codes in all reports.
# -v flag: Print verbose output.
# -n flag: Do not print warnings. (shortcut for --warning-severity=0)
# --standard: Use WordPress as the standard.
# --extensions: Only sniff PHP files.
- if [[ "$SNIFF" == "1" ]]; then $PHPCS_DIR/scripts/phpcs -p -s -v -n . --standard="WordPress-VIP" --extensions=php; fi
# Unit tests
- phpunit
165 changes: 156 additions & 9 deletions co-authors-plus.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: Co-Authors Plus
Plugin URI: http://wordpress.org/extend/plugins/co-authors-plus/
Description: Allows multiple authors to be assigned to a post. This plugin is an extended version of the Co-Authors plugin developed by Weston Ruter.
Version: 3.1.2
Version: 3.2.2
Author: Mohammad Jangda, Daniel Bachhuber, Automattic
Copyright: 2008-2015 Shared and distributed between Mohammad Jangda, Daniel Bachhuber, Weston Ruter

Expand All @@ -24,7 +24,7 @@

*/

define( 'COAUTHORS_PLUS_VERSION', '3.1.2' );
define( 'COAUTHORS_PLUS_VERSION', '3.2.2' );

require_once( dirname( __FILE__ ) . '/template-tags.php' );
require_once( dirname( __FILE__ ) . '/deprecated.php' );
Expand Down Expand Up @@ -86,6 +86,9 @@ function __construct() {
// Action to set up author auto-suggest
add_action( 'wp_ajax_coauthors_ajax_suggest', array( $this, 'ajax_suggest' ) );

// Action to create user
add_action( 'wp_ajax_coauthors_ajax_create_user', array( $this, 'ajax_create_user' ) );

// Filter to allow coauthors to edit posts
add_filter( 'user_has_cap', array( $this, 'filter_user_has_cap' ), 10, 3 );

Expand Down Expand Up @@ -116,6 +119,10 @@ function __construct() {
// Support infinite scroll for Guest Authors on author pages
add_filter( 'infinite_scroll_js_settings', array( $this, 'filter_infinite_scroll_js_settings' ), 10, 2 );

// Delete CoAuthor Cache on Post Save & Post Delete
add_action( 'save_post', array( $this, 'clear_cache') );
add_action( 'delete_post', array( $this, 'clear_cache') );
add_action( 'set_object_terms', array( $this, 'clear_cache_on_terms_set' ), 10, 6 );
}

/**
Expand Down Expand Up @@ -375,14 +382,14 @@ public function coauthors_meta_box( $post ) {
?>
</ul>
<div class="clear"></div>
<p><?php wp_kses( __( '<strong>Note:</strong> To edit post authors, please enable javascript or use a javascript-capable browser', 'co-authors-plus' ), array( 'strong' => array() ) ); ?></p>
<p><?php echo wp_kses( __( '<strong>Note:</strong> To edit post authors, please enable javascript or use a javascript-capable browser', 'co-authors-plus' ), array( 'strong' => array() ) ); ?></p>
</div>
<?php
endif;
?>

<div id="coauthors-edit" class="hide-if-no-js">
<p><?php wp_kses( __( 'Click on an author to change them. Drag to change their order. Click on <strong>Remove</strong> to remove them.', 'co-authors-plus' ), array( 'strong' => array() ) ); ?></p>
<p><?php echo wp_kses( __( 'Click on an author to change them. Drag to change their order. Click on <strong>Remove</strong> to remove them.', 'co-authors-plus' ), array( 'strong' => array() ) ); ?></p>
</div>

<?php wp_nonce_field( 'coauthors-edit', 'coauthors-nonce' ); ?>
Expand Down Expand Up @@ -506,7 +513,7 @@ function _action_quick_edit_custom_box( $column_name, $post_type ) {
<label class="inline-edit-group inline-edit-coauthors">
<span class="title"><?php esc_html_e( 'Authors', 'co-authors-plus' ) ?></span>
<div id="coauthors-edit" class="hide-if-no-js">
<p><?php wp_kses( __( 'Click on an author to change them. Drag to change their order. Click on <strong>Remove</strong> to remove them.', 'co-authors-plus' ), array( 'strong' => array() ) ); ?></p>
<p><?php echo wp_kses( __( 'Click on an author to change them. Drag to change their order. Click on <strong>Remove</strong> to remove them.', 'co-authors-plus' ), array( 'strong' => array() ) ); ?></p>
</div>
<?php wp_nonce_field( 'coauthors-edit', 'coauthors-nonce' ); ?>
</label>
Expand Down Expand Up @@ -611,12 +618,18 @@ function posts_join_filter( $join, $query ) {
}

// Check to see that JOIN hasn't already been added. Props michaelingp and nbaxley
$term_relationship_join = " INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)";
$term_taxonomy_join = " INNER JOIN {$wpdb->term_taxonomy} ON ( {$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id )";
$term_relationship_inner_join = " INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)";
$term_relationship_left_join = " LEFT JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)";

$term_taxonomy_join = " INNER JOIN {$wpdb->term_relationships} AS tr1 ON ({$wpdb->posts}.ID = tr1.object_id)";
$term_taxonomy_join .= " INNER JOIN {$wpdb->term_taxonomy} ON ( tr1.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id )";

if ( false === strpos( $join, trim( $term_relationship_join ) ) ) {
$join .= str_replace( 'INNER JOIN', 'LEFT JOIN', $term_relationship_join );
// 4.6+ uses a LEFT JOIN for tax queries so we need to check for both
if ( false === strpos( $join, trim( $term_relationship_inner_join ) )
&& false === strpos( $join, trim( $term_relationship_left_join ) ) ) {
$join .= $term_relationship_left_join;
}

if ( false === strpos( $join, trim( $term_taxonomy_join ) ) ) {
$join .= str_replace( 'INNER JOIN', 'LEFT JOIN', $term_taxonomy_join );
}
Expand Down Expand Up @@ -1065,6 +1078,14 @@ public function ajax_suggest() {

$authors = $this->search_authors( $search, $ignore );

if ( empty( $authors ) ) {
$author_display_name = $search;
$author_login = str_replace( " ", "-", $search );
echo __( 'New', 'co-authors-plus' ) . ' | ' . $author_login . ' | ' . $author_display_name . "\n";

die();
}

foreach ( $authors as $author ) {
echo esc_html( $author->ID . ' | ' . $author->user_login . ' | ' . $author->display_name . ' | ' . $author->user_email . ' | ' . $author->user_nicename ) . "\n";
}
Expand All @@ -1073,6 +1094,40 @@ public function ajax_suggest() {

}

/**
* Create new guest author if not exists.
*/
public function ajax_create_user() {
global $coauthors_plus;

if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'coauthors-create' ) ) {
die();
}

// Get author by user_login.
$guest_author = $coauthors_plus->guest_authors->get_guest_author_by( 'user_email', $_POST['email'], true );

if ( ! $guest_author ){
// Get author by user_login.
$guest_author = $coauthors_plus->guest_authors->get_guest_author_by( 'user_login', $_POST['login'], true );
}

// If user isn't exists.
if ( ! $guest_author ) {

$guest_author_id = $coauthors_plus->guest_authors->create( array(
'display_name' => sanitize_text_field( $_POST['name'] ),
'user_login' => sanitize_text_field( $_POST['login'] ),
'user_email' => sanitize_email( $_POST['email'] ) ,
) );

if ( $guest_author_id ) {
echo wp_json_encode( array( 'success' => true ) );
}
}
die();
}

/**
* Get matching authors based on a search value
*/
Expand Down Expand Up @@ -1253,6 +1308,21 @@ public function js_vars() {
wp_nonce_url( 'admin-ajax.php', 'coauthors-search' )
)
); ?>;
// AJAX link used for the create new guest user.
var coAuthorsPlus_ajax_create_new_user_link = <?php
echo wp_json_encode(
add_query_arg(
array(
'action' => 'coauthors_ajax_create_user',
'post_type' => rawurlencode( get_post_type() ),
),
wp_nonce_url( 'admin-ajax.php', 'coauthors-create' )
)
); ?>;
var coAuthorsPlus_vars = {
'email_prompt': "<?php echo __( 'Please enter author\'s email address: ', 'co-authors-plus' ); ?>",
'email_invalid': "<?php echo __( 'Please enter valid email address', 'co-authors-plus' ); ?>"
};
</script>
<?php
}
Expand Down Expand Up @@ -1463,6 +1533,68 @@ public function filter_jetpack_open_graph_tags( $og_tags, $image_dimensions ) {
// Send back the updated Open Graph Tags
return apply_filters( 'coauthors_open_graph_tags', $og_tags );
}

/**
* Retrieve a list of coauthor terms for a single post.
*
* Grabs a correctly ordered list of authors for a single post, appropriately
* cached because it requires `wp_get_object_terms()` to succeed.
*
* @param int $post_id ID of the post for which to retrieve authors.
* @return array Array of coauthor WP_Term objects
*/
public function get_coauthor_terms_for_post( $post_id ) {

if ( ! $post_id ) {
return array();
}

$cache_key = 'coauthors_post_' . $post_id;
$coauthor_terms = wp_cache_get( $cache_key, 'co-authors-plus' );

if ( false === $coauthor_terms ) {
$coauthor_terms = wp_get_object_terms( $post_id, $this->coauthor_taxonomy, array(
'orderby' => 'term_order',
'order' => 'ASC',
) );

// This usually happens if the taxonomy doesn't exist, which should never happen, but you never know.
if ( is_wp_error( $coauthor_terms ) ) {
return array();
}

wp_cache_set( $cache_key, $coauthor_terms, 'co-authors-plus' );
}

return $coauthor_terms;

}

/**
* Callback to clear the cache on post save and post delete.
*
* @param $post_id The Post ID.
*/
public function clear_cache( $post_id ) {
wp_cache_delete( 'coauthors_post_' . $post_id, 'co-authors-plus' );
}

/**
* Callback to clear the cache when an object's terms are changed.
*
* @param $post_id The Post ID.
*/
public function clear_cache_on_terms_set( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) {

// We only care about the coauthors taxonomy
if ( $this->coauthor_taxonomy !== $taxonomy ) {
return;
}

wp_cache_delete( 'coauthors_post_' . $object_id, 'co-authors-plus' );

}

}

global $coauthors_plus;
Expand Down Expand Up @@ -1595,6 +1727,7 @@ function cap_filter_comment_moderation_email_recipients( $recipients, $comment_i

if ( isset( $post_id ) ) {
$coauthors = get_coauthors( $post_id );
$extra_recipients = array();
foreach ( $coauthors as $user ) {
if ( ! empty( $user->user_email ) ) {
$extra_recipients[] = $user->user_email;
Expand All @@ -1605,3 +1738,17 @@ function cap_filter_comment_moderation_email_recipients( $recipients, $comment_i
}
return $recipients;
}

/**
* Retrieve a list of coauthor terms for a single post.
*
* Grabs a correctly ordered list of authors for a single post, appropriately
* cached because it requires `wp_get_object_terms()` to succeed.
*
* @param int $post_id ID of the post for which to retrieve authors.
* @return array Array of coauthor WP_Term objects
*/
function cap_get_coauthor_terms_for_post( $post_id ) {
global $coauthors_plus;
return $coauthors_plus->get_coauthor_terms_for_post( $post_id );
}
42 changes: 41 additions & 1 deletion js/co-authors-plus.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,28 @@ jQuery( document ).ready(function () {
return true;
}

/*
* Create new guest author if not exists
* @param string Author Data
* @param object The autosuggest input box
*/
function coauthors_new_author_display( authordata, co ){
// Reset placeholder.
co.attr( 'value', coAuthorsPlusStrings.search_box_text )
.focus( function(){ co.val( '' ) } )
.blur( function(){ co.val( coAuthorsPlusStrings.search_box_text ) } );

// Ajax Request to create a new guest author.
jQuery.post(coAuthorsPlus_ajax_create_new_user_link, authordata ,function(res){
// If guest user created successfully.
if( JSON.parse(res).success == true ){
coauthors_add_coauthor( authordata, co );
}

});

return true;
}

/*
* Add the autosuggest box and text tag to the Co-Authors table
Expand Down Expand Up @@ -202,7 +224,25 @@ jQuery( document ).ready(function () {
author.nicename = jQuery.trim( vals[4] );

if ( author.id=='New' ) {
coauthors_new_author_display( name );

// Allow user to enter email address of user.
author.email = prompt( coAuthorsPlus_vars.email_prompt );

var email_filter = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;

if ( ! author.email || ! email_filter.test( author.email ) ) {

alert( coAuthorsPlus_vars.email_invalid );
$this.attr( 'value', coAuthorsPlusStrings.search_box_text )
.focus( function(){ $this.val( '' ) } )
.blur( function(){ $this.val( coAuthorsPlusStrings.search_box_text ) } )
;
return false;
}

// Create new guest author if not exists.
coauthors_new_author_display( author, $this );

} else {
coauthors_add_coauthor( author, $this );
// Show the delete button if we now have more than one co-author
Expand Down
Loading