| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Blocks for ACF Fields — Display Custom Fields in the Block Editor |
| 4 |
* Plugin URI: https://www.acffieldblocks.com |
| 5 |
* Description: The easiest way to display ACF fields in the WordPress block editor — no coding required! |
| 6 |
* Requires at least: 6.5 |
| 7 |
* Tested up to: 7.1 |
| 8 |
* Requires PHP: 7.4 |
| 9 |
* Version: 1.6.5 |
| 10 |
* Author: gamaup |
| 11 |
* License: GPL-2.0-or-later |
| 12 |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 13 |
* Text Domain: acf-field-blocks |
| 14 |
*/ |
| 15 |
|
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
if ( ! class_exists( 'ACF_Field_Blocks' ) ) { |
| 23 |
|
| 24 |
/** |
| 25 |
* Main Class |
| 26 |
*/ |
| 27 |
class ACF_Field_Blocks { |
| 28 |
|
| 29 |
/** |
| 30 |
* Class instance |
| 31 |
* |
| 32 |
* @var ACF_Field_Blocks |
| 33 |
*/ |
| 34 |
private static $instance; |
| 35 |
|
| 36 |
/** |
| 37 |
* Initiator |
| 38 |
* |
| 39 |
* @return ACF_Field_Blocks() |
| 40 |
*/ |
| 41 |
public static function get_instance() { |
| 42 |
if ( ! isset( self::$instance ) ) { |
| 43 |
self::$instance = new self(); |
| 44 |
do_action( 'acf_field_blocks_loaded' ); |
| 45 |
} |
| 46 |
return self::$instance; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Constructor |
| 51 |
*/ |
| 52 |
public function __construct() { |
| 53 |
if ( ! version_compare( get_bloginfo( 'version' ), '6.3', '>=' ) ) { |
| 54 |
add_action( 'admin_notices', [ $this, 'fail_wp_version' ] ); |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
add_action( 'after_setup_theme', function() { |
| 59 |
if ( ! class_exists( 'ACF' ) || ! version_compare( acf()->version, '6.1.0', '>=' ) ) { |
| 60 |
add_action( 'admin_notices', [ $this, 'fail_acf_required' ] ); |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
add_filter( 'network_admin_plugin_action_links_acf-field-blocks/acf-field-blocks.php', array( $this, 'filter_plugin_action_links' ) ); |
| 65 |
add_filter( 'plugin_action_links_acf-field-blocks/acf-field-blocks.php', array( $this, 'filter_plugin_action_links' ) ); |
| 66 |
add_action( 'activated_plugin', array( $this, 'deactivate_other_instances' ) ); |
| 67 |
add_action( 'pre_current_active_plugins', array( $this, 'plugin_deactivated_notice' ) ); |
| 68 |
add_action( 'admin_init', array( $this, 'handle_admin_actions' ) ); |
| 69 |
|
| 70 |
$this->define_constants(); |
| 71 |
$this->autoload(); |
| 72 |
}); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Define all constants |
| 77 |
*/ |
| 78 |
public function define_constants() { |
| 79 |
define( 'ACF_FIELD_BLOCKS_VERSION', '1.6.5' ); |
| 80 |
define( 'ACF_FIELD_BLOCKS_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) ); |
| 81 |
define( 'ACF_FIELD_BLOCKS_URL', plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Load autoloader and register the namespace |
| 86 |
*/ |
| 87 |
public function autoload() { |
| 88 |
require_once ACF_FIELD_BLOCKS_PATH . '/autoloader.php'; |
| 89 |
$autoloader = new \ACFFieldBlocks\Autoloader(); |
| 90 |
$autoloader->add_namespace( '\ACFFieldBlocks', ACF_FIELD_BLOCKS_PATH . '/inc/' ); |
| 91 |
$autoloader->add_namespace( '\ACFFieldBlocks\Pro', ACF_FIELD_BLOCKS_PATH . '/pro/' ); |
| 92 |
$autoloader->register(); |
| 93 |
|
| 94 |
\ACFFieldBlocks\Blocks::instance(); |
| 95 |
if ( is_dir( ACF_FIELD_BLOCKS_PATH . '/pro/' ) ) { |
| 96 |
\ACFFieldBlocks\Pro\Blocks::instance(); |
| 97 |
\ACFFieldBlocks\Pro\Rest::instance(); |
| 98 |
\ACFFieldBlocks\Pro\Block_Visibility::instance(); |
| 99 |
} else { |
| 100 |
add_action( 'admin_notices', array( $this, 'review_notice' ) ); |
| 101 |
} |
| 102 |
\ACFFieldBlocks\Rest::instance(); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Handle admin actions |
| 107 |
*/ |
| 108 |
public function handle_admin_actions() { |
| 109 |
if ( isset( $_GET['afb_action'] ) && ! empty( $_GET['afb_action'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['afb_action'] ) ), 'hide_review_notice' ) ) { |
| 110 |
update_option( 'acf_field_blocks_hide_review_notice', true ); |
| 111 |
wp_safe_redirect( admin_url() ); |
| 112 |
exit; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Show review notice. |
| 118 |
*/ |
| 119 |
public function review_notice() { |
| 120 |
$activation_date = get_option( 'acf_field_blocks_first_activate' ); |
| 121 |
$is_hide = get_option( 'acf_field_blocks_hide_review_notice', false ); |
| 122 |
if ( ! empty( $activation_date ) && time() > ( intval( $activation_date ) + ( 2 * WEEK_IN_SECONDS ) ) && false === $is_hide ) { |
| 123 |
?> |
| 124 |
<div class="notice notice-info is-dismissible"> |
| 125 |
<?php /* translators: %s: Plugin name. */ ?> |
| 126 |
<p style="margin-bottom: 0;"><?php printf( esc_html__( "Enjoying %s? You've been using this plugin for a while.", "acf-field-blocks" ), "<strong>Blocks for ACF Fields</strong>" ); ?></p> |
| 127 |
<p style="margin-top: 0;"><?php esc_html_e( 'If you could take a few moments to rate it on WordPress.org, we would really appreciate your help making the plugin better. Thanks!', 'acf-field-blocks' ); ?></p> |
| 128 |
<div style="margin-bottom: 1em;"> |
| 129 |
<a target="_blank" href="https://wordpress.org/support/plugin/acf-field-blocks/reviews/#new-post" class="button-primary"><?php esc_html_e( 'Drop a Review', 'acf-field-blocks' ); ?></a> |
| 130 |
<a href="<?php echo esc_url( wp_nonce_url( admin_url(), 'hide_review_notice', 'afb_action' ) ); ?>" class="button-secondary"><?php esc_html_e( "Don't show again", 'acf-field-blocks' ); ?></a> |
| 131 |
</div> |
| 132 |
</div> |
| 133 |
<?php |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Warn user when the site doesn't have the minimum required WordPress version. |
| 139 |
*/ |
| 140 |
public function fail_wp_version() { |
| 141 |
/* translators: %s: WordPress version */ |
| 142 |
$message = sprintf( esc_html__( 'Blocks for ACF Fields requires WordPress version %s+. Because you are using an earlier version, the plugin is currently not running.', 'acf-field-blocks' ), '6.5' ); |
| 143 |
$html_message = sprintf( '<div class="error">%s</div>', wpautop( $message ) ); |
| 144 |
echo wp_kses_post( $html_message ); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Warn user when the site doesn't have the minimum required WordPress version. |
| 149 |
*/ |
| 150 |
public function fail_acf_required() { |
| 151 |
/* translators: %s: WordPress version */ |
| 152 |
$message = sprintf( esc_html__( 'Blocks for ACF Fields requires the Advanced Custom Fields plugin version %s+ to be activated. The plugin is currently not running.', 'acf-field-blocks' ), '6.1.0' ); |
| 153 |
$html_message = sprintf( '<div class="error">%s</div>', wpautop( $message ) ); |
| 154 |
echo wp_kses_post( $html_message ); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* @param array<string, string> $actions |
| 159 |
* @return array<string, string> |
| 160 |
*/ |
| 161 |
public function filter_plugin_action_links( array $actions ) { |
| 162 |
return array_merge( array( |
| 163 |
'upgrade' => '<a href="http://www.acffieldblocks.com/pro/?utm_source=usersite&utm_medium=wp%20admin%20plugins&utm_campaign=BlocksforACFFields%20Pro%20Upgrade">' . esc_html__( 'Upgrade to PRO', 'acf-field-blocks' ) . '</a>', |
| 164 |
), $actions ); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Checks if another version of Blocks for ACF Fields/Blocks for ACF Fields PRO is active and deactivates it. |
| 169 |
* Hooked on `activated_plugin` so other plugin is deactivated when current plugin is activated. |
| 170 |
* |
| 171 |
* @param string $plugin The plugin being activated. |
| 172 |
*/ |
| 173 |
public function deactivate_other_instances( $plugin ) { |
| 174 |
if ( ! in_array( $plugin, array( 'acf-field-blocks/acf-field-blocks.php', 'acf-field-blocks-pro/acf-field-blocks-pro.php' ), true ) ) { |
| 175 |
return; |
| 176 |
} |
| 177 |
|
| 178 |
$plugin_to_deactivate = 'acf-field-blocks/acf-field-blocks.php'; |
| 179 |
$deactivated_notice_id = '1'; |
| 180 |
|
| 181 |
// If we just activated the free version, deactivate the pro version. |
| 182 |
if ( $plugin === $plugin_to_deactivate ) { |
| 183 |
$plugin_to_deactivate = 'acf-field-blocks-pro/acf-field-blocks-pro.php'; |
| 184 |
$deactivated_notice_id = '2'; |
| 185 |
} |
| 186 |
|
| 187 |
if ( is_multisite() && is_network_admin() ) { |
| 188 |
$active_plugins = (array) get_site_option( 'active_sitewide_plugins', array() ); |
| 189 |
$active_plugins = array_keys( $active_plugins ); |
| 190 |
} else { |
| 191 |
$active_plugins = (array) get_option( 'active_plugins', array() ); |
| 192 |
} |
| 193 |
|
| 194 |
foreach ( $active_plugins as $plugin_basename ) { |
| 195 |
if ( $plugin_to_deactivate === $plugin_basename ) { |
| 196 |
set_transient( 'acffieldblocks_deactivated_notice_id', $deactivated_notice_id, 1 * HOUR_IN_SECONDS ); |
| 197 |
deactivate_plugins( $plugin_basename ); |
| 198 |
return; |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Displays a notice when either ACF or ACF PRO is automatically deactivated. |
| 205 |
*/ |
| 206 |
public function plugin_deactivated_notice() { |
| 207 |
$deactivated_notice_id = (int) get_transient( 'acffieldblocks_deactivated_notice_id' ); |
| 208 |
if ( ! in_array( $deactivated_notice_id, array( 1, 2 ), true ) ) { |
| 209 |
return; |
| 210 |
} |
| 211 |
|
| 212 |
$message = __( "Blocks for ACF Fields and Blocks for ACF Fields PRO should not be active at the same time. We've automatically deactivated Blocks for ACF Fields.", 'acf-field-blocks' ); |
| 213 |
if ( 2 === $deactivated_notice_id ) { |
| 214 |
$message = __( "Blocks for ACF Fields and Blocks for ACF Fields PRO should not be active at the same time. We've automatically deactivated Blocks for ACF Fields PRO.", 'acf-field-blocks' ); |
| 215 |
} |
| 216 |
|
| 217 |
?> |
| 218 |
<div class="updated" style="border-left: 4px solid #ffba00;"> |
| 219 |
<p><?php echo esc_html( $message ); ?></p> |
| 220 |
</div> |
| 221 |
<?php |
| 222 |
|
| 223 |
delete_transient( 'acffieldblocks_deactivated_notice_id' ); |
| 224 |
} |
| 225 |
|
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
add_action( 'plugins_loaded', function() { |
| 230 |
ACF_Field_Blocks::get_instance(); |
| 231 |
} ); |
| 232 |
|
| 233 |
function acf_field_blocks() { |
| 234 |
return ACF_Field_Blocks::get_instance(); |
| 235 |
} |
| 236 |
|
| 237 |
register_activation_hook( __FILE__, function() { |
| 238 |
$first_activate = get_option( 'acf_field_blocks_first_activate' ); |
| 239 |
if ( empty( $first_activate ) ) { |
| 240 |
update_option( 'acf_field_blocks_first_activate', time(), 'no' ); |
| 241 |
} |
| 242 |
} ); |