| 1 |
<?php |
| 2 |
/** |
| 3 |
* Main plugin class file. |
| 4 |
*/ |
| 5 |
|
| 6 |
// Exit if accessed directly. |
| 7 |
if ( !defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
|
| 12 |
/** |
| 13 |
* Main plugin class. |
| 14 |
*/ |
| 15 |
class BLNOTIFIER_LOADER { |
| 16 |
|
| 17 |
/** |
| 18 |
* Define your custom capability |
| 19 |
*/ |
| 20 |
public $capability = 'manage_broken_links'; |
| 21 |
|
| 22 |
|
| 23 |
/** |
| 24 |
* Define your custom role slug |
| 25 |
*/ |
| 26 |
public $role_slug = 'blnotifier_link_manager'; |
| 27 |
|
| 28 |
|
| 29 |
/** |
| 30 |
* Constructor |
| 31 |
*/ |
| 32 |
public function __construct() { |
| 33 |
|
| 34 |
// Load dependencies. |
| 35 |
if ( is_admin() ) { |
| 36 |
$this->load_admin_dependencies(); |
| 37 |
} |
| 38 |
$this->load_dependencies(); |
| 39 |
|
| 40 |
// Add custom capability to the admin |
| 41 |
add_action( 'admin_init', [ $this, 'add_custom_capability_to_admin' ] ); |
| 42 |
|
| 43 |
} // End __construct() |
| 44 |
|
| 45 |
|
| 46 |
/** |
| 47 |
* Admin-only dependencies |
| 48 |
* |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public function load_admin_dependencies() { |
| 52 |
|
| 53 |
// Add a settings link to plugins list page |
| 54 |
add_filter( 'plugin_action_links_'.BLNOTIFIER_TEXTDOMAIN.'/'.BLNOTIFIER_TEXTDOMAIN.'.php', [ $this, 'settings_link' ] ); |
| 55 |
|
| 56 |
// Add links to the website and discord |
| 57 |
add_filter( 'plugin_row_meta', [ $this, 'plugin_row_meta' ], 10, 2 ); |
| 58 |
|
| 59 |
// Requires |
| 60 |
require_once BLNOTIFIER_PLUGIN_INCLUDES_PATH.'scan.php'; |
| 61 |
require_once BLNOTIFIER_PLUGIN_INCLUDES_PATH.'scan-multi.php'; |
| 62 |
|
| 63 |
} // End load_admin_dependencies() |
| 64 |
|
| 65 |
|
| 66 |
/** |
| 67 |
* Add a settings link to plugins list page |
| 68 |
* |
| 69 |
* @param array $links |
| 70 |
* @return array |
| 71 |
*/ |
| 72 |
public function settings_link( $links ) { |
| 73 |
array_unshift( |
| 74 |
$links, |
| 75 |
'<a href="'.(new BLNOTIFIER_MENU)->get_plugin_page().'">' . __( 'Settings', 'broken-link-notifier' ) . '</a>' |
| 76 |
); |
| 77 |
return $links; |
| 78 |
} // End settings_link() |
| 79 |
|
| 80 |
|
| 81 |
/** |
| 82 |
* Add link to our website to plugin page |
| 83 |
* |
| 84 |
* @param array $links |
| 85 |
* @param string $file |
| 86 |
* @return array |
| 87 |
*/ |
| 88 |
public function plugin_row_meta( $links, $file ) { |
| 89 |
$text_domain = BLNOTIFIER_TEXTDOMAIN; |
| 90 |
if ( $text_domain . '/' . $text_domain . '.php' == $file ) { |
| 91 |
|
| 92 |
$guide_url = BLNOTIFIER_GUIDE_URL; |
| 93 |
$docs_url = BLNOTIFIER_DOCS_URL; |
| 94 |
$support_url = BLNOTIFIER_SUPPORT_URL; |
| 95 |
$plugin_name = BLNOTIFIER_NAME; |
| 96 |
|
| 97 |
$our_links = [ |
| 98 |
'guide' => [ |
| 99 |
// translators: Link label for the plugin's user-facing guide. |
| 100 |
'label' => __( 'How-To Guide', 'broken-link-notifier' ), |
| 101 |
'url' => $guide_url |
| 102 |
], |
| 103 |
'docs' => [ |
| 104 |
// translators: Link label for the plugin's developer documentation. |
| 105 |
'label' => __( 'Developer Docs', 'broken-link-notifier' ), |
| 106 |
'url' => $docs_url |
| 107 |
], |
| 108 |
'support' => [ |
| 109 |
// translators: Link label for the plugin's support page. |
| 110 |
'label' => __( 'Support', 'broken-link-notifier' ), |
| 111 |
'url' => $support_url |
| 112 |
], |
| 113 |
]; |
| 114 |
|
| 115 |
$row_meta = []; |
| 116 |
foreach ( $our_links as $key => $link ) { |
| 117 |
// translators: %1$s is the link label, %2$s is the plugin name. |
| 118 |
$aria_label = sprintf( __( '%1$s for %2$s', 'broken-link-notifier' ), $link[ 'label' ], $plugin_name ); |
| 119 |
$row_meta[ $key ] = '<a href="' . esc_url( $link[ 'url' ] ) . '" target="_blank" aria-label="' . esc_attr( $aria_label ) . '">' . esc_html( $link[ 'label' ] ) . '</a>'; |
| 120 |
} |
| 121 |
|
| 122 |
// Add the links |
| 123 |
return array_merge( $links, $row_meta ); |
| 124 |
} |
| 125 |
|
| 126 |
// Return the links |
| 127 |
return (array) $links; |
| 128 |
} // End plugin_row_meta() |
| 129 |
|
| 130 |
|
| 131 |
/** |
| 132 |
* Front-end dependencies |
| 133 |
* |
| 134 |
* @return void |
| 135 |
*/ |
| 136 |
public function load_dependencies() { |
| 137 |
|
| 138 |
// Requires |
| 139 |
require_once BLNOTIFIER_PLUGIN_INCLUDES_PATH.'menu.php'; |
| 140 |
require_once BLNOTIFIER_PLUGIN_INCLUDES_PATH.'cache.php'; |
| 141 |
require_once BLNOTIFIER_PLUGIN_INCLUDES_PATH.'helpers.php'; |
| 142 |
require_once BLNOTIFIER_PLUGIN_INCLUDES_PATH.'omits.php'; |
| 143 |
require_once BLNOTIFIER_PLUGIN_INCLUDES_PATH.'discord.php'; |
| 144 |
require_once BLNOTIFIER_PLUGIN_INCLUDES_PATH.'slack.php'; |
| 145 |
require_once BLNOTIFIER_PLUGIN_INCLUDES_PATH.'msteams.php'; |
| 146 |
require_once BLNOTIFIER_PLUGIN_INCLUDES_PATH.'results.php'; |
| 147 |
require_once BLNOTIFIER_PLUGIN_INCLUDES_PATH.'integrations.php'; |
| 148 |
require_once BLNOTIFIER_PLUGIN_INCLUDES_PATH.'export.php'; |
| 149 |
require_once BLNOTIFIER_PLUGIN_INCLUDES_PATH.'api.php'; |
| 150 |
|
| 151 |
} // End load_dependencies() |
| 152 |
|
| 153 |
|
| 154 |
/** |
| 155 |
* Plugin activation hook callback |
| 156 |
*/ |
| 157 |
public function plugin_activate() { |
| 158 |
$admin_role = get_role( 'administrator' ); |
| 159 |
if ( $admin_role ) { |
| 160 |
$admin_role->add_cap( $this->capability ); |
| 161 |
} |
| 162 |
|
| 163 |
add_role( |
| 164 |
$this->role_slug, |
| 165 |
__( 'Broken Link Manager', 'broken-link-notifier' ), |
| 166 |
[ |
| 167 |
'read' => true, |
| 168 |
$this->capability => true, |
| 169 |
] |
| 170 |
); |
| 171 |
} // End plugin_activate() |
| 172 |
|
| 173 |
|
| 174 |
/** |
| 175 |
* Plugin deactivation hook callback |
| 176 |
*/ |
| 177 |
public function plugin_deactivate() { |
| 178 |
$admin_role = get_role( 'administrator' ); |
| 179 |
if ( $admin_role ) { |
| 180 |
$admin_role->remove_cap( $this->capability ); |
| 181 |
} |
| 182 |
|
| 183 |
remove_role( $this->role_slug ); |
| 184 |
} // End plugin_deactivate() |
| 185 |
|
| 186 |
|
| 187 |
/** |
| 188 |
* Ensures the custom capability exists for administrators on admin_init |
| 189 |
*/ |
| 190 |
public function add_custom_capability_to_admin() { |
| 191 |
$role = get_role( 'administrator' ); |
| 192 |
if ( $role && !$role->has_cap( $this->capability ) ) { |
| 193 |
$role->add_cap( $this->capability ); |
| 194 |
} |
| 195 |
} // End add_custom_capability_to_admin() |
| 196 |
|
| 197 |
} |