Skip to content

Commit

Permalink
add site type methods, move to Plugin class, tidy naming
Browse files Browse the repository at this point in the history
  • Loading branch information
tharsheblows committed Aug 6, 2024
1 parent e842334 commit a6d2d36
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 31 deletions.
39 changes: 13 additions & 26 deletions classes/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,12 @@
*/
class Admin {

/**
* Used to check if it's a single site, not multisite.
*/
const WP_STREAM_SINGLE_SITE = 'single';

/**
* Used to check if it's a multisite with the plugin network enabled.
*/
const WP_STREAM_MULTI_NETWORK = 'multisite-network';

/**
* Used to check if it's a multisite with the plugin not network enabled.
*/
const WP_STREAM_MULTI_NOT_NETWORK = 'multisite-not-network';

/**
* The async deletion action for large sites.
*
* @const string
*/
const WP_STREAM_ASYNC_DELETION_ACTION = 'stream_erase_large_records_action';
const ASYNC_DELETION_ACTION = 'stream_erase_large_records_action';

/**
* Holds Instance of plugin object
Expand Down Expand Up @@ -162,7 +149,7 @@ public function __construct( $plugin ) {
add_filter( 'user_has_cap', array( $this, 'filter_user_caps' ), 10, 4 );
add_filter( 'role_has_cap', array( $this, 'filter_role_caps' ), 10, 3 );

if ( self::WP_STREAM_MULTI_NETWORK === $this->plugin->get_site_type() && ! is_network_admin() ) {
if ( $this->plugin->is_multisite_network_activated() && ! is_network_admin() ) {
$options = (array) get_site_option( 'wp_stream_network', array() );
$option = isset( $options['general_site_access'] ) ? absint( $options['general_site_access'] ) : 1;

Expand Down Expand Up @@ -235,7 +222,7 @@ public function __construct( $plugin ) {

// Async action for erasing large log tables.
add_action(
self::WP_STREAM_ASYNC_DELETION_ACTION,
self::ASYNC_DELETION_ACTION,
array(
$this,
'erase_large_records',
Expand Down Expand Up @@ -761,7 +748,7 @@ private function erase_stream_records() {

// If this is a multisite and it's not networked activated,
// only delete the entries from the blog which made the request.
if ( self::WP_STREAM_MULTI_NOT_NETWORK === $this->plugin->get_site_type() ) {
if ( $this->plugin->is_multisite_not_network_activated() ) {

// First check the log size.
$stream_log_size = self::get_blog_record_table_size();
Expand Down Expand Up @@ -823,7 +810,7 @@ private function schedule_erase_large_records( int $log_size ) {
'blog_id' => (int) get_current_blog_id(),
);

as_enqueue_async_action( self::WP_STREAM_ASYNC_DELETION_ACTION, $args );
as_enqueue_async_action( self::ASYNC_DELETION_ACTION, $args );
}

/**
Expand All @@ -832,7 +819,7 @@ private function schedule_erase_large_records( int $log_size ) {
* @return bool True if the async deletion process is running, false otherwise.
*/
public static function is_running_async_deletion() {
return as_has_scheduled_action( self::WP_STREAM_ASYNC_DELETION_ACTION );
return as_has_scheduled_action( self::ASYNC_DELETION_ACTION );
}

/**
Expand Down Expand Up @@ -889,7 +876,7 @@ public function erase_large_records( int $total, int $done, int $last_entry, int
$done = $total - $remaining;

as_enqueue_async_action(
self::WP_STREAM_ASYNC_DELETION_ACTION,
self::ASYNC_DELETION_ACTION,
array(
'total' => (int) $total,
'done' => (int) $done,
Expand Down Expand Up @@ -957,15 +944,15 @@ public function purge_scheduled_action() {

// Don't purge when in Network Admin unless Stream is network activated.
if (
self::WP_STREAM_MULTI_NOT_NETWORK === $this->plugin->get_site_type()
$this->plugin->is_multisite_not_network_activated()
&&
is_network_admin()
) {
return;
}

$defaults = $this->plugin->settings->get_defaults();
if ( self::WP_STREAM_MULTI_NETWORK === $this->plugin->get_site_type() ) {
if ( $this->plugin->is_multisite_network_activated() ) {
$options = (array) get_site_option( 'wp_stream_network', $defaults );
} else {
$options = (array) get_option( 'wp_stream', $defaults );
Expand All @@ -984,7 +971,7 @@ public function purge_scheduled_action() {
$where = $wpdb->prepare( ' AND `stream`.`created` < %s', $date->format( 'Y-m-d H:i:s' ) );

// Multisite but NOT network activated, only purge the current blog.
if ( self::WP_STREAM_MULTI_NOT_NETWORK === $this->plugin->get_site_type() ) {
if ( $this->plugin->is_multisite_not_network_activated() ) {
$where .= $wpdb->prepare( ' AND `blog_id` = %d', get_current_blog_id() );
}

Expand Down Expand Up @@ -1013,7 +1000,7 @@ public function plugin_action_links( $links, $file ) {
}

// Also don't show links in Network Admin if Stream isn't network enabled.
if ( is_network_admin() && self::WP_STREAM_MULTI_NOT_NETWORK === $this->plugin->get_site_type() ) {
if ( is_network_admin() && $this->plugin->is_multisite_not_network_activated() ) {
return $links;
}

Expand Down
53 changes: 51 additions & 2 deletions classes/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@ class Plugin {
*/
const WP_CLI_COMMAND = 'stream';


/**
* Used to check if it's a single site, not multisite.
*
* @const string
*/
const SINGLE_SITE = 'single';

/**
* Used to check if it's a multisite with the plugin network enabled.
*
* @const string
*/
const MULTI_NETWORK = 'multisite-network';

/**
* Used to check if it's a multisite with the plugin not network enabled.
*
* @const string
*/
const MULTI_NOT_NETWORK = 'multisite-not-network';

/**
* Holds and manages WordPress Admin configurations.
*
Expand Down Expand Up @@ -349,10 +371,10 @@ public function get_site_type(): string {

// If it's a multisite, is it network activated or not?
if ( is_multisite() ) {
return $this->is_network_activated() ? Admin::WP_STREAM_MULTI_NETWORK : Admin::WP_STREAM_MULTI_NOT_NETWORK;
return $this->is_network_activated() ? self::MULTI_NETWORK : self::MULTI_NOT_NETWORK;
}

return Admin::WP_STREAM_SINGLE_SITE;
return self::SINGLE_SITE;
}

/**
Expand All @@ -364,4 +386,31 @@ public function get_site_type(): string {
public function is_large_records_table( int $record_number ): bool {
return apply_filters( 'wp_stream_is_large_records_table', $record_number > 1000000, $record_number );
}

/**
* Checks if the plugin is running on a single site installation.
*
* @return bool True if the plugin is running on a single site installation, false otherwise.
*/
public function is_single_site() {
return self::SINGLE_SITE === $this->get_site_type();
}

/**
* Check if the plugin is activated on a multisite installation but not network activated.
*
* @return bool True if the plugin is activated on a multisite installation but not network activated, false otherwise.
*/
public function is_multisite_not_network_activated() {
return self::MULTI_NOT_NETWORK === $this->get_site_type();
}

/**
* Check if the plugin is activated on a multisite network.
*
* @return bool True if the plugin is network activated on a multisite, false otherwise.
*/
public function is_multisite_network_activated() {
return self::MULTI_NETWORK === $this->get_site_type();
}
}
4 changes: 2 additions & 2 deletions classes/class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,10 +481,10 @@ public function get_deletion_warning(): string {
$site_type = $this->plugin->get_site_type();

switch ( $site_type ) {
case Admin::WP_STREAM_MULTI_NETWORK:
case Plugin::MULTI_NETWORK:
$warning = __( 'Warning: This will delete all activity records from the database for all sites.', 'stream' );
break;
case Admin::WP_STREAM_MULTI_NOT_NETWORK:
case Plugin::MULTI_NOT_NETWORK:
$warning = $this->plugin->is_large_records_table( Admin::get_blog_record_table_size() ) ?
$this->get_async_deletion_warning() :
__( 'Warning: This will delete all activity records from the database for this site.', 'stream' );
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/test-class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public function test_wp_ajax_reset_large_records_blog() {
// Assert the scheduled action has been set.
$this->assertTrue(
as_has_scheduled_action(
Admin::WP_STREAM_ASYNC_DELETION_ACTION
Admin::ASYNC_DELETION_ACTION
)
);

Expand Down

0 comments on commit a6d2d36

Please sign in to comment.