| 1 |
<?php |
| 2 |
/** |
| 3 |
* Heartbeat Extension |
| 4 |
* |
| 5 |
* @package PoweredCache\Extensions |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace PoweredCache\Extensions\Heartbeat; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class Heartbeat |
| 12 |
*/ |
| 13 |
class Heartbeat { |
| 14 |
|
| 15 |
/** |
| 16 |
* Plugin settings |
| 17 |
* |
| 18 |
* @var $settings |
| 19 |
*/ |
| 20 |
private $settings; |
| 21 |
|
| 22 |
/** |
| 23 |
* Holds location for condition |
| 24 |
* |
| 25 |
* @var string $current_location |
| 26 |
*/ |
| 27 |
private $current_location; |
| 28 |
|
| 29 |
/** |
| 30 |
* Placeholder constructor |
| 31 |
*/ |
| 32 |
public function __construct() { |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
/** |
| 37 |
* Return an instance of the current class, create one if it doesn't exist |
| 38 |
* |
| 39 |
* @return object |
| 40 |
* @since 1.0 |
| 41 |
*/ |
| 42 |
public static function factory() { |
| 43 |
|
| 44 |
static $instance; |
| 45 |
|
| 46 |
if ( ! $instance ) { |
| 47 |
$instance = new self(); |
| 48 |
$instance->setup(); |
| 49 |
} |
| 50 |
|
| 51 |
return $instance; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Setup routine |
| 56 |
*/ |
| 57 |
public function setup() { |
| 58 |
if ( wp_doing_ajax() ) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
$this->settings = \PoweredCache\Utils\get_settings(); |
| 63 |
$this->set_location(); |
| 64 |
|
| 65 |
add_action( 'wp_enqueue_scripts', [ $this, 'maybe_stop_heartbeat' ] ); |
| 66 |
add_action( 'admin_enqueue_scripts', [ $this, 'maybe_stop_heartbeat' ] ); |
| 67 |
add_action( 'heartbeat_settings', [ $this, 'maybe_change_frequency' ] ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Maybe stop heartbeat for particular location |
| 72 |
*/ |
| 73 |
public function maybe_stop_heartbeat() { |
| 74 |
$settings_key = sprintf( 'heartbeat_%s_status', $this->current_location ); |
| 75 |
|
| 76 |
if ( isset( $this->settings[ $settings_key ] ) && 'disable' === $this->settings[ $settings_key ] ) { |
| 77 |
wp_deregister_script( 'heartbeat' ); |
| 78 |
|
| 79 |
return; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Maybe modify heartbeat frequency |
| 85 |
* |
| 86 |
* @param array $settings heartbeat settings |
| 87 |
* |
| 88 |
* @return mixed |
| 89 |
*/ |
| 90 |
public function maybe_change_frequency( $settings ) { |
| 91 |
$status_key = sprintf( 'heartbeat_%s_status', $this->current_location ); |
| 92 |
$interval_key = sprintf( 'heartbeat_%s_interval', $this->current_location ); |
| 93 |
|
| 94 |
if ( ! isset( $this->settings[ $status_key ] ) ) { |
| 95 |
return $settings; |
| 96 |
} |
| 97 |
|
| 98 |
if ( 'modify' !== $this->settings[ $status_key ] ) { |
| 99 |
return $settings; |
| 100 |
} |
| 101 |
|
| 102 |
if ( ! empty( $this->settings[ $interval_key ] ) ) { |
| 103 |
$settings['interval'] = absint( $this->settings[ $interval_key ] ); |
| 104 |
} |
| 105 |
|
| 106 |
return $settings; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Set current location |
| 111 |
* Supported locations [editor,dashboard,frontend] |
| 112 |
*/ |
| 113 |
public function set_location() { |
| 114 |
$editor_pages = [ |
| 115 |
'/wp-admin/post-new.php', |
| 116 |
'/wp-admin/post.php', |
| 117 |
]; |
| 118 |
|
| 119 |
if ( is_admin() && in_array( $_SERVER['REQUEST_URI'], $editor_pages, true ) ) { |
| 120 |
$this->current_location = 'editor'; |
| 121 |
} elseif ( is_admin() ) { |
| 122 |
$this->current_location = 'dashboard'; |
| 123 |
} else { |
| 124 |
$this->current_location = 'frontend'; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
} |
| 129 |
|