diff --git a/plugin.php b/plugin.php index f655bb6..1a72588 100644 --- a/plugin.php +++ b/plugin.php @@ -3,7 +3,7 @@ * Plugin Name: The Events Calendar: Community Events Extension: reCAPTCHA v3 * Plugin URI: https://theeventscalendar.com/extensions/recaptcha-v3-for-community-events * GitHub Plugin URI: https://github.com/mt-support/tec-labs-ce-recaptcha-v3 - * Description: Add reCAPTCHA v3 support for Community Events + * Description: Add reCAPTCHA v3 support for Community Events. * Version: 1.0.0 * Author: The Events Calendar * Author URI: https://evnt.is/1971 diff --git a/readme.txt b/readme.txt index f80d19a..08229fc 100644 --- a/readme.txt +++ b/readme.txt @@ -2,20 +2,14 @@ Contributors: theeventscalendar Donate link: https://evnt.is/29 Tags: events, calendar -Requires at least: 4.9 -Tested up to: 5.7 -Requires PHP: 5.6 +Requires at least: 6.1.0 +Tested up to: 6.4.1 +Requires PHP: 7.4 Stable tag: 1.0.0 License: GPL version 3 or any later version License URI: https://www.gnu.org/licenses/gpl-3.0.html -Add reCAPTCHA v3 support for Community Events - -== Description == - -This is the long description. No limit, and you can use Markdown (as well as in the following sections). - -For backwards compatibility, if this section is missing, the full length of the short description will be used, and Markdown parsed. +Add reCAPTCHA v3 support for Community Events. == Installation == @@ -37,6 +31,10 @@ We're always interested in your feedback and our [Help Desk](https://support.the == Changelog == -= [1.0.0] YYYY-MM-DD = += [1.0.0] 2023-11-20 = + +* Initial release + += [1.0.0-dev] 2023-11-16 = -* Initial release \ No newline at end of file +* Beta release \ No newline at end of file diff --git a/src/Tec/Plugin.php b/src/Tec/Plugin.php index 76e352c..6c15aae 100644 --- a/src/Tec/Plugin.php +++ b/src/Tec/Plugin.php @@ -70,13 +70,11 @@ class Plugin extends Service_Provider { * @since 1.0.0 * * @var Settings - * - * TODO: Remove if not using settings */ private $settings; /** - * Setup the Extension's properties. + * Set up the Extension's properties. * * This always executes even if the required plugins are not present. * @@ -100,12 +98,14 @@ public function register() { } // Do the settings. - // TODO: Remove if not using settings $this->get_settings(); // Start binds. + // Remove reCAPTCHA v2 + add_filter( 'tribe_community_events_captcha_plugin', '__return_null' ); + $this->maybe_do_recaptcha_v3(); // End binds. @@ -113,6 +113,93 @@ public function register() { $this->container->register( Assets::class ); } + /** + * Replace reCAPTCHA v2 with v3 if there is a license key set. + * + * @return void + * + * @since 1.0.0 + */ + function maybe_do_recaptcha_v3() { + $ce_options = \Tribe__Events__Community__Main::getOptions(); + $option_key = $this->get_options_prefix() . '_site_key'; + $recaptcha_key = $ce_options[ $option_key ]; + + if ( $recaptcha_key !== '' ) { + // Template override for the main templates (in src/views/community). + add_filter( 'tribe_events_template_paths', [ $this, 'template_base_paths' ] ); + + // Template override for module templates (under src/views/community). + add_filter( 'tribe_get_template_part_path', [ $this, 'custom_templates' ], 10, 4 ); + + // Add scripts needed for v3. + add_action( 'wp_head', [ $this, 'add_recaptcha_scripts' ] ); + } + } + + /** + * Add template override location for the main template files. + * (Files in src/views/community.) + * + * @param array $paths The template paths. + * + * @return array The new template paths. + * + * @since 1.0.0 + */ + function template_base_paths( array $paths ): array { + $slug = "tec-labs-" . PUE::get_slug(); + $paths[ $slug ] = trailingslashit( plugin_dir_path( TRIBE_EXTENSION_CE_RECAPTCHA_V3_FILE ) ); + + return $paths; + } + + /** + * Add template override location for template parts. + * (Files under src/views/community.) + * + * @param string $file The template file name with full server path. + * @param string $template The template file name with the default override path. (E.g. community/modules/template.php) + * @param string $slug The template slug. (Same as file name but without extension.) + * @param string|null $name + * + * @return string + * + * @since 1.0.0 + */ + function custom_templates( string $file, string $template, string $slug, ?string $name ): string { + $custom_folder = "src/views"; + + $plugin_path = implode( "", array_merge( + (array) trailingslashit( plugin_dir_path( TRIBE_EXTENSION_CE_RECAPTCHA_V3_FILE ) ), + (array) $custom_folder, + ) ); + + $new_file = $plugin_path . "/" . $template; + + if ( file_exists( $new_file ) ) { + return $new_file; + } + + return $file; + } + + /** + * Add recaptcha scripts to the head. + * + * @return void + * + * @since 1.0.0 + * + * @see https://developers.google.com/recaptcha/docs/v3 + */ + function add_recaptcha_scripts() { + if ( tribe_context()->get('view') == "community_event_page" ) { + echo ''; + echo ''; + } + } + /** * Checks whether the plugin dependency manifest is satisfied or not. * @@ -147,8 +234,6 @@ protected function register_plugin_dependencies() { * @return string * * @see \Tribe\Extensions\CeRecaptchaV3\Settings::set_options_prefix() - * - * TODO: Remove if not using settings */ private function get_options_prefix() { return (string) str_replace( '-', '_', 'tec-labs-ce-recaptcha-v3' ); @@ -158,8 +243,6 @@ private function get_options_prefix() { * Get Settings instance. * * @return Settings - * - * TODO: Remove if not using settings */ private function get_settings() { if ( empty( $this->settings ) ) { @@ -169,32 +252,4 @@ private function get_settings() { return $this->settings; } - /** - * Get all of this extension's options. - * - * @return array - * - * TODO: Remove if not using settings - */ - public function get_all_options() { - $settings = $this->get_settings(); - - return $settings->get_all_options(); - } - - /** - * Get a specific extension option. - * - * @param $option - * @param string $default - * - * @return array - * - * TODO: Remove if not using settings - */ - public function get_option( $option, $default = '' ) { - $settings = $this->get_settings(); - - return $settings->get_option( $option, $default ); - } } diff --git a/src/Tec/Plugin_Register.php b/src/Tec/Plugin_Register.php index 662ad0c..5d6db46 100644 --- a/src/Tec/Plugin_Register.php +++ b/src/Tec/Plugin_Register.php @@ -26,7 +26,7 @@ class Plugin_Register extends Abstract_Plugin_Register { protected $main_class = Plugin::class; protected $dependencies = [ 'parent-dependencies' => [ - 'Tribe__Events__Main' => '6.1.2.2-dev', + 'Tribe__Events__Community__Main' => '4.10.0-dev', ], ]; } diff --git a/src/Tec/Settings.php b/src/Tec/Settings.php index 4464302..a3cdf99 100644 --- a/src/Tec/Settings.php +++ b/src/Tec/Settings.php @@ -12,8 +12,6 @@ /** * Do the Settings. - * - * TODO: Delete file if not using settings */ class Settings { @@ -36,8 +34,6 @@ class Settings { /** * Settings constructor. * - * TODO: Update this entire class for your needs, or remove the entire `src` directory this file is in and do not load it in the main plugin file. - * * @param string $options_prefix Recommended: the plugin text domain, with hyphens converted to underscores. */ public function __construct( $options_prefix ) { @@ -45,10 +41,7 @@ public function __construct( $options_prefix ) { $this->set_options_prefix( $options_prefix ); - // Remove settings specific to Google Maps - add_action( 'admin_init', [ $this, 'remove_settings' ] ); - - // Add settings specific to OSM + // Add settings specific to the extension add_action( 'admin_init', [ $this, 'add_settings' ] ); } @@ -204,50 +197,47 @@ public function delete_option( $key = '' ) { return Tribe__Settings_Manager::set_options( $options ); } - /** - * Here is an example of removing settings from Events > Settings > General tab > "Map Settings" section - * that are specific to Google Maps. - * - * TODO: Remove this method and the corresponding hook in `__construct()` if you don't want to remove any settings. - */ - public function remove_settings() { - // Remove "Enable Google Maps" checkbox - $this->settings_helper->remove_field( 'embedGoogleMaps', 'general' ); - - // Remove "Map view search distance limit" (default of 25) - $this->settings_helper->remove_field( 'geoloc_default_geofence', 'general' ); - - // Remove "Google Maps default zoom level" (0-21, default of 10) - $this->settings_helper->remove_field( 'embedGoogleMapsZoom', 'general' ); - } - /** * Adds a new section of fields to Events > Settings > General tab, appearing after the "Map Settings" section * and before the "Miscellaneous Settings" section. - * - * TODO: Move the setting to where you want and update this docblock. If you like it here, just delete this TODO. */ public function add_settings() { $fields = [ - // TODO: Settings heading start. Remove this element if not needed. Also remove the corresponding `get_example_intro_text()` method below. - 'Example' => [ + 'Intro' => [ 'type' => 'html', - 'html' => $this->get_example_intro_text(), + 'html' => $this->recaptcha_v3_settings_intro_text(), ], - // TODO: Settings heading end. - 'a_setting' => [ // TODO: Change setting. + 'site_key' => [ // TODO: Change setting. 'type' => 'text', - 'label' => esc_html__( 'Example setting', 'tec-labs-ce-recaptcha-v3' ), - 'tooltip' => sprintf( esc_html__( 'Example setting description. Enter your custom URL, including "http://" or "https://", for example %s.', 'tec-labs-ce-recaptcha-v3' ), 'https://demo.theeventscalendar.com/' ), + 'label' => esc_html__( 'Site Key', 'tec-labs-ce-recaptcha-v3' ), + 'tooltip' => sprintf( esc_html__( 'Get your Site Key at %s.', 'tec-labs-ce-recaptcha-v3' ), 'https://www.google.com/recaptcha/admin/create' ), 'validation_type' => 'html', + 'default' => '', + 'can_be_empty' => true, + 'parent_option' => \Tribe__Events__Community__Main::OPTIONNAME, // We're using the Community Events entry, instead of the default TEC. + 'size' => 'large', ], + 'theme' => [ + 'type' => 'dropdown', + 'label' => esc_html__( 'Theme', 'tec-labs-ce-recaptcha-v3' ), + 'tooltip' => esc_html__( 'The color theme of the widget.', 'tec-labs-ce-recaptcha-v3' ), + 'default' => 'light', + 'validation_type' => 'options', + 'size' => 'small', + 'parent_option' => \Tribe__Events__Community__Main::OPTIONNAME, // We're using the Community Events entry, instead of the default TEC. + 'options' => [ + 'light' => esc_html__( 'Light', 'tec-labs-ce-recaptcha-v3' ), + 'dark' => esc_html__( 'Dark', 'tec-labs-ce-recaptcha-v3' ), + ], + ], + ]; $this->settings_helper->add_fields( $this->prefix_settings_field_keys( $fields ), - 'general', - 'tribeEventsMiscellaneousTitle', - true + 'addons', + 'google_maps_js_api_key', + false ); } @@ -272,17 +262,15 @@ function ( $key ) { } /** - * Here is an example of getting some HTML for the Settings Header. - * - * TODO: Delete this method if you do not need a heading for your settings. Also remove the corresponding element in the the $fields array in the `add_settings()` method above. + * HTML for the Settings Header. * * @return string */ - private function get_example_intro_text() { - $result = '

' . esc_html_x( 'Example Extension Setup', 'Settings header', 'tec-labs-ce-recaptcha-v3' ) . '

'; + private function recaptcha_v3_settings_intro_text() { + $result = '

' . esc_html_x( 'reCAPTCHA v3 API Key', 'Settings header', 'tec-labs-ce-recaptcha-v3' ) . '

'; $result .= '
'; $result .= '

'; - $result .= esc_html_x( 'Some text here about this settings section.', 'Setting section description', 'tec-labs-ce-recaptcha-v3' ); + $result .= esc_html_x( 'Provide reCAPTCHA v3 API key to enable reCAPTCHA on your Community Events form.', 'Setting section description', 'tec-labs-ce-recaptcha-v3' ); $result .= '

'; $result .= '
'; diff --git a/src/Tec/Settings_Helper.php b/src/Tec/Settings_Helper.php index 2db93b2..afe8f0d 100644 --- a/src/Tec/Settings_Helper.php +++ b/src/Tec/Settings_Helper.php @@ -13,8 +13,6 @@ /** * Helper for inserting/removing fields on the WP Admin TEC Settings pages - * - * @TODO Delete file if not using settings */ class Settings_Helper { diff --git a/src/views/community/edit-event.php b/src/views/community/edit-event.php new file mode 100644 index 0000000..3dee281 --- /dev/null +++ b/src/views/community/edit-event.php @@ -0,0 +1,113 @@ +getOption( 'termsEnabled' ); + +$terms_description = $main->getOption( 'termsDescription' ); + +// Variables for the Website view. +$event_url = $_POST['EventURL'] ?? tribe_get_event_website_url(); +$event_url = esc_attr( $event_url ); + + +?> + + + + + +
+ + + + $events_label_singular ] ); ?> + + + + + + + + Tribe__Events__Main::TAXONOMY ] ); ?> + + 'post_tag' ] ); ?> + + + + + + + + + + $event_url ] ); ?> + + + + + + + + + + $terms_enabled, 'terms_description' => $terms_description ] ); ?> + + +
+ + diff --git a/src/views/community/modules/captcha.php b/src/views/community/modules/captcha.php new file mode 100644 index 0000000..3044bee --- /dev/null +++ b/src/views/community/modules/captcha.php @@ -0,0 +1,14 @@ + diff --git a/src/views/community/modules/submit.php b/src/views/community/modules/submit.php new file mode 100644 index 0000000..43ae053 --- /dev/null +++ b/src/views/community/modules/submit.php @@ -0,0 +1,64 @@ +getOption( 'recaptchaPublicKey', '' ); + +if ( tribe_is_event( $post_id ) ) { + $button_label = sprintf( __( 'Update %s', 'tribe-events-community' ), $events_label_singular ); +} else { + $button_label = sprintf( __( 'Submit %s', 'tribe-events-community' ), $events_label_singular ); +} +$button_label = apply_filters( 'tribe_community_event_edit_button_label', $button_label ); + +$recaptcha_theme = tribe( 'community.main' )->getOption( 'tec_labs_ce_recaptcha_v3_theme', 'light' ); +?> + + + +