-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-augment-types.php
More file actions
171 lines (114 loc) · 3.39 KB
/
class-augment-types.php
File metadata and controls
171 lines (114 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* @package Augment Types
* @since 0.1.0
*/
use AugmentTypes\Admin;
use AugmentTypes\Archive;
use AugmentTypes\Excerpt;
use AugmentTypes\Expire;
use AugmentTypes\Feature;
use AugmentTypes\Sort;
class AugmentTypes {
private static ?self $instance = null;
/** @var array<string, mixed> */
private static array $data;
public static function instance(): self {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
add_action( 'init', array( $this, 'load_text_domain' ) );
add_action( 'wpmu_new_blog', array( $this, 'new_blog' ) );
add_filter( 'term_count', array( $this, 'per_type' ), 10, 3 );
add_action( 'init', array( Admin::instance(), 'init' ), 20 );
add_filter( 'plugin_action_links_' . plugin_basename( AUGMENT_TYPES ), array( $this, 'settings_link' ) );
Sort::instance();
Feature::instance();
Archive::instance();
Excerpt::instance();
Expire::instance();
}
/** @return mixed */
public static function get_data( string $key ) {
return self::$data[ $key ];
}
public static function activate( bool $network_wide ): void {
if ( function_exists( 'is_multisite' ) && is_multisite() && $network_wide ) {
/** @var wpdb $wpdb */
global $wpdb;
$current = $wpdb->blogid;
$blogs = $wpdb->get_col( "SELECT `blog_id` FROM $wpdb->blogs" );
foreach ( $blogs as $blog ) {
switch_to_blog( $blog );
self::alter_table();
}
switch_to_blog( $current );
} else {
self::alter_table();
}
}
public function load_text_domain(): void {
load_plugin_textdomain( 'augment-types' );
if ( ! function_exists( 'get_plugin_data' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
self::$data = get_plugin_data( AUGMENT_TYPES, false, false );
self::$data['URL'] = plugin_dir_url( AUGMENT_TYPES );
self::$data['PATH'] = plugin_dir_path( AUGMENT_TYPES );
}
public static function new_blog( int $id ): void {
/** @var wpdb $wpdb */
global $wpdb;
if ( is_plugin_active_for_network( plugin_basename( AUGMENT_TYPES ) ) ) {
$current = $wpdb->blogid;
switch_to_blog( $id );
self::alter_table();
switch_to_blog( $current );
}
}
protected static function alter_table(): void {
/** @var wpdb $wpdb */
global $wpdb;
if ( ! $wpdb->query( "SHOW COLUMNS FROM $wpdb->terms LIKE 'term_order'" ) ) {
$wpdb->query( "ALTER TABLE $wpdb->terms ADD `term_order` INT( 11 ) NOT NULL DEFAULT '0'" );
}
}
public function per_type( int $value, int $term_id, string $taxonomy ): int {
$screen = get_current_screen();
if ( null === $screen || 'edit-tags' !== $screen->base ) {
return $value;
}
$args = array(
'post_type' => $screen->post_type,
'post_status' => 'any',
'fields' => 'ids',
'no_found_rows' => true,
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'terms' => $term_id,
),
),
);
$query = new WP_Query( $args );
return $query->post_count;
}
/**
* @param string[] $links
*
* @return string[]
*/
public function settings_link( array $links ): array {
$settings = sprintf(
'<a href="%1$s" target="%2$s">%3$s</a>',
admin_url( Admin::PARENT_PAGE . '?page=' . Admin::OPTION_KEY ),
'_self',
__( 'Settings', 'augment-types' ),
);
return array_merge( compact( 'settings' ), $links );
}
}