Skip to content

Commit

Permalink
Merge pull request #1 from x-team/master
Browse files Browse the repository at this point in the history
Update from remote
  • Loading branch information
lukecarbis committed Mar 6, 2014
2 parents 5fb1617 + 949511c commit c0c3b01
Show file tree
Hide file tree
Showing 12 changed files with 293 additions and 34 deletions.
62 changes: 53 additions & 9 deletions connectors/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,49 @@ public static function get_action_labels() {
/**
* Return translated context labels
*
* Based on extension types used by wp_ext2type() in wp-includes/functions.php.
*
* @return array Context label translations
*/
public static function get_context_labels() {
return array(
'media' => __( 'Media', 'stream' ),
'image' => __( 'Image', 'stream' ),
'audio' => __( 'Audio', 'stream' ),
'video' => __( 'Video', 'stream' ),
'document' => __( 'Document', 'stream' ),
'spreadsheet' => __( 'Spreadsheet', 'stream' ),
'interactive' => __( 'Interactive', 'stream' ),
'text' => __( 'Text', 'stream' ),
'archive' => __( 'Archive', 'stream' ),
'code' => __( 'Code', 'stream' ),
);
}

/**
* Return the file type for an attachment which corresponds with a context label
*
* @param object $file_uri URI of the attachment
* @return string A file type which corresponds with a context label
*/
public static function get_attachment_type( $file_uri ) {

$extension = pathinfo( $file_uri, PATHINFO_EXTENSION );
$extension_type = wp_ext2type( $extension );

if ( empty( $extension_type ) ) {
$extension_type = 'document';
}

$context_labels = self::get_context_labels();

if ( ! isset( $context_labels[ $extension_type ] ) ) {
$extension_type = 'document';
}

return $extension_type;

}

/**
* Add action links to Stream drop row in admin list screen
*
Expand Down Expand Up @@ -92,16 +127,18 @@ public static function callback_add_attachment( $post_id ) {
} else {
$message = __( 'Added "%s" to Media library', 'stream' );
}
$name = $post->post_title;
$url = $post->guid;
$parent_id = $post->post_parent;
if ( $parent_id && $parent = get_post( $post->post_parent ) ) $parent_title = $parent->post_title;
$name = $post->post_title;
$url = $post->guid;
$parent_id = $post->post_parent;
$parent_title = $parent_id ? get_the_title‎( $parent_id ) : null;

$attachment_type = self::get_attachment_type( $post->guid );

self::log(
$message,
compact( 'name', 'parent_title', 'parent_id', 'url' ),
$post_id,
array( 'media' => $post->post_parent ? 'attached' : 'uploaded' )
array( $attachment_type => $post->post_parent ? 'attached' : 'uploaded' )
);
}

Expand All @@ -115,11 +152,13 @@ public static function callback_edit_attachment( $post_id ) {
$message = __( 'Updated "%s"', 'stream' );
$name = $post->post_title;

$attachment_type = self::get_attachment_type( $post->guid );

self::log(
$message,
compact( 'name' ),
$post_id,
array( 'media' => 'updated' )
array( $attachment_type => 'updated' )
);
}

Expand All @@ -136,21 +175,26 @@ public static function callback_delete_attachment( $post_id ) {
$name = $post->post_title;
$url = $post->guid;

$attachment_type = self::get_attachment_type( $post->guid );

self::log(
$message,
compact( 'name', 'parent_id', 'url' ),
$post_id,
array( 'media' => 'deleted' )
array( $attachment_type => 'deleted' )
);
}

public static function callback_wp_save_image_editor_file( $dummy, $filename, $image, $mime_type, $post_id ) {
$name = basename( $filename );

$attachment_type = self::get_attachment_type( $post->guid );

self::log(
__( 'Edited image "%s"', 'stream' ),
compact( 'name', 'filename', 'post_id' ),
$post_id,
array( 'media' => 'edited' )
array( $attachment_type => 'edited' )
);
}

Expand Down
143 changes: 128 additions & 15 deletions includes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public static function load() {
// Load Dashboard widget
add_action( 'wp_dashboard_setup', array( __CLASS__, 'dashboard_stream_activity' ) );

// Dashboard AJAX pagination
add_action( 'wp_ajax_stream_activity_dashboard_update', array( __CLASS__, 'dashboard_stream_activity_update_contents' ) );

// Heartbeat live update
add_filter( 'heartbeat_received', array( __CLASS__, 'live_update' ), 10, 2 );

Expand Down Expand Up @@ -143,6 +146,11 @@ public static function admin_enqueue_scripts( $hook ) {

wp_enqueue_style( 'wp-stream-admin', WP_STREAM_URL . 'ui/admin.css', array() );

if ( ! in_array( $hook, self::$screen_id ) && 'dashboard.php' !== $hook ) {
wp_enqueue_script( 'wp-stream-admin-dashboard', WP_STREAM_URL . 'ui/dashboard.js', array( 'jquery' ) );
return;
}

if ( ! in_array( $hook, self::$screen_id ) && 'plugins.php' !== $hook ) {
return;
}
Expand Down Expand Up @@ -503,20 +511,38 @@ public static function dashboard_stream_activity() {

wp_add_dashboard_widget(
'dashboard_stream_activity',
__( 'Stream Activity', 'stream' ),
array( __CLASS__, 'dashboard_stream_activity_contents' ),
esc_html__( 'Stream Activity', 'stream' ),
array( __CLASS__, 'dashboard_stream_activity_initial_contents' ),
array( __CLASS__, 'dashboard_stream_activity_options' )
);
}

public static function dashboard_get_total_found_rows(){
global $wpdb;
return $wpdb->get_var( 'SELECT FOUND_ROWS()' );
}

public static function dashboard_stream_activity_initial_contents(){
self::dashboard_stream_activity_contents();
}

public static function dashboard_stream_activity_update_contents(){

$paged = ! empty( $_POST['stream-paged'] ) ? absint( $_POST['stream-paged'] ) : 1;
self::dashboard_stream_activity_contents( $paged );
die;
}

/**
* Contents of the Stream Activity dashboard widget
*/
public static function dashboard_stream_activity_contents() {
$options = get_option( 'dashboard_stream_activity_options', array() );
public static function dashboard_stream_activity_contents( $paged = 1 ) {

$args = array(
'records_per_page' => isset( $options['records_per_page'] ) ? absint( $options['records_per_page'] ) : 5,
$options = get_option( 'dashboard_stream_activity_options', array() );
$records_per_page = isset( $options['records_per_page'] ) ? absint( $options['records_per_page'] ) : 5;
$args = array(
'records_per_page' => $records_per_page,
'paged' => $paged,
);
$records = stream_query( $args );

Expand Down Expand Up @@ -581,12 +607,99 @@ public static function dashboard_stream_activity_contents() {

echo '</ul>';

echo sprintf(
'<div class="sub-links"><a href="%s" title="%s">%s</a></div>',
$total_items = self::dashboard_get_total_found_rows();
$args = array(
'total_pages' => ceil( $total_items / $records_per_page ),
'current' => $paged,
);

self::dashboard_pagination( $args );
}

/*
* Display pagination links for Dashboard Widget
* Copied from private class WP_List_Table::pagination()
*/
public static function dashboard_pagination( $args = array() ){

$args = wp_parse_args(
$args,
array(
'current' => 1,
'total_pages' => 1,
)
);
extract( $args );

$records_link = add_query_arg(
array( 'page' => self::RECORDS_PAGE_SLUG ),
admin_url( self::ADMIN_PARENT_PAGE )
);

$html_view_all = sprintf(
'<a class="%s" title="%s" href="%s">%s</a>',
'view-all',
esc_attr__( 'View all records', 'stream' ),
esc_url( $records_link ),
esc_attr__( 'View all Stream Records', 'stream' ),
esc_html__( 'More', 'stream' )
); // xss ok
esc_html__( 'View All', 'stream' )
);

$page_links = array();
$disable_first = $disable_last = '';
if ( 1 === $current ){
$disable_first = ' disabled';
}
if ( $current === $total_pages ){
$disable_last = ' disabled';
}

$page_links[] = sprintf(
'<a class="%s" title="%s" href="%s" data-page="1">%s</a>',
'first-page' . $disable_first,
esc_attr__( 'Go to the first page', 'stream' ),
esc_url( remove_query_arg( 'paged', $records_link ) ),
'&laquo;'
);

$page_links[] = sprintf(
'<a class="%s" title="%s" href="%s" data-page="%s">%s</a>',
'prev-page' . $disable_first,
esc_attr__( 'Go to the previous page', 'stream' ),
esc_url( add_query_arg( 'paged', max( 1, $current - 1 ), $records_link ) ),
max( 1, $current - 1 ),
'&lsaquo;'
);

$html_total_pages = sprintf( '<span class="total-pages">%s</span>', number_format_i18n( $total_pages ) );
$page_links[] = '<span class="paging-input">' . sprintf( _x( '%1$s of %2$s', 'paging', 'stream' ), $current, $html_total_pages ) . '</span>';

$page_links[] = sprintf(
'<a class="%s" title="%s" href="%s" data-page="%s">%s</a>',
'next-page' . $disable_last,
esc_attr__( 'Go to the next page', 'stream' ),
esc_url( add_query_arg( 'paged', min( $total_pages, $current + 1 ), $records_link ) ),
min( $total_pages, $current + 1 ),
'&rsaquo;'
);

$page_links[] = sprintf(
'<a class="%s" title="%s" href="%s" data-page="%s">%s</a>',
'last-page' . $disable_last,
esc_attr__( 'Go to the last page', 'stream' ),
esc_url( add_query_arg( 'paged', $total_pages, $records_link ) ),
$total_pages,
'&raquo;'
);

$html_pagination_links = '
<div class="tablenav">
<div class="tablenav-pages">
<span class="pagination-links">' . join( "\n", $page_links ) . '</span>
</div>
<div class="clear"></div>
</div>';

echo '<div>' . $html_view_all . $html_pagination_links . '</div>';
}

/**
Expand All @@ -595,7 +708,7 @@ public static function dashboard_stream_activity_contents() {
public static function dashboard_stream_activity_options() {
$options = get_option( 'dashboard_stream_activity_options', array() );

if ( 'POST' == $_SERVER['REQUEST_METHOD'] && isset( $_POST['dashboard_stream_activity_options'] ) ) {
if ( 'POST' === $_SERVER['REQUEST_METHOD'] && isset( $_POST['dashboard_stream_activity_options'] ) ) {
$options['records_per_page'] = absint( $_POST['dashboard_stream_activity_options']['records_per_page'] );
update_option( 'dashboard_stream_activity_options', $options );
}
Expand All @@ -607,8 +720,8 @@ public static function dashboard_stream_activity_options() {
?>
<div id="dashboard-stream-activity-options">
<p>
<label for="dashboard_stream_activity_options[records_per_page]"><?php esc_html_e( 'Number of Records', 'stream' ) ?></label>
<input type="number" min="1" maxlength="3" class="small-text" name="dashboard_stream_activity_options[records_per_page]" id="dashboard_stream_activity_options[records_per_page]" value="<?php echo absint( $options['records_per_page'] ) ?>">
<input type="number" step="1" min="1" max="999" class="screen-per-page" name="dashboard_stream_activity_options[records_per_page]" id="dashboard_stream_activity_options[records_per_page]" value="<?php echo absint( $options['records_per_page'] ) ?>">
<label for="dashboard_stream_activity_options[records_per_page]"><?php esc_html_e( 'Records per page', 'stream' ) ?></label>
</p>
</div>
<?php
Expand All @@ -632,7 +745,7 @@ public static function live_update( $response, $data ) {
$enable_update = get_user_meta( get_current_user_id(), 'stream_live_update_records', true );
$enable_update = isset( $enable_update ) ? $enable_update : '';

if ( isset( $data['wp-stream-heartbeat'] ) && 'live-update' === $data['wp-stream-heartbeat'] && $enable_update == 'on' ) {
if ( isset( $data['wp-stream-heartbeat'] ) && 'live-update' === $data['wp-stream-heartbeat'] && 'on' === $enable_update ) {
// Register list table
require_once WP_STREAM_INC_DIR . 'list-table.php';
self::$list_table = new WP_Stream_List_Table( array( 'screen' => self::RECORDS_PAGE_SLUG ) );
Expand Down
6 changes: 3 additions & 3 deletions includes/feeds.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ public static function user_feed_key( $user ) {
?>
<table class="form-table">
<tr>
<th><label for="stream_feed_url"><?php esc_html_e( 'Stream Feeds Key', 'stream' ) ?></label></th>
<th><label for="<?php echo esc_attr( self::USER_FEED_KEY ) ?>"><?php esc_html_e( 'Stream Feeds Key', 'stream' ) ?></label></th>
<td>
<p>
<code><?php echo esc_html( $key ) ?></code>
<small><a href="<?php echo esc_url( add_query_arg( array( self::GENERATE_KEY_QUERY_VAR => true, 'wp_stream_nonce' => $nonce ) ) ) ?>"><?php esc_html_e( 'Generate new key', 'stream' ) ?></a></small>
<input type="text" name="<?php echo esc_attr( self::USER_FEED_KEY ) ?>" id="<?php echo esc_attr( self::USER_FEED_KEY ) ?>" class="regular-text code" value="<?php echo esc_attr( $key ) ?>" readonly>
<small><a href="<?php echo esc_url( add_query_arg( array( self::GENERATE_KEY_QUERY_VAR => true, 'wp_stream_nonce' => $nonce ) ) . sprintf( '#wp-stream-highlight:%s', self::USER_FEED_KEY ) ) ?>"><?php esc_html_e( 'Generate new key', 'stream' ) ?></a></small>
</p>
<p class="description"><?php esc_html_e( 'This is your private key used for accessing feeds of Stream Records securely. You can change your key at any time by generating a new one using the link above.', 'stream' ) ?></p>
<p>
Expand Down
41 changes: 41 additions & 0 deletions includes/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,47 @@ public static function update( $db_version, $current ) {
}
}
}

// If version is lower than 1.2.8, do the update routine
// Change the context for Media connectors to the attachment type
if ( version_compare( $db_version, '1.2.8', '<' ) ) {

require_once( WP_STREAM_CLASS_DIR . 'connector.php' );
require_once( WP_STREAM_DIR . 'connectors/media.php' );

$sql = "SELECT r.ID id, r.object_id pid, c.meta_id mid
FROM $wpdb->stream r
JOIN $wpdb->streamcontext c
ON r.ID = c.record_id AND c.connector = 'media' AND c.context = 'media'
";
$media_records = $wpdb->get_results( $sql ); // db call okay

foreach ( $media_records as $record ) {

$post = get_post( $record->pid );

if ( empty( $post ) ) {
$sql = "SELECT meta_value
FROM $wpdb->streammeta
WHERE meta_key = 'url' AND meta_id = %d
";
$url = $wpdb->get_var( $wpdb->prepare( $sql, $record->mid ) );
} else {
$url = $post->guid;
}

if ( ! empty( $url ) ) {
$context = WP_Stream_Connector_Media::get_attachment_type( $url );
$wpdb->update(
$wpdb->streamcontext,
array( 'context' => $context ),
array( 'record_id' => $record->id ),
array( '%s' ),
array( '%d' )
);
}
}
}
}

}
4 changes: 2 additions & 2 deletions includes/list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,9 @@ function filter_date() {
<label class="screen-reader-text" for="date_to">%3$s:</label>
<input type="text" name="date_to" id="date_to" class="date-picker" placeholder="%3$s" size="14" value="%4$s" />
</div>',
esc_attr__( 'Date start', 'stream' ),
esc_attr__( 'Start date', 'stream' ),
isset( $_GET['date_from'] ) ? esc_attr( $_GET['date_from'] ) : null,
esc_attr__( 'Date end', 'stream' ),
esc_attr__( 'End date', 'stream' ),
isset( $_GET['date_to'] ) ? esc_attr( $_GET['date_to'] ) : null
);

Expand Down
Loading

0 comments on commit c0c3b01

Please sign in to comment.