| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin.php |
| 4 |
* |
| 5 |
* @package expanding-archives |
| 6 |
* @copyright Copyright (c) 2026, Ashley Gibson |
| 7 |
* @license GPL2+ |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Ashleyfae\ExpandingArchives; |
| 11 |
|
| 12 |
use Ashleyfae\ExpandingArchives\Api\v1\Posts; |
| 13 |
use Ashleyfae\ExpandingArchives\Helpers\ArchiveRenderer; |
| 14 |
use Ashleyfae\ExpandingArchives\ValueObjects\Month; |
| 15 |
|
| 16 |
class Plugin |
| 17 |
{ |
| 18 |
/** |
| 19 |
* Single instance of this plugin. |
| 20 |
* |
| 21 |
* @var Plugin|null |
| 22 |
* @since 2.0 |
| 23 |
*/ |
| 24 |
private static ?Plugin $instance = null; |
| 25 |
|
| 26 |
private string $assetsUrl; |
| 27 |
|
| 28 |
public function __construct() |
| 29 |
{ |
| 30 |
$this->assetsUrl = trailingslashit(plugins_url('assets/build/', EXPANDING_ARCHIVES_FILE)); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Returns the single instance of this class. |
| 35 |
* |
| 36 |
* @since 2.0 |
| 37 |
* |
| 38 |
* @return Plugin |
| 39 |
*/ |
| 40 |
public static function instance(): Plugin |
| 41 |
{ |
| 42 |
if (is_null(self::$instance)) { |
| 43 |
self::$instance = new self; |
| 44 |
} |
| 45 |
|
| 46 |
return self::$instance; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Magic getter to handle backwards compatibility for properties that no longer exist. |
| 51 |
* |
| 52 |
* @since 2.0 |
| 53 |
* |
| 54 |
* @param string $property |
| 55 |
* |
| 56 |
* @return string|null |
| 57 |
*/ |
| 58 |
public function __get($property) |
| 59 |
{ |
| 60 |
if (property_exists($this, $property)) { |
| 61 |
return $this->{$property}; |
| 62 |
} |
| 63 |
|
| 64 |
// Backwards compatibility for old properties from 1.x. |
| 65 |
switch ($property) { |
| 66 |
case '_version' : |
| 67 |
return EXPANDING_ARCHIVES_VERSION; |
| 68 |
case '_token' : |
| 69 |
return 'expanding-archives'; |
| 70 |
case 'file' : |
| 71 |
return EXPANDING_ARCHIVES_FILE; |
| 72 |
case 'dir' : |
| 73 |
return dirname(EXPANDING_ARCHIVES_FILE); |
| 74 |
case 'assets_dir' : |
| 75 |
return trailingslashit(dirname(EXPANDING_ARCHIVES_FILE)).'assets'; |
| 76 |
case 'assets_url' : |
| 77 |
return $this->assetsUrl; |
| 78 |
case 'script_suffix' : |
| 79 |
return ''; |
| 80 |
default : |
| 81 |
return null; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Boots the plugin. |
| 87 |
* |
| 88 |
* @since 2.0 |
| 89 |
* |
| 90 |
* @return void |
| 91 |
*/ |
| 92 |
public function boot(): void |
| 93 |
{ |
| 94 |
add_action('init', [$this, 'loadTextDomain']); |
| 95 |
add_action('wp_enqueue_scripts', [$this, 'loadAssets']); |
| 96 |
add_action('widgets_init', function () { |
| 97 |
register_widget(Widget::class); |
| 98 |
}); |
| 99 |
add_action('rest_api_init', function () { |
| 100 |
(new Posts())->register(); |
| 101 |
}); |
| 102 |
|
| 103 |
// Delete transient when new post is published. |
| 104 |
add_action('transition_post_status', array($this, 'delete_transient'), 10, 3); |
| 105 |
|
| 106 |
// Backwards compatibility. |
| 107 |
class_alias(Widget::class, 'NG_Expanding_Archives_Widget'); |
| 108 |
class_alias(Plugin::class, 'NG_Expanding_Archives'); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Load the plugin language files. |
| 113 |
* |
| 114 |
* @since 2.0 |
| 115 |
*/ |
| 116 |
public function loadTextDomain(): void |
| 117 |
{ |
| 118 |
$locale = apply_filters('plugin_locale', get_locale(), 'expanding-archives'); |
| 119 |
|
| 120 |
load_textdomain( |
| 121 |
'expanding-archives', |
| 122 |
WP_LANG_DIR.'/'.'expanding-archives'.'/'.'expanding-archives'.'-'.$locale.'.mo' |
| 123 |
); |
| 124 |
load_plugin_textdomain('expanding-archives', false, dirname(plugin_basename(EXPANDING_ARCHIVES_FILE)).'/lang/'); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Adds the CSS and JavaScript for the plugin. |
| 129 |
* |
| 130 |
* @since 2.0 |
| 131 |
*/ |
| 132 |
public function loadAssets(): void |
| 133 |
{ |
| 134 |
wp_enqueue_style( |
| 135 |
'expanding-archives', |
| 136 |
$this->assetsUrl.'css/expanding-archives.css', |
| 137 |
[], |
| 138 |
EXPANDING_ARCHIVES_VERSION |
| 139 |
); |
| 140 |
|
| 141 |
wp_enqueue_script( |
| 142 |
'expanding-archives-frontend', |
| 143 |
$this->assetsUrl.'js/expanding-archives.js', |
| 144 |
[], |
| 145 |
EXPANDING_ARCHIVES_VERSION, |
| 146 |
true |
| 147 |
); |
| 148 |
|
| 149 |
wp_localize_script( |
| 150 |
'expanding-archives-frontend', |
| 151 |
'expandingArchives', |
| 152 |
[ |
| 153 |
'ajaxurl' => admin_url('admin-ajax.php'), |
| 154 |
'nonce' => wp_create_nonce('expand_archives'), |
| 155 |
'restBase' => rest_url('expanding-archives/v1/posts'), |
| 156 |
'restNonce' => wp_create_nonce('rest_nonce'), |
| 157 |
] |
| 158 |
); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Gets a list of all the posts in the current month. |
| 163 |
* |
| 164 |
* @access public |
| 165 |
* @since 1.0.0 |
| 166 |
* @deprecated 2.0 |
| 167 |
* @return string |
| 168 |
*/ |
| 169 |
public function get_current_month_posts(): string |
| 170 |
{ |
| 171 |
$renderer = new ArchiveRenderer(); |
| 172 |
$month = new Month( |
| 173 |
date('Y'), |
| 174 |
date('m') |
| 175 |
); |
| 176 |
|
| 177 |
return $renderer->getPostsInMonthHtml($month); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Gets a list of all the posts in a given month/date via ajax. |
| 182 |
* |
| 183 |
* @access public |
| 184 |
* @since 1.0.0 |
| 185 |
* @deprecated 2.0 |
| 186 |
* @return void |
| 187 |
*/ |
| 188 |
public function load_monthly_archives(): void |
| 189 |
{ |
| 190 |
// Security check. |
| 191 |
check_ajax_referer('expand_archives', 'nonce'); |
| 192 |
|
| 193 |
$renderer = new ArchiveRenderer(); |
| 194 |
$month = new Month( |
| 195 |
absint($_POST['year']) ?? date('Y'), |
| 196 |
absint($_POST['month']) ?? date('m') |
| 197 |
); |
| 198 |
|
| 199 |
wp_send_json_success( |
| 200 |
$renderer->getPostsInMonthHtml($month) |
| 201 |
); |
| 202 |
|
| 203 |
exit; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Deletes our transient of posts in the current month when |
| 208 |
* a new post is published. |
| 209 |
* |
| 210 |
* @param string $new_status |
| 211 |
* @param string $old_status |
| 212 |
* @param \WP_Post $post |
| 213 |
* |
| 214 |
* @access public |
| 215 |
* @since 1.0.0 |
| 216 |
* @return void |
| 217 |
*/ |
| 218 |
public function delete_transient($new_status, $old_status, $post): void |
| 219 |
{ |
| 220 |
if ($new_status === 'publish' || $old_status === 'publish') { |
| 221 |
delete_transient(sprintf('expanding_archives_posts_%d_%d', date('Y'), date('m'))); |
| 222 |
delete_transient('expanding_archives_current_month_posts'); |
| 223 |
delete_transient('expanding_archives_months'); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
} |
| 228 |
|