Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial #1

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 10 additions & 12 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ==

Expand All @@ -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
* Beta release
127 changes: 91 additions & 36 deletions src/Tec/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -100,19 +98,108 @@ 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' );
andrasguseo marked this conversation as resolved.
Show resolved Hide resolved

$this->maybe_do_recaptcha_v3();

// End binds.

$this->container->register( Hooks::class );
$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];
andrasguseo marked this conversation as resolved.
Show resolved Hide resolved

if ( $recaptcha_key != '' ) {
andrasguseo marked this conversation as resolved.
Show resolved Hide resolved
// 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();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd try to centralize a place for the slug, for maintenance and clarity purposes. We currently have it in the Plugin and PUE classes (for the latter, with the extension- prefix). I think for the template path it would be more friendly to use the plugin slug, but that's a personal preference. PUE is oriented to the update engine.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bordoni just FYI. What do you think about this? In general, for the extensions?

$paths[$slug] = trailingslashit( plugin_dir_path( TRIBE_EXTENSION_CE_RECAPTCHA_V3_FILE ) );
andrasguseo marked this conversation as resolved.
Show resolved Hide resolved

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 '<script src="https://www.google.com/recaptcha/api.js"></script>';
echo '<script>function onSubmit(token) { document.getElementById("tec_ce_submission_form").submit(); }</script>';
}
}

/**
* Checks whether the plugin dependency manifest is satisfied or not.
*
Expand Down Expand Up @@ -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' );
Expand All @@ -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 ) ) {
Expand All @@ -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 );
}
}
2 changes: 1 addition & 1 deletion src/Tec/Plugin_Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
];
}
74 changes: 31 additions & 43 deletions src/Tec/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

/**
* Do the Settings.
*
* TODO: Delete file if not using settings
*/
class Settings {

Expand All @@ -36,19 +34,14 @@ 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 ) {
$this->settings_helper = new Settings_Helper();

$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' ] );
}

Expand Down Expand Up @@ -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' ), '<code>https://demo.theeventscalendar.com/</code>' ),
'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' ), '<a href="https://www.Google.com/recaptcha/admin/create" target="_blank">https://www.google.com/recaptcha/admin/create</a>' ),
'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
);
}

Expand All @@ -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 = '<h3>' . esc_html_x( 'Example Extension Setup', 'Settings header', 'tec-labs-ce-recaptcha-v3' ) . '</h3>';
private function recaptcha_v3_settings_intro_text() {
$result = '<h3>' . esc_html_x( 'reCAPTCHA v3 API Key', 'Settings header', 'tec-labs-ce-recaptcha-v3' ) . '</h3>';
$result .= '<div style="margin-left: 20px;">';
$result .= '<p>';
$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 .= '</p>';
$result .= '</div>';

Expand Down
2 changes: 0 additions & 2 deletions src/Tec/Settings_Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Loading