-
Notifications
You must be signed in to change notification settings - Fork 0
/
dovetail-podcasts.php
141 lines (126 loc) · 4.4 KB
/
dovetail-podcasts.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
<?php
/**
* Plugin Name: Dovetail Podcasts
* Plugin URI: https://github.com/PRX/Dovetail-Wordpress-Plugin
* GitHub Plugin URI: https://github.com/PRX/Dovetail-Wordpress-Plugin
* Description: Dovetail Podcasts publishing integration.
* Author: PRX
* Author URI: http://prx.org
* Version: 0.0.0
* Text Domain: dovetail-podcasts
* License: GPL-3
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
*
* @package DovetailPodcasts
* @author PRX
* @version 0.0.0
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Load files that are required even if the composer autoloader isn't installed
*/
function dovetail_podcasts_require_bootstrap_files(): void {
if ( file_exists( __DIR__ . '/constants.php' ) ) {
require_once __DIR__ . '/constants.php';
}
if ( file_exists( __DIR__ . '/activation.php' ) ) {
require_once __DIR__ . '/activation.php';
}
if ( file_exists( __DIR__ . '/deactivation.php' ) ) {
require_once __DIR__ . '/deactivation.php';
}
if ( file_exists( __DIR__ . '/access-functions.php' ) ) {
require_once __DIR__ . '/access-functions.php';
}
if ( file_exists( __DIR__ . '/src/DovetailPodcasts.php' ) ) {
require_once __DIR__ . '/src/DovetailPodcasts.php';
}
}
/**
* Determines if the plugin can load.
*
* Test env:
* - DTPODCASTS_AUTOLOAD: false
* - autoload installed and manually added in test env
*
* Bedrock
* - DTPODCASTS_AUTOLOAD: not defined
* - composer deps installed outside of the plugin
*
* Normal (.org repo install)
* - DTPODCASTS_AUTOLOAD: not defined
* - composer deps installed INSIDE the plugin
*/
function dovetail_podcasts_can_load_plugin(): bool {
// Load the bootstrap files (needed before autoloader is configured).
dovetail_podcasts_require_bootstrap_files();
// If DTPODCASTS is already loaded,
// We can assume that DTPODCASTS has been installed as a composer dependency of a parent project.
if ( class_exists( 'DovetailPodcasts' ) ) {
return true;
}
/**
* DTPODCASTS_AUTOLOAD can be set to "false" to prevent the autoloader from running.
* In most cases, this is not something that should be disabled, but some environments
* may bootstrap their dependencies in a global autoloader that will autoload files
* before we get to this point, and requiring the autoloader again can trigger fatal errors.
*
* The codeception tests are an example of an environment where adding the autoloader again causes issues
* so this is set to false for tests.
*/
if ( defined( 'DTPODCASTS_AUTOLOAD' ) && false === DTPODCASTS_AUTOLOAD ) {
// IF DTPODCASTS_AUTOLOAD is defined as false,
// but the DTPODCASTS Class exists, we can assume the dependencies
// are loaded from the parent project.
return true;
}
if ( file_exists( plugin_dir_path( __FILE__ ) . 'vendor/autoload.php' ) ) {
// Autoload Required Classes.
require_once plugin_dir_path( __FILE__ ) . 'vendor/autoload.php';
}
// If the DovetailPodcasts class still doesn't exist, bail as there was an issue bootstrapping the plugin.
if ( ! class_exists( 'DovetailPodcasts' ) ) {
return false;
}
return true;
}
if ( ! function_exists( 'dovetail_podcasts_init' ) ) {
/**
* Function that instantiates the plugins main class
*
* @return object|null
*/
function dovetail_podcasts_init() {
// iBail f the plugin can't be loaded.
if ( false === dovetail_podcasts_can_load_plugin() ) {
add_action( 'network_admin_notices', 'dovetail_podcasts_cannot_load_admin_notice_callback' );
add_action( 'admin_notices', 'dovetail_podcasts_cannot_load_admin_notice_callback' );
return null;
}
/**
* Return an instance of the action.
*/
return \DovetailPodcasts::instance();
}
}
dovetail_podcasts_init();
// Setup activation/deactivation hooks.
register_deactivation_hook( __FILE__, 'dovetail_podcasts_deactivation_callback' );
register_activation_hook( __FILE__, 'dovetail_podcasts_activation_callback' );
/**
* Render an admin notice if the plugin cannot load.
*/
function dovetail_podcasts_cannot_load_admin_notice_callback(): void {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
printf(
'<div class="notice notice-error">' .
'<p>%s</p>' .
'</div>',
esc_html__( 'Dovetail Podcasts appears to have been installed without it\'s dependencies. It will not work properly until dependencies are installed. This likely means you have cloned Dovetail Podcasts from Github and need to run the command `composer install`.', 'dovetail-podcasts' )
);
}