| 1 |
<?php |
| 2 |
/** |
| 3 |
* Shared plugin runtime. |
| 4 |
* |
| 5 |
* @package WPGO_Content_Censor |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPGO\Content_Censor; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Registers shared WordPress hooks and delegates text replacement to the engine. |
| 16 |
*/ |
| 17 |
final class Plugin { |
| 18 |
|
| 19 |
/** |
| 20 |
* Whether licensed premium code is available. |
| 21 |
* |
| 22 |
* @var bool |
| 23 |
*/ |
| 24 |
private static $premium_available = false; |
| 25 |
|
| 26 |
/** |
| 27 |
* Register lifecycle hooks. |
| 28 |
* |
| 29 |
* @param bool $premium_available Whether premium source is loaded and licensed. |
| 30 |
* @return void |
| 31 |
*/ |
| 32 |
public static function boot( $premium_available ) { |
| 33 |
self::$premium_available = (bool) $premium_available; |
| 34 |
add_action( 'plugins_loaded', array( 'WPGO\\Content_Censor\\Migrator', 'maybe_upgrade' ), 1 ); |
| 35 |
add_action( 'plugins_loaded', array( __CLASS__, 'initialize' ), 5 ); |
| 36 |
add_filter( 'plugin_action_links_' . plugin_basename( WPGO_CONTENT_CENSOR_FILE ), array( __CLASS__, 'action_links' ) ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Initialize settings and public filters. |
| 41 |
* |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
public static function initialize() { |
| 45 |
load_plugin_textdomain( 'wp-content-filter', false, dirname( plugin_basename( WPGO_CONTENT_CENSOR_FILE ) ) . '/languages' ); |
| 46 |
Settings::boot(); |
| 47 |
|
| 48 |
if ( ! class_exists( 'WPGO_Content_Censor_Current', false ) ) { |
| 49 |
class_alias( __CLASS__, 'WPGO_Content_Censor_Current' ); |
| 50 |
} |
| 51 |
|
| 52 |
if ( self::$premium_available && class_exists( 'WPGO\\Content_Censor\\Premium' ) ) { |
| 53 |
Premium::boot(); |
| 54 |
} |
| 55 |
|
| 56 |
if ( is_admin() ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
self::register_public_filters( Settings::get() ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Register shared frontend filter hooks from current settings. |
| 65 |
* |
| 66 |
* @param array<string, string> $options Plugin settings. |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
public static function register_public_filters( array $options ) { |
| 70 |
$callback = array( __CLASS__, 'filter' ); |
| 71 |
if ( '1' === $options['chk_post_content'] ) { |
| 72 |
add_filter( 'the_content', $callback ); |
| 73 |
add_filter( 'get_the_excerpt', $callback ); |
| 74 |
if ( class_exists( 'bbPress' ) ) { |
| 75 |
add_filter( 'bbp_get_topic_content', $callback ); |
| 76 |
add_filter( 'bbp_get_reply_content', $callback ); |
| 77 |
} |
| 78 |
} |
| 79 |
if ( '1' === $options['chk_post_title'] ) { |
| 80 |
add_filter( 'the_title', $callback ); |
| 81 |
} |
| 82 |
if ( '1' === $options['chk_comments'] ) { |
| 83 |
add_filter( 'comment_text', $callback ); |
| 84 |
add_filter( 'get_comment_author', $callback ); |
| 85 |
} |
| 86 |
if ( '1' === $options['chk_tags'] ) { |
| 87 |
add_filter( 'term_links-post_tag', $callback ); |
| 88 |
} |
| 89 |
if ( '1' === $options['chk_tag_cloud'] ) { |
| 90 |
add_filter( 'wp_tag_cloud', $callback ); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Filter a string or string array using the current settings. |
| 96 |
* |
| 97 |
* @param mixed $value Filter value. |
| 98 |
* @return mixed |
| 99 |
*/ |
| 100 |
public static function filter( $value ) { |
| 101 |
global $post; |
| 102 |
$post_id = isset( $post->ID ) ? (int) $post->ID : 0; |
| 103 |
return Filter_Engine::filter( $value, Settings::get(), $post_id ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Add a direct settings link on the Plugins screen. |
| 108 |
* |
| 109 |
* @param array<int, string> $links Existing links. |
| 110 |
* @return array<int, string> |
| 111 |
*/ |
| 112 |
public static function action_links( array $links ) { |
| 113 |
$url = add_query_arg( 'page', Settings::PAGE_SLUG, admin_url( 'options-general.php' ) ); |
| 114 |
array_unshift( $links, '<a href="' . esc_url( $url ) . '">' . esc_html__( 'Settings', 'wp-content-filter' ) . '</a>' ); |
| 115 |
return $links; |
| 116 |
} |
| 117 |
} |
| 118 |
|