tools-page.php
3.48 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
<?php
class P2P_Tools_Page extends scbAdminPage {
function setup() {
$this->args = array(
'page_title' => __( 'Connection Types', P2P_TEXTDOMAIN ),
'page_slug' => 'connection-types',
'parent' => 'tools.php',
);
add_action( 'admin_notices', array( $this, 'maybe_install' ) );
}
function maybe_install() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$current_ver = badgeos_utilities::get_option( 'p2p_storage' );
if ( $current_ver == P2P_Storage::$version ) {
return;
}
P2P_Storage::install();
badgeos_utilities::update_option( 'p2p_storage', P2P_Storage::$version );
}
function form_handler() {
if ( empty( $_POST['p2p_convert'] ) ) {
return false;
}
check_admin_referer( $this->nonce );
global $wpdb;
$old_p2p_type = sanitize_text_field( $_POST['old_p2p_type'] );
$new_p2p_type = sanitize_text_field( $_POST['new_p2p_type'] );
if ( ! p2p_type( $new_p2p_type ) ) {
$this->admin_msg( sprintf( __( '<em>%s</em> is not a registered connection type.', P2P_TEXTDOMAIN ), esc_html( $new_p2p_type ) ) );
return;
}
$count = $wpdb->update(
$wpdb->p2p,
array( 'p2p_type' => $new_p2p_type ),
array( 'p2p_type' => $old_p2p_type )
);
$this->admin_msg(
sprintf(
__( 'Converted %1$s connections from <em>%2$s</em> to <em>%3$s</em>.', P2P_TEXTDOMAIN ),
number_format_i18n( $count ),
esc_html( $old_p2p_type ),
esc_html( $new_p2p_type )
)
);
}
function page_head() {
wp_enqueue_style( 'p2p-tools', plugins_url( 'tools.css', __FILE__ ), array(), P2P_PLUGIN_VERSION );
}
function page_content() {
$data = array(
'columns' => array(
__( 'Name', P2P_TEXTDOMAIN ),
__( 'Information', P2P_TEXTDOMAIN ),
__( 'Connections', P2P_TEXTDOMAIN ),
),
);
$connection_counts = $this->get_connection_counts();
if ( empty( $connection_counts ) ) {
$data['has-rows'] = false;
$data['no-rows'] = __( 'No connection types registered.', P2P_TEXTDOMAIN );
$data['no-rows2'] = sprintf(
__( 'To register a connection type, see <a href="%s">the wiki</a>.', P2P_TEXTDOMAIN ),
'https://github.com/scribu/wp-posts-to-posts/wiki/'
);
} else {
$data['has-rows'] = array( true );
foreach ( $connection_counts as $p2p_type => $count ) {
$row = array(
'p2p_type' => $p2p_type,
'count' => number_format_i18n( $count ),
);
$ctype = p2p_type( $p2p_type );
if ( $ctype ) {
$row['desc'] = $ctype->get_desc();
} else {
$row['desc'] = __( 'Convert to registered connection type:', P2P_TEXTDOMAIN ) . scbForms::form_wrap( $this->get_dropdown( $p2p_type ), $this->nonce );
$row['class'] = 'error';
}
$data['rows'][] = $row;
}
}
echo P2P_Mustache::render( 'connection-types', $data );
}
private function get_connection_counts() {
global $wpdb;
$counts = $wpdb->get_results(
"
SELECT p2p_type, COUNT(*) as count
FROM $wpdb->p2p
GROUP BY p2p_type
"
);
$counts = scb_list_fold( $counts, 'p2p_type', 'count' );
foreach ( P2P_Connection_Type_Factory::get_all_instances() as $p2p_type => $ctype ) {
if ( ! isset( $counts[ $p2p_type ] ) ) {
$counts[ $p2p_type ] = 0;
}
}
ksort( $counts );
return $counts;
}
private function get_dropdown( $p2p_type ) {
$data = array(
'old_p2p_type' => $p2p_type,
'options' => array_keys( P2P_Connection_Type_Factory::get_all_instances() ),
'button_text' => __( 'Go', P2P_TEXTDOMAIN ),
);
return P2P_Mustache::render( 'connection-types-form', $data );
}
}