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

Issue 49: AJAX loading for user filter in list table #236

Merged
merged 16 commits into from
Feb 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 14 additions & 1 deletion classes/connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static function register() {
/**
* Callback for all registered hooks throughout Stream
* Looks for a class method with the convention: "callback_{action name}"
*
*
* @return void
*/
public static function callback() {
Expand All @@ -52,6 +52,11 @@ public static function callback() {

//For the sake of testing, trigger an action with the name of the callback
if ( defined( 'STREAM_TESTS' ) ) {
/**
* Action fires during testing to test the current callback
*
* @param array $callback Callback name
*/
do_action( 'stream_test_' . $callback[1] );
}

Expand Down Expand Up @@ -96,6 +101,14 @@ public static function is_logging_enabled_for_user( $user = null ) {
$bool = ! ( count( array_intersect( $user_roles, $roles_logged ) ) === 0 );
}

/**
* Filter sets boolean result value for this method
*
* @param bool
* @param obj $user Current user object
* @param string Current class name
* @return bool
*/
return apply_filters( 'wp_stream_record_log', $bool, $user, get_called_class() );
}

Expand Down
27 changes: 21 additions & 6 deletions connectors/posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public static function get_context_labels() {
public static function action_links( $links, $record ) {
if ( get_post( $record->object_id ) ) {
if ( $link = get_edit_post_link( $record->object_id ) ) {
$post_type = get_post_type_object( get_post_type( $record->object_id ) );
$links[ sprintf( __( 'Edit %s', 'stream' ), $post_type->labels->singular_name ) ] = $link;
$post_type_name = self::get_post_type_name( get_post_type( $record->object_id ) );
$links[ sprintf( __( 'Edit %s', 'stream' ), $post_type_name ) ] = $link;
}
if ( post_type_exists( get_post_type( $record->object_id ) ) && $link = get_permalink( $record->object_id ) ) {
$links[ __( 'View', 'stream' ) ] = $link;
Expand Down Expand Up @@ -135,13 +135,13 @@ public static function callback_transition_post_status( $new, $old, $post ) {
}
}

$post_type = get_post_type_object( $post->post_type );
$post_type_name = strtolower( self::get_post_type_name( $post->post_type ) );

self::log(
$message,
array(
'post_title' => $post->post_title,
'singular_name' => strtolower( $post_type->labels->singular_name ),
'singular_name' => $post_type_name,
'new_status' => $new,
'old_status' => $old,
'revision_id' => $revision_id,
Expand All @@ -166,13 +166,13 @@ public static function callback_deleted_post( $post_id ) {
return;
}

$post_type = get_post_type_object( $post->post_type );
$post_type_name = strtolower( self::get_post_type_name( $post->post_type ) );

self::log(
__( '"%s" %s deleted from trash', 'stream' ),
array(
'post_title' => $post->post_title,
'singular_name' => strtolower( $post_type->labels->singular_name ),
'singular_name' => $post_type_name,
),
$post->ID,
array(
Expand All @@ -181,6 +181,11 @@ public static function callback_deleted_post( $post_id ) {
);
}

/**
* Constructs list of ignored post types for the post connector
*
* @return array List of ignored post types
*/
public static function get_ignored_post_types() {
return apply_filters(
'wp_stream_post_exclude_post_types',
Expand All @@ -192,4 +197,14 @@ public static function get_ignored_post_types() {
);
}

private static function get_post_type_name( $post_type_slug ) {
if ( post_type_exists( $post_type_slug ) ) {
$post_type = get_post_type_object( $post_type_slug );
$name = $post_type->labels->singular_name;
} else {
$name = __( 'Post', 'stream' );
}
return $name;
}

}
32 changes: 20 additions & 12 deletions connectors/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ public static function get_serialized_field_label( $option_name, $field_key ) {
// to be updated
);

/**
* Filter allows for insertion of serialized labels
*
* @param array $lables Serialized labels
* @return array Updated array of serialzed labels
*/
$labels = apply_filters( 'wp_stream_serialized_labels', $labels );

if ( isset( $labels[$option_name] ) && isset( $labels[$option_name][$field_key] ) ) {
Expand Down Expand Up @@ -276,6 +282,11 @@ public static function callback_update_option_tag_base( $old_value, $value ) {
*/
public static function callback_updated_option( $option, $old_value, $value ) {
global $new_whitelist_options, $whitelist_options;

if ( 0 === strpos( $option, '_transient_' ) ) {
return;
}

$options = array_merge(
(array) $whitelist_options,
$new_whitelist_options,
Expand Down Expand Up @@ -311,18 +322,15 @@ public static function callback_updated_option( $option, $old_value, $value ) {
}
}

$changed_options = array_map(
function( $field_key ) use ( $current_key, $value, $old_value ) {
return array(
'label' => self::get_serialized_field_label( $current_key, $field_key ),
'option' => $current_key,
// Prevent fatal error when saving option as array
'old_value' => isset( $old_value[$field_key] ) ? maybe_serialize( $old_value[$field_key] ) : null,
'value' => isset( $value[$field_key] ) ? maybe_serialize( $value[$field_key] ) : null,
);
},
$changed_keys
);
foreach ( $changed_keys as $field_key ) {
$changed_options[] = array(
'label' => self::get_serialized_field_label( $current_key, $field_key ),
'option' => $current_key,
// Prevent fatal error when saving option as array
'old_value' => isset( $old_value[$field_key] ) ? maybe_serialize( $old_value[$field_key] ) : null,
'value' => isset( $value[$field_key] ) ? maybe_serialize( $value[$field_key] ) : null,
);
}
} else {
$changed_options[] = array(
'label' => self::get_field_label( $option ),
Expand Down
6 changes: 4 additions & 2 deletions connectors/taxonomies.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ public static function get_context_labels() {
*/
public static function action_links( $links, $record ) {
if ( $record->object_id && $record->action != 'deleted' && ( $term = get_term( $record->object_id, $record->context ) ) ) {
$links[ __( 'Edit', 'stream' ) ] = get_edit_term_link( $record->object_id, $record->context );
$links[ __( 'View', 'stream' ) ] = get_term_link( get_term( $record->object_id, $record->context ) );
if ( ! is_wp_error( $term ) ) {
$links[ __( 'Edit', 'stream' ) ] = get_edit_term_link( $record->object_id, $record->context );
$links[ __( 'View', 'stream' ) ] = get_term_link( get_term( $record->object_id, $record->context ) );
}
}
return $links;
}
Expand Down
7 changes: 7 additions & 0 deletions connectors/widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,13 @@ public static function get_widget_settings( $id ) {
* @return array
*/
public static function get_sidebar_widgets() {
/**
* Filter allows for insertion of sidebar widgets
*
* @param array Sidebar Widgets in Options table
* @param array Inserted Sidebar Widgets
* @return array Array of updated Sidebar Widgets
*/
return apply_filters( 'sidebars_widgets', get_option( 'sidebars_widgets', array() ) );
}

Expand Down
71 changes: 64 additions & 7 deletions includes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ class WP_Stream_Admin {
*/
public static $list_table = null;

const ADMIN_BODY_CLASS = 'wp_stream_screen';
const RECORDS_PAGE_SLUG = 'wp_stream';
const SETTINGS_PAGE_SLUG = 'wp_stream_settings';
const ADMIN_PARENT_PAGE = 'admin.php';
const VIEW_CAP = 'view_stream';
const SETTINGS_CAP = 'manage_options';
const ADMIN_BODY_CLASS = 'wp_stream_screen';
const RECORDS_PAGE_SLUG = 'wp_stream';
const SETTINGS_PAGE_SLUG = 'wp_stream_settings';
const ADMIN_PARENT_PAGE = 'admin.php';
const VIEW_CAP = 'view_stream';
const SETTINGS_CAP = 'manage_options';
const PRELOAD_AUTHORS_MAX = 50;

public static function load() {
// User and role caps
Expand Down Expand Up @@ -62,6 +63,12 @@ public static function load() {
// Enable/Disable live update per user
add_action( 'wp_ajax_stream_enable_live_update', array( __CLASS__, 'enable_live_update' ) );

// Ajax authors list
add_action( 'wp_ajax_wp_stream_filters', array( __CLASS__, 'ajax_filters' ) );

// Ajax author's name by ID
add_action( 'wp_ajax_wp_stream_get_author_name_by_id', array( __CLASS__, 'get_author_name_by_id' ) );

}

/**
Expand Down Expand Up @@ -328,7 +335,7 @@ public static function wp_ajax_reset() {
);
exit;
} else {
wp_die( "You don't have sufficient priviledges to do this action." );
wp_die( "You don't have sufficient privileges to do this action." );
}
}

Expand Down Expand Up @@ -693,4 +700,54 @@ public static function enable_live_update() {
}
}

/**
* @action wp_ajax_wp_stream_filters
*/
public static function ajax_filters() {
switch ( $_REQUEST['filter'] ) {
case 'author':
$results = array_map(
function( $user ) {
return array(
'id' => $user->id,
'text' => $user->display_name,
);
},
get_users()
);
break;
}

// `search` arg for get_users() is not enough
$results = array_filter(
$results,
function( $result ) {
return mb_strpos( mb_strtolower( $result['text'] ), mb_strtolower( $_REQUEST['q'] ) ) !== false;
}
);

$results_count = count( $results );

if ( $results_count > self::PRELOAD_AUTHORS_MAX ) {
$results = array_slice( $results, 0, self::PRELOAD_AUTHORS_MAX );
$results[] = array(
'id' => 0,
'disabled' => true,
'text' => sprintf( _n( 'One more result...', '%d more results...', $results_count - self::PRELOAD_AUTHORS_MAX, 'stream' ), $results_count - self::PRELOAD_AUTHORS_MAX ),
);
}

echo json_encode( array_values( $results ) );
die();
}

/**
* @action wp_ajax_wp_stream_get_author_name_by_id
*/
public static function get_author_name_by_id() {
$user = get_userdata( $_REQUEST['id'] );
echo json_encode( $user->display_name );
die();
}

}
8 changes: 8 additions & 0 deletions includes/connectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ public static function load() {
$classes[] = "WP_Stream_Connector_$class";
}
}

/**
* Filter allows for adding additional connectors via classes that extend
* WP_Stream_Connector
*
* @param array Connector Class names
* @return array Updated Array of Connector Class names
*/
self::$connectors = apply_filters( 'wp_stream_connectors', $classes );

foreach ( self::$connectors as $connector ) {
Expand Down
10 changes: 8 additions & 2 deletions includes/context-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ function get_sql() {
$where = array();

$queries = $this->queries;

$meta_query = new WP_Meta_Query;

// Context table is always joined
// $join[] = " INNER JOIN $context_table ON $main_table.ID = $context_table.record_id";

Expand Down Expand Up @@ -148,6 +148,12 @@ function get_sql() {

$join = implode( "\n", $join );

/**
* Filter allows modification of context sql statement
*
* @param array Array of context sql statement components
* @return string Updated context sql statement
*/
return apply_filters_ref_array( 'get_context_sql', array( compact( 'join', 'where' ), $this->queries ) );
}

Expand Down
27 changes: 24 additions & 3 deletions includes/db-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ class WP_Stream_DB {

public function __construct() {
global $wpdb;
// Allow devs to alter the tables prefix, default to base_prefix
/**
* Allows devs to alter the tables prefix, default to base_prefix
*
* @param string database prefix
* @return string udpated database prefix
*/
$prefix = apply_filters( 'wp_stream_db_tables_prefix', $wpdb->prefix );
self::$table = $prefix . 'stream';
self::$table_meta = $prefix . 'stream_meta';
Expand Down Expand Up @@ -50,6 +55,12 @@ public function get_table_names() {
public function insert( $recordarr ) {
global $wpdb;

/**
* Filter allows modification of record information
*
* @param array array of record information
* @return array udpated array of record information
*/
$recordarr = apply_filters( 'wp_stream_record_array', $recordarr );

// Allow extensions to handle the saving process
Expand All @@ -62,7 +73,7 @@ public function insert( $recordarr ) {

$data = array_filter( $data );

// TODO Check/Validate *required* fields
// TODO Check/Validate *required* fields

$result = $wpdb->insert(
self::$table,
Expand All @@ -73,6 +84,11 @@ public function insert( $recordarr ) {
$record_id = $wpdb->insert_id;
}
else {
/**
* Action Hook that fires on an error during post insertion
*
* @param int $record_id Record being inserted
*/
do_action( 'wp_stream_post_insert_error', $record_id );
return $record_id;
}
Expand All @@ -91,7 +107,12 @@ public function insert( $recordarr ) {
$this->insert_meta( $record_id, $key, $val );
}
}

/**
* Fires when A Post is inserted
*
* @param int $record_id Inserted record ID
* @param array $recordarr Array of information on this record
*/
do_action( 'wp_stream_post_inserted', $record_id, $recordarr );

return $record_id;
Expand Down
Loading