Skip to content

Commit

Permalink
Release: (f622db7) Merge branch 'develop' into trunk
Browse files Browse the repository at this point in the history
  • Loading branch information
dkotter committed Jul 28, 2022
1 parent a870a61 commit 50600ad
Show file tree
Hide file tree
Showing 18 changed files with 553 additions and 126 deletions.
3 changes: 3 additions & 0 deletions assets/img/editor-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion classifai.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin URI: https://github.com/10up/classifai
* Update URI: https://classifaiplugin.com
* Description: Enhance your WordPress content with Artificial Intelligence and Machine Learning services.
* Version: 1.7.2
* Version: 1.7.3
* Author: 10up
* Author URI: https://10up.com
* License: GPLv2
Expand Down
2 changes: 1 addition & 1 deletion config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* declared here instead of a Class.
*/

$plugin_version = '1.7.2';
$plugin_version = '1.7.3';

if ( file_exists( __DIR__ . '/.commit' ) ) {
$plugin_version .= '-' . file_get_contents( __DIR__ . '/.commit' );
Expand Down
3 changes: 2 additions & 1 deletion dist/js/admin.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/js/admin.min.js.LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/*! choices.js v10.1.0 | © 2022 Josh Johnson | https://github.com/jshjohnson/Choices#readme */
3 changes: 2 additions & 1 deletion dist/js/editor-ocr.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/js/editor-ocr.min.js.LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */
2 changes: 1 addition & 1 deletion dist/js/editor.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/js/gutenberg-plugin.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/js/media.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

101 changes: 95 additions & 6 deletions includes/Classifai/Admin/BulkActions.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Classifai\Admin;

use Classifai\Providers\Azure\ComputerVision;
use function Classifai\get_supported_post_types;

/**
Expand All @@ -21,6 +22,11 @@ public function can_register() {
*/
private $save_post_handler;

/**
* @var $computer_vision ComputerVision
*/
private $computer_vision;

/**
* Register the actions needed.
*/
Expand All @@ -32,6 +38,7 @@ public function register() {
}

$this->save_post_handler = new SavePostHandler();
$this->computer_vision = new ComputerVision( false );

foreach ( $post_types as $post_type ) {
add_filter( "bulk_actions-edit-$post_type", [ $this, 'register_bulk_actions' ] );
Expand All @@ -44,6 +51,9 @@ public function register() {
}
}

add_filter( 'bulk_actions-upload', [ $this, 'register_media_bulk_actions' ] );
add_filter( 'handle_bulk_actions-upload', [ $this, 'media_bulk_action_handler' ], 10, 3 );

add_action( 'admin_notices', [ $this, 'bulk_action_admin_notice' ] );
}

Expand All @@ -59,6 +69,30 @@ public function register_bulk_actions( $bulk_actions ) {
return $bulk_actions;
}

/**
* Register Classifai media bulk actions.
*
* @param array $bulk_actions Current bulk actions.
*
* @return array
*/
public function register_media_bulk_actions( $bulk_actions ) {
$settings = $this->computer_vision->get_settings();

if (
'no' !== $settings['enable_image_tagging'] ||
'no' !== $settings['enable_image_captions']
) {
$bulk_actions['scan_image'] = __( 'Scan Image', 'classifai' );
}

if ( isset( $settings['enable_smart_cropping'] ) && '1' === $settings['enable_smart_cropping'] ) {
$bulk_actions['smart_crop'] = __( 'Smart Crop', 'classifai' );
}

return $bulk_actions;
}

/**
* Handle bulk actions.
*
Expand All @@ -76,29 +110,84 @@ public function bulk_action_handler( $redirect_to, $doaction, $post_ids ) {
foreach ( $post_ids as $post_id ) {
$this->save_post_handler->classify( $post_id );
}
$redirect_to = remove_query_arg( [ 'bulk_classified', 'bulk_scanned', 'bulk_cropped' ], $redirect_to );
$redirect_to = add_query_arg( 'bulk_classified', count( $post_ids ), $redirect_to );
return $redirect_to;
}

/**
* Display an admin notice after classifying posts in bulk.
* Handle media bulk actions.
*
* @param string $redirect_to Redirect URL after bulk actions.
* @param string $doaction Action ID.
* @param array $attachment_ids Attachment ids to apply bulk actions to.
*
* @return string
*/
public function media_bulk_action_handler( $redirect_to, $doaction, $attachment_ids ) {
if (
empty( $attachment_ids ) ||
! in_array( $doaction, [ 'scan_image', 'smart_crop' ], true )
) {
return $redirect_to;
}

foreach ( $attachment_ids as $attachment_id ) {
$current_meta = wp_get_attachment_metadata( $attachment_id );

if ( 'smart_crop' === $doaction ) {
$this->computer_vision->smart_crop_image( $current_meta, $attachment_id );
} else {
$this->computer_vision->generate_image_alt_tags( $current_meta, $attachment_id );
}
}

$action = 'scan_image' === $doaction ? 'scanned' : 'cropped';

$redirect_to = remove_query_arg( [ 'bulk_classified', 'bulk_scanned', 'bulk_cropped' ], $redirect_to );
$redirect_to = add_query_arg( rawurlencode( "bulk_{$action}" ), count( $attachment_ids ), $redirect_to );
return esc_url_raw( $redirect_to );
}

/**
* Display an admin notice after bulk updates.
*/
public function bulk_action_admin_notice() {
if ( empty( $_REQUEST['bulk_classified'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended

$classified = ! empty( $_GET['bulk_classified'] ) ? intval( wp_unslash( $_GET['bulk_classified'] ) ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$scanned = ! empty( $_GET['bulk_scanned'] ) ? intval( wp_unslash( $_GET['bulk_scanned'] ) ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$cropped = ! empty( $_GET['bulk_cropped'] ) ? intval( wp_unslash( $_GET['bulk_cropped'] ) ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

if ( ! $classified && ! $scanned && ! $cropped ) {
return;
}

$classified_posts_count = intval( $_REQUEST['bulk_classified'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( $classified ) {
$classified_posts_count = $classified;
$post_type = 'post';
$action = __( 'Classified', 'classifai' );
} elseif ( $scanned ) {
$classified_posts_count = $scanned;
$post_type = 'image';
$action = __( 'Scanned', 'classifai' );
} elseif ( $cropped ) {
$classified_posts_count = $cropped;
$post_type = 'image';
$action = __( 'Cropped', 'classifai' );
}

$output = '<div id="message" class="notice notice-success is-dismissible fade"><p>';
$output .= sprintf(
/* translators: %1$s: action, %2$s: number of posts, %3$s: post type*/
_n(
'Classified %s post.',
'Classified %s posts.',
'%1$s %2$s %3$s.',
'%1$s %2$s %3$ss.',
$classified_posts_count,
'classifai'
),
$classified_posts_count
$action,
$classified_posts_count,
$post_type
);
$output .= '</p></div>';
echo wp_kses(
Expand Down
Loading

0 comments on commit 50600ad

Please sign in to comment.