Skip to content

Commit

Permalink
Merge pull request #267 from codepress/release/4.0.2
Browse files Browse the repository at this point in the history
Release/4.0.2
  • Loading branch information
DGStefan authored Feb 26, 2020
2 parents 12a0071 + 9abf8e6 commit 341424d
Show file tree
Hide file tree
Showing 60 changed files with 41,225 additions and 1,810 deletions.
4 changes: 3 additions & 1 deletion classes/Admin/Page/Addons.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
class Addons extends Page
implements AC\Registrable {

const NAME = 'addons';

public function __construct() {
parent::__construct( 'addons', __( 'Add-ons', 'codepress-admin-columns' ) );
parent::__construct( self::NAME, __( 'Add-ons', 'codepress-admin-columns' ) );
}

/**
Expand Down
174 changes: 20 additions & 154 deletions classes/AdminColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

use AC\Admin\GeneralSectionFactory;
use AC\Admin\Page;
use AC\Admin\PromoCollection;
use AC\Admin\Section\ListScreenMenu;
use AC\Admin\Section\Restore;
use AC\Check;
use AC\Controller\AjaxColumnValue;
use AC\Controller\AjaxRequestCustomFieldKeys;
use AC\Controller\AjaxRequestNewColumn;
use AC\Controller\ListScreenRequest;
use AC\Controller\ListScreenRestoreColumns;
use AC\Controller\RedirectAddonStatus;
use AC\Deprecated;
use AC\ListScreenRepository;
use AC\ListScreenRepository\FilterStrategy;
Expand Down Expand Up @@ -58,6 +58,8 @@ private function __construct() {
$this->list_screen_repository = new ListScreenRepository\Aggregate();
$this->list_screen_repository->register_repository( new ListScreenRepository\DataBase( ListScreenTypes::instance() ) );

$this->register_admin();

$services = [
new Ajax\NumberFormat( new Request() ),
new Deprecated\Hooks,
Expand All @@ -72,7 +74,11 @@ private function __construct() {
new Capabilities\Manage(),
new AjaxRequestNewColumn( $this->list_screen_repository ),
new AjaxRequestCustomFieldKeys(),
new AjaxColumnValue( $this->list_screen_repository ),
new ListScreenRestoreColumns( $this->list_screen_repository ),
new RedirectAddonStatus( $this->admin->get_url( Page\Addons::NAME ) ),
new PluginActionLinks( $this->get_basename(), $this->admin->get_url( Page\Columns::NAME ) ),
new NoticeChecks(),
];

foreach ( $services as $service ) {
Expand All @@ -81,23 +87,12 @@ private function __construct() {
}
}

$this->register_admin();
$this->localize();

add_action( 'init', [ $this, 'register_list_screens' ], 1000 ); // run after all post types are registered
add_action( 'init', [ $this, 'install' ], 1000 );
add_action( 'init', [ $this, 'notice_checks' ] );
add_action( 'init', [ $this, 'register_global_scripts' ] );

add_filter( 'plugin_action_links', [ $this, 'add_settings_link' ], 1, 2 );
add_filter( 'plugin_action_links', [ $this, 'add_pro_link' ], 10, 2 );

add_action( 'ac/screen', [ $this, 'init_table_on_screen' ] );
add_action( 'wp_ajax_ac_get_column_value', [ $this, 'table_ajax_value' ] );

add_filter( 'wp_redirect', [ $this, 'redirect_after_status_change' ] );

// run after all post types are registered
add_action( 'init', [ $this, 'register_list_screens' ], 1000 );
}

/**
Expand Down Expand Up @@ -132,21 +127,19 @@ public function init_table_on_screen( Screen $screen ) {
$list_id = $this->preferences()->get( $key );
}

$list_screen = null;
$permission_checker = ( new PermissionChecker( wp_get_current_user() ) );

if ( $list_id ) {
$_list_screen = $this->list_screen_repository->find( $list_id );

if ( $_list_screen && $permission_checker->is_valid( $_list_screen ) ) {
$list_screen = $_list_screen;
} else {
$requested_list_screen = $this->list_screen_repository->find( $list_id );

// List screen not found.
$list_screen = $this->get_first_list_screen( $key, $permission_checker );
if ( $requested_list_screen && $permission_checker->is_valid( $requested_list_screen ) ) {
$list_screen = $requested_list_screen;
}
} else {
}

// First visit.
// First visit or not found
if ( ! $list_screen ) {
$list_screen = $this->get_first_list_screen( $key, $permission_checker );
}

Expand All @@ -161,7 +154,8 @@ public function init_table_on_screen( Screen $screen ) {
}

/**
* @param string $key
* @param string $key
* @param PermissionChecker $permission_checker
*
* @return ListScreen|null
*/
Expand All @@ -181,63 +175,6 @@ private function get_first_list_screen( $key, PermissionChecker $permission_chec
return ListScreenTypes::instance()->get_list_screen_by_key( $key );
}

/**
* Get column value by ajax.
*/
public function table_ajax_value() {
check_ajax_referer( 'ac-ajax' );

// Get ID of entry to edit
$id = intval( filter_input( INPUT_POST, 'pk' ) );

if ( ! $id ) {
wp_die( __( 'Invalid item ID.', 'codepress-admin-columns' ), null, 400 );
}

$list_screen = $this->list_screen_repository->find( filter_input( INPUT_POST, 'layout' ) );

if ( ! $list_screen ) {
wp_die( __( 'Invalid list screen.', 'codepress-admin-columns' ), null, 400 );
}

$column = $list_screen->get_column_by_name( filter_input( INPUT_POST, 'column' ) );

if ( ! $column ) {
wp_die( __( 'Invalid column.', 'codepress-admin-columns' ), null, 400 );
}

if ( ! $column instanceof Column\AjaxValue ) {
wp_die( __( 'Invalid method.', 'codepress-admin-columns' ), null, 400 );
}

// Trigger ajax callback
echo $column->get_ajax_value( $id );
exit;
}

/**
* Init checks
*/
public function notice_checks() {
$checks = [
new Check\Review(),
];

if ( ! ac_is_pro_active() ) {
foreach ( new PromoCollection() as $promo ) {
$checks[] = new Check\Promotion( $promo );
}
}

foreach ( new Integrations() as $integration ) {
$checks[] = new Check\AddonAvailable( $integration );
}

foreach ( $checks as $check ) {
$check->register();
}
}

/**
* @return string
*/
Expand All @@ -259,41 +196,6 @@ public function get_version() {
return AC_VERSION;
}

/**
* Add a settings link to the Admin Columns entry in the plugin overview screen
*
* @param array $links
* @param string $file
*
* @return array
* @see filter:plugin_action_links
* @since 1.0
*/
public function add_settings_link( $links, $file ) {
if ( $file === $this->get_basename() ) {
array_unshift( $links, sprintf( '<a href="%s">%s</a>', $this->admin->get_url( 'columns' ), __( 'Settings', 'codepress-admin-columns' ) ) );
}

return $links;
}

/**
* @param array $links
* @param string $file
*
* @return array
*/
public function add_pro_link( $links, $file ) {
if ( $file === $this->get_basename() && ! ac_is_pro_active() ) {
$links[] = sprintf( '<a href="%s" target="_blank">%s</a>',
esc_url( ac_get_site_utm_url( 'admin-columns-pro', 'upgrade' ) ),
sprintf( '<span style="font-weight: bold;">%s</span>', __( 'Go Pro', 'codepress-admin-columns' ) )
);
}

return $links;
}

/**
* @since 2.5
*/
Expand Down Expand Up @@ -389,9 +291,9 @@ public function get_post_types() {
* Load text-domain
*/
public function localize() {
$path = pathinfo( $this->get_dir() );
$relative_dir = str_replace( WP_PLUGIN_DIR, '', $this->get_dir() );

load_plugin_textdomain( 'codepress-admin-columns', false, $path['basename'] . '/languages/' );
load_plugin_textdomain( 'codepress-admin-columns', false, $relative_dir . 'languages/' );
}

/**
Expand All @@ -416,42 +318,6 @@ private function register_admin() {
->register();
}

/**
* Redirect the user to the Admin Columns add-ons page after activation/deactivation of an add-on from the add-ons page
*
* @param $location
*
* @return string
* @since 2.2
*/
public function redirect_after_status_change( $location ) {
global $pagenow;

if ( 'plugins.php' !== $pagenow || ! filter_input( INPUT_GET, 'ac-redirect' ) || filter_input( INPUT_GET, 'error' ) ) {
return $location;
}

$status = filter_input( INPUT_GET, 'action' );

if ( ! $status ) {
return $location;
}

$integration = IntegrationFactory::create( filter_input( INPUT_GET, 'plugin' ) );

if ( ! $integration ) {
return $location;
}

$location = add_query_arg( [
'status' => $status,
'plugin' => $integration->get_slug(),
'_ac_nonce' => wp_create_nonce( 'ac-plugin-status-change' ),
], $this->admin()->get_url( 'addons' ) );

return $location;
}

/**
* @param $file
*
Expand Down
8 changes: 4 additions & 4 deletions classes/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class Autoloader {
protected $prefixes;

protected function __construct() {
$this->prefixes = array();
$this->prefixes = [];

spl_autoload_register( array( $this, 'autoload' ) );
spl_autoload_register( [ $this, 'autoload' ] );
}

public static function instance() {
Expand Down Expand Up @@ -127,11 +127,11 @@ public function get_class_names_from_dir( $namespace ) {
$path = realpath( $path );

if ( ! $path ) {
return array();
return [];
}

$iterator = new FilesystemIterator( $path, FilesystemIterator::SKIP_DOTS );
$classes = array();
$classes = [];

/* @var DirectoryIterator $leaf */
foreach ( $iterator as $leaf ) {
Expand Down
5 changes: 2 additions & 3 deletions classes/Check/AddonAvailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct( Integration $integration ) {
* @throws Exception
*/
public function register() {
add_action( 'ac/screen', array( $this, 'display' ) );
add_action( 'ac/screen', [ $this, 'display' ] );

$this->get_ajax_handler()->register();
}
Expand All @@ -41,7 +41,7 @@ private function get_ajax_handler() {
$handler = new Ajax\Handler();
$handler
->set_action( 'ac_dismiss_notice_addon_' . $this->integration->get_slug() )
->set_callback( array( $this, 'ajax_dismiss_notice' ) );
->set_callback( [ $this, 'ajax_dismiss_notice' ] );

return $handler;
}
Expand Down Expand Up @@ -88,7 +88,6 @@ public function display( Screen $screen ) {
)
);


$notice = new Dismissible( $message, $this->get_ajax_handler() );
$notice->register();
}
Expand Down
65 changes: 65 additions & 0 deletions classes/Controller/AjaxColumnValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace AC\Controller;

use AC\Ajax;
use AC\Column\AjaxValue;
use AC\ListScreenRepository\ListScreenRepository;
use AC\Registrable;

class AjaxColumnValue implements Registrable {

/**
* @var ListScreenRepository
*/
private $repository;

public function __construct( ListScreenRepository $repository ) {
$this->repository = $repository;
}

public function register() {
$this->get_ajax_handler()->register();
}

private function get_ajax_handler() {
$handler = new Ajax\Handler();
$handler
->set_action( 'ac_get_column_value' )
->set_callback( [ $this, 'get_value' ] );

return $handler;
}

public function get_value() {
check_ajax_referer( 'ac-ajax' );

// Get ID of entry to edit
$id = intval( filter_input( INPUT_POST, 'pk' ) );

if ( ! $id ) {
wp_die( __( 'Invalid item ID.', 'codepress-admin-columns' ), null, 400 );
}

$list_screen = $this->repository->find( filter_input( INPUT_POST, 'layout' ) );

if ( ! $list_screen ) {
wp_die( __( 'Invalid list screen.', 'codepress-admin-columns' ), null, 400 );
}

$column = $list_screen->get_column_by_name( filter_input( INPUT_POST, 'column' ) );

if ( ! $column ) {
wp_die( __( 'Invalid column.', 'codepress-admin-columns' ), null, 400 );
}

if ( ! $column instanceof AjaxValue ) {
wp_die( __( 'Invalid method.', 'codepress-admin-columns' ), null, 400 );
}

// Trigger ajax callback
echo $column->get_ajax_value( $id );
exit;
}

}
Loading

0 comments on commit 341424d

Please sign in to comment.