Skip to content

Commit

Permalink
First pass at WA Fronted integration.
Browse files Browse the repository at this point in the history
Comment auto-repositioning code adjusted to work with both WA Fronted
and FEE.

See #41.
  • Loading branch information
r-a-y committed Oct 25, 2015
1 parent aeab1ea commit 39a83fc
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 46 deletions.
22 changes: 20 additions & 2 deletions assets/js/changes.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,17 @@ jQuery(document).ready( function($) {

me.data = [];

var data, container;

// What editor are we using?
if ( typeof wa_fronted !== 'undefined' ) {
container = '.entry-content';
} else {
container = '.fee-content-original';
}

// get current
$('.fee-content-original').find( '[data-incom]' ).each( function( i, element ) {
$( container ).find( '[data-incom]' ).each( function( i, element ) {

// construct default data
var data = {
Expand All @@ -83,8 +92,17 @@ jQuery(document).ready( function($) {

me.data = [];

var data, container;

// What editor are we using?
if ( typeof wa_fronted !== 'undefined' ) {
container = '.entry-content.wa-fronted-editor';
} else {
container = '.fee-content-body';
}

// get current
$('.fee-content-body').find( '[data-incom]' ).each( function( i, element ) {
$( container ).find( '[data-incom]' ).each( function( i, element ) {

// construct default data
var data = {
Expand Down
87 changes: 51 additions & 36 deletions assets/js/hooks-wp-fee.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ jQuery(document).ready( function($) {


var me = this, // prevent reference collisions
active = false;
active = false,
container;

if ( typeof wa_fronted !== 'undefined' ) {
container = '.entry-content';
} else {
container = '.fee-content-original';
}

/**
* Enable reassignment of comments
Expand All @@ -55,7 +62,7 @@ jQuery(document).ready( function($) {
});

// get all droppable items
droppers = $('.fee-content-original').find( '[data-incom]' );
droppers = $( container ).find( '[data-incom]' );

// make textblocks droppable
droppers.droppable({
Expand All @@ -67,20 +74,20 @@ jQuery(document).ready( function($) {

activate: function( event, ui ) {

$( '.fee-content-original [data-incom]' ).removeClass( 'suppress-highlight' );
$( container + ' [data-incom]' ).removeClass( 'suppress-highlight' );

// get existing attribute from either bubble or comment
incom_attr = ui.draggable.data( 'incomBubble' );
if ( 'undefined' === typeof incom_attr ) {
incom_attr = $(ui.draggable).closest('li.incom').attr( 'data-incom-comment' );
}

$( '.fee-content-original [data-incom="' + incom_attr + '"]' ).addClass( 'suppress-highlight' );
$( container + ' [data-incom="' + incom_attr + '"]' ).addClass( 'suppress-highlight' );

},

deactivate: function( event, ui ) {
$( '.fee-content-original [data-incom]' ).removeClass( 'suppress-highlight' );
$( container + ' [data-incom]' ).removeClass( 'suppress-highlight' );
},

// when the button is dropped
Expand Down Expand Up @@ -172,9 +179,9 @@ jQuery(document).ready( function($) {
}

// destroy droppable if present
drop = $('.fee-content-original').find( '[data-incom]' ).droppable( 'instance' );
drop = $(container).find( '[data-incom]' ).droppable( 'instance' );
if ( 'undefined' !== typeof drop ) {
$('.fee-content-original').find( '[data-incom]' ).droppable( 'destroy' );
$(container).find( '[data-incom]' ).droppable( 'destroy' );
}

};
Expand Down Expand Up @@ -258,35 +265,7 @@ jQuery(document).ready( function($) {
/**
* Hook into WP FEE initialisation.
*/
$(document).on( 'fee-editor-init', function( event ) {

// store editor in our "global" if not already done
if ( $.isEmptyObject( SocialPaper.editor ) ) {
SocialPaper.editor = tinyMCE.get( window.wpActiveEditor );
}

// Add the Settings button to fee-toolbar, if necessary.
$( '.fee-toolbar' ).prepend( '<div class="fee-toolbar-left"><button class="button button-large fee-button-settings"><div class="dashicons dashicons-admin-generic"></div></button></div>' );

// Set up Settings toggle.
$sidebar = $( '.entry-sidebar' );
$settings_toggle = $( '.fee-button-settings' );
$settings_toggle.on( 'click', function( e ) {
$sidebar.toggleClass( 'toggle-on' );
$( e.target ).toggleClass( 'active' );
} );

// Set up Readers hide/show.
$readers_subsection = $( '.sidebar-section-subsection-readers' );
$( 'input[name="cacsp-paper-status"]' ).on( 'change', function() {
var self = $(this);
if ( 'public' === self.val() ) {
$readers_subsection.addClass( 'hidden' );
} else {
$readers_subsection.removeClass( 'hidden' );
}
} );
} );
$(document).on( 'fee-editor-init', cacsp_paper_on_init );

/**
* Hook into WP FEE activation
Expand Down Expand Up @@ -435,6 +414,42 @@ jQuery(document).ready( function($) {

});

/**
* WA Fronted hooks.
*/
if ( typeof wa_fronted !== 'undefined' ) {
//wa_fronted.add_action( 'on_init', cacsp_paper_on_init );
}

function cacsp_paper_on_init( event ) {

// store editor in our "global" if not already done
if ( $.isEmptyObject( SocialPaper.editor ) ) {
SocialPaper.editor = tinyMCE.get( window.wpActiveEditor );
}

// Add the Settings button to fee-toolbar, if necessary.
$( '.fee-toolbar' ).prepend( '<div class="fee-toolbar-left"><button class="button button-large fee-button-settings"><div class="dashicons dashicons-admin-generic"></div></button></div>' );

// Set up Settings toggle.
$sidebar = $( '.entry-sidebar' );
$settings_toggle = $( '.fee-button-settings' );
$settings_toggle.on( 'click', function( e ) {
$sidebar.toggleClass( 'toggle-on' );
$( e.target ).toggleClass( 'active' );
} );

// Set up Readers hide/show.
$readers_subsection = $( '.sidebar-section-subsection-readers' );
$( 'input[name="cacsp-paper-status"]' ).on( 'change', function() {
var self = $(this);
if ( 'public' === self.val() ) {
$readers_subsection.addClass( 'hidden' );
} else {
$readers_subsection.removeClass( 'hidden' );
}
} );
}
});


151 changes: 151 additions & 0 deletions includes/hooks-wa-fronted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php
/**
* WA Fronted plugin integration into Social Paper.
*
* @package Social_Paper
* @subpackage Hooks
*/

// Not sure why WA Fronted does not support older versions of PHP...
if( phpversion() < 5.43 ){
add_action( 'plugins_loaded', 'wa_fronted_init', 999 );
}

/**
* Register our post type with WA Fronted.
*
* @param array $retval Current settings.
* @return array
*/
function cacsp_wa_fronted_options( $retval = array() ){
if ( empty( get_queried_object()->ID ) ) {
return $retval;
}

return array_merge( $retval, array(
'post_types' => array(
'cacsp_paper' => array(
'editable_areas' => array(
array(
'container' => '.entry-title',
'field_type' => 'post_title',
'toolbar' => false,
'permission' => current_user_can( 'edit_paper', get_queried_object()->ID )
),
array(
'container' => '.entry-content',
'field_type' => 'post_content',
'toolbar' => 'full',
'permission' => current_user_can( 'edit_paper', get_queried_object()->ID )
)
)
)
)
) );
}
add_filter( 'wa_fronted_options', 'cacsp_wa_fronted_options' );

/**
* Add scripts so we can augment WP FEE's behaviour
*/
function cacsp_wa_fronted_enqueue_scripts() {
if ( ! cacsp_is_page() ) {
return;
}

if ( is_user_logged_in() ) {

// enqueue script
wp_enqueue_script(
'social-paper-single-fee',
Social_Paper::$URL . '/assets/js/hooks-wp-fee.js',
array( 'jquery', 'jquery-ui-droppable', 'jquery-ui-dialog' ), // load droppable and dialog as dependencies
'0.2'
);

// assume user cannot drag-n-drop
$drag_allowed = '0';

// enqueue change tracking script
wp_enqueue_script(
'social-paper-single-changes',
Social_Paper::$URL . '/assets/js/changes.js',
array( 'social-paper-single-fee' ), // make dependent on our main script above
'0.1'
);

if ( current_user_can( 'edit_paper', get_queried_object()->ID ) ) {

// user can, so override
$drag_allowed = '1';

// add style for dialog
wp_enqueue_style( 'wp-jquery-ui-dialog' );

}

// localise
wp_localize_script( 'social-paper-single-fee', 'Social_Paper_FEE', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'drag_allowed' => $drag_allowed,
'i18n' => cacsp_wa_frontend_localise(),
) );

}

}
add_action( 'wp_enqueue_scripts', 'cacsp_wa_fronted_enqueue_scripts', 999 );

/**
* Define translation strings for our Javascript
*
* @return array $translations The array of translations to pass to the script
*/
function cacsp_wa_frontend_localise() {

// add translations for comment reassignment
$translations = array(
'title' => __( 'Are you sure?', 'social-paper' ),
'body' => __( 'Are you sure you want to assign the comment and its replies to the paragraph? This action cannot be undone.', 'social-paper' ),
'submit' => __( 'Submitting...', 'social-paper' ),
'message' => __( 'Please wait while the comments are reassigned. The page will refresh when this has been done.', 'social-paper' ),
);

return $translations;

}

if ( ! function_exists( 'cacsp_filter_ajax_query_attachments' ) ) :
/**
* Filter attachments when selecting media on the frontend.
*
* By default, the "Add Media" modal shows all available attachments across
* the site. We do not want to do this due to privacy issues. Instead,
* this function filters the attachments query to only list attachments
* uploaded by the logged-in user.
*
* @param array $retval Current attachment query arguments
* @return array
*/
function cacsp_filter_ajax_query_attachments( $retval ) {
// don't do this in the admin area or if user isn't logged in
if ( defined( 'WP_NETWORK_ADMIN' ) || false === is_user_logged_in() ) {
return $retval;
}

if ( empty( $_POST['post_id'] ) ) {
return $retval;
}

// check if the post is our event type
$post = get_post( $_POST['post_id'] );
if ( 'cacsp_paper' !== $post->post_type ) {
return $retval;
}

// modify the attachments query to filter by the logged-in user
$retval['author'] = get_current_user_id();
return $retval;
}
add_filter( 'ajax_query_attachments_args', 'cacsp_filter_ajax_query_attachments' );
endif;
21 changes: 13 additions & 8 deletions social-paper.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,17 @@ public function __construct() {
return;
}

// Bail if the "Front-end Editor" plugin is not enabled
if ( ! class_exists( 'FEE' ) ) {
// Bail if a frontend editor plugin is not enabled
if ( ! class_exists( 'FEE' ) && ! class_exists( 'WA_Fronted' ) ) {
// Show admin notice
if ( current_user_can( 'install_plugins' ) ) {
$notice = sprintf(
__( 'Social Paper requires the %s plugin to be enabled. Please download it %shere%s.', 'social-paper' ),
'<strong>' . __( 'Inline Comments', 'social-paper' ). '</strong>',
'<a target="_blank" href="https://wordpress.org/plugins/inline-comments/">',
'</a>'
);
$notice = __( 'Social Paper requires a frontend editor plugin to be enabled. Please download and activate either:', 'social-paper' );
$notice .= '<br />';
$notice .= '&middot; ';
$notice .= sprintf( '<a target="_blank" href="https://wordpress.org/plugins/wp-front-end-editor/">%s</a>', __( 'Front-End Editor', 'social-paper' ) );
$notice .= '<br />';
$notice .= '&middot; ';
$notice .= sprintf( '<a target="_blank" href="https://github.com/jesperbjerke/wa-fronted/">%s</a>', __( 'WA Fronted', 'social-paper' ) );

add_action( 'admin_notices', create_function( '', "
echo '<div class=\"error\"><p>" . $notice . "</p></div>';
Expand Down Expand Up @@ -173,6 +174,10 @@ protected function includes() {
// WP FEE integration
if ( class_exists( 'FEE' ) ) {
require dirname( __FILE__ ) . '/includes/hooks-wp-fee.php';

// WA Fronted integration
} elseif ( class_exists( 'WA_Fronted' ) ) {
require dirname( __FILE__ ) . '/includes/hooks-wa-fronted.php';
}

// Inline Comments integration
Expand Down

0 comments on commit 39a83fc

Please sign in to comment.