-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwp-contributions.php
263 lines (220 loc) · 7.35 KB
/
wp-contributions.php
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/**
* Plugin Name: WP Contributions
* Plugin URI: https://michaelbox.net
* Description: Provides an easy way to display your WordPress.org Themes, Plugins, Core tickets, and Codex contributions with handy widgets and template tags.
* Author: Michael Beckwith
* Author URI: https://michaelbox.net
* Version: 1.3.1
* License: GPLv2
* Requires PHP: 7.4
* Text Domain: wp-contributions
*
* @package WP Contributions
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WDS_WP_Contributions' ) ) {
class WDS_WP_Contributions {
public string $basename;
public string $directory_path;
public string $directory_url;
public string $is_query;
public object $query;
/**
* Construct function to get things started.
*/
public function __construct() {
// Setup some base variables for the plugin.
$this->basename = plugin_basename( __FILE__ );
$this->directory_path = plugin_dir_path( __FILE__ );
$this->directory_url = plugins_url( dirname( $this->basename ) );
$this->is_query = 'false';
$this->query = new stdClass();
}
/**
* Creates or returns an instance of this class.
*
* @since 1.1.0
*
* @return WDS_WP_Contributions A single instance of this class.
*/
public function init(): WDS_WP_Contributions {
static $instance = null;
if ( null === $instance ) {
$instance = new self();
}
$instance->includes();
$instance->hooks();
return $instance;
}
/**
* Add hooks and filters.
*
* @since 1.1.0
*/
public function hooks() {
// Activation/Deactivation Hooks.
register_activation_hook( __FILE__, [ $this, 'activate' ] );
register_deactivation_hook( __FILE__, [ $this, 'deactivate' ] );
// Register Widgets.
add_action( 'widgets_init', [ $this, 'register_widgets' ] );
// Enqueue necessary styles.
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue' ] );
// Block init
add_action( 'init', [ $this, 'wp_contributions_block_init' ] );
}
/**
* Include our plugin dependencies.
*/
public function includes() {
require_once( $this->directory_path . 'inc/class-wds-wp-contributions-plugins.php' );
require_once( $this->directory_path . 'inc/class-wds-wp-contributions-themes.php' );
require_once( $this->directory_path . 'inc/class-wds-wp-contributions-core.php' );
require_once( $this->directory_path . 'inc/class-wds-wp-contributions-codex.php' );
require_once( $this->directory_path . 'inc/class-wds-wp-contributions-plugin-widget.php' );
require_once( $this->directory_path . 'inc/class-wds-wp-contributions-theme-widget.php' );
require_once( $this->directory_path . 'inc/class-wds-wp-contributions-core-widget.php' );
require_once( $this->directory_path . 'inc/class-wds-wp-contributions-codex-widget.php' );
require_once( $this->directory_path . 'inc/helper-functions.php' );
require_once( $this->directory_path . 'inc/shortcodes.php' );
}
/**
* Activation hook for the plugin.
*/
public function activate() {
}
/**
* Deactivation hook for the plugin.
*/
public function deactivate() {
}
/**
* Register our widgets to display plugins, author, themes, and more.
*/
function register_widgets() {
register_widget( 'WDS_WP_Contributions_Plugin_Widget' );
register_widget( 'WDS_WP_Contributions_Theme_Widget' );
register_widget( 'WDS_WP_Contributions_Core_Widget' );
register_widget( 'WDS_WP_Contributions_Codex_Widget' );
}
/**
* Enqueue script.
*
* @since 1.1.0
*/
public function enqueue() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
wp_enqueue_style( 'wds-wp-contributions', $this-> directory_url . "/assets/css/style$min.css", [ 'dashicons' ], '150505' );
}
/**
* Handles the display of the card view to display contribution data.
*
* @param array $args Arguments to pass to the card display.
* @return string The HTML output for the card view.
*/
public function display_card( $args = [] ) {
if ( ! isset( $args['slug'] ) ) {
return '<p>' . esc_html__( 'No Slug Entered', 'wp-contributions' );
} else {
$args['slug'] = strtolower( $args['slug'] );
}
$args = apply_filters( 'wp_contributions_display_card_args', $args );
$card = '';
if ( 'plugin' === $args['type'] ) {
$plugin_api = new WDS_WP_Contributions_Plugins();
$plugin_data = $plugin_api->get_plugin( $args['slug'] );
if ( ! is_wp_error( $plugin_data ) ) {
$card .= $plugin_api->display( $plugin_data );
} else {
$card .= '<p>' . esc_html__( 'Plugin API failed. The plugin slug could be incorrect or there could be an error with the WP Plugin API.', 'wp-contributions' );
}
} elseif ( 'theme' === $args['type'] ) {
$theme_api = new WDS_WP_Contributions_Themes();
$theme_data = $theme_api->get_theme( $args['slug'] );
if ( ! is_wp_error( $theme_data ) ) {
$card .= $theme_api->display( $theme_data );
} else {
$card .= '<p>' . esc_html__( 'Theme API failed. The theme slug could be incorrect or there could be an error with the WP Theme API.', 'wp-contributions' );
}
} elseif ( 'core' === $args['type'] ) {
$count = isset( $args['count'] ) ? $args['count'] : 5;
$core = new WDS_WP_Contributions_Core();
$card .= $core->display( $args['slug'], $count );
} elseif ( 'codex' === $args['type'] ) {
$count = isset( $args['count'] ) ? $args['count'] : 5;
$codex = new WDS_WP_Contributions_Codex();
$card .= $codex->display( $args['slug'], $count );
}
return apply_filters( 'wp_contributions_display_card', $card, $args );
}
/**
* Block Initializer.
*/
public function wp_contributions_block_init() {
register_block_type(
plugin_dir_path( __FILE__ ) . 'assets/block/build',
[
'render_callback' => 'wp_contributions_block_callback',
'attributes' => [
'slug' => [
'type' => 'string',
],
'preferred_username' => [
'type' => 'string',
'default' => 'My Preferred_handle',
],
'theme' => [
'type' => 'boolean',
'default' => false,
],
'contribution_type' => [
'type' => 'string',
'default' => 'plugin',
],
],
]
);
}
}
/**
* Block Output.
*
* @param string $attr Block attributes
* @return string The HTML output for the card view.
*/
function wp_contributions_block_callback( $attr ) {
if ( isset( $attr['slug'] ) && isset( $attr['contribution_type'] ) ) {
try {
ob_start();
global $wp_contributions;
echo $wp_contributions->display_card( [ 'type' => $attr['contribution_type'], 'slug' => $attr['slug'] ] );
return ob_get_clean();
} catch ( Exception $e ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG === true ) {
error_log( $e );
}
}
}
/* Default output */
ob_start();
echo '<p>' . esc_html__( 'Still missing must-have paramaters for the render.', 'wp-contributions' );
return ob_get_clean();
}
/**
* Grab the WDS_WP_Contributions object and return it.
* Wrapper for WDS_WP_Contributions::get_instance()
*/
function load_wp_contributions() {
global $wp_contributions;
$wp_contributions = new WDS_WP_Contributions();
$wp_contributions->init();
return $wp_contributions;
}
add_action( 'plugins_loaded', 'load_wp_contributions' );
}
add_action( 'init', function() {
load_plugin_textdomain( 'wp-contributions' );
} );