Skip to content

Commit

Permalink
chore: update with more addon boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Waldstein committed Oct 4, 2024
1 parent fb220ea commit 99fb51b
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 3 deletions.
3 changes: 3 additions & 0 deletions givewp-cloudflare-turnstile.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
require __DIR__ . '/vendor/autoload.php';
}

// Check to make sure GiveWP core is installed and compatible with this add-on.
add_action('admin_init', [Environment::class, 'checkEnvironment']);

// Register the add-on service provider with the GiveWP core.
add_action(
'before_give_init',
Expand Down
47 changes: 47 additions & 0 deletions src/Addon/ActivationBanner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace GiveCloudflareTurnstile\Addon;

use Give_Addon_Activation_Banner;

use GiveCloudflareTurnstile\Settings\Repositories\GlobalSettings;

use const GIVE_CLOUDFLARE_TURNSTILE_VERSION;

/**
* Helper class responsible for showing add-on Activation Banner.
* @since 1.0.0
*/
class ActivationBanner
{

/**
* Show activation banner
*
* @since 1.0.0
*/
public function show(): void
{
// Check for Activation banner class.
if (!class_exists('Give_Addon_Activation_Banner') && file_exists(
GIVE_PLUGIN_DIR . 'includes/admin/class-addon-activation-banner.php'
)) {
include GIVE_PLUGIN_DIR . 'includes/admin/class-addon-activation-banner.php';
}

// Only runs on admin.
$args = [
'file' => GIVE_CLOUDFLARE_TURNSTILE_FILE,
'name' => GIVE_CLOUDFLARE_TURNSTILE_NAME,
'version' => GIVE_CLOUDFLARE_TURNSTILE_VERSION,
'settings_url' => give(GlobalSettings::class)->getSettingsUrl(),
'documentation_url' => 'https://givewp.com/documentation/add-ons/boilerplate/',
'support_url' => 'https://givewp.com/support/',
'testing' => false, // Never leave true.
];

ray($args);

new Give_Addon_Activation_Banner($args);
}
}
2 changes: 0 additions & 2 deletions src/Addon/Notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace GiveCloudflareTurnstile\Addon;

use Give\Framework\Views\View;

/**
* Helper class responsible for showing add-on notices.
*/
Expand Down
6 changes: 5 additions & 1 deletion src/Addon/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ServiceProvider implements \Give\ServiceProviders\ServiceProvider
*/
public function register()
{
give()->singleton(Links::class);

}

/**
Expand All @@ -25,5 +25,9 @@ public function boot()
{
// Load add-on links.
Hooks::addFilter('plugin_action_links_' . GIVE_CLOUDFLARE_TURNSTILE_BASENAME, Links::class);

if (is_admin()){
Hooks::addAction('admin_init', ActivationBanner::class, 'show', 20);
}
}
}
91 changes: 91 additions & 0 deletions src/Addon/View.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace GiveCloudflareTurnstile\Addon;

use InvalidArgumentException;

use const GIVE_CLOUDFLARE_TURNSTILE_DIR;

/**
* Helper class responsible for loading add-on views.
* @since 1.0.0
*/
class View
{
/**
* Default add-on domain
*/
const DEFAULT_DOMAIN = 'Addon';

/**
* @since 1.0.0
*
* @param array $templateParams Arguments for template.
* @param bool $echo
*
* @param string $view Template name
* When using multiple domains within this add-on, the domain directory can be set by using "." in the template name.
* String before the "." character is domain directory, and everything after is the template file path
* Example usage: View::render( 'DomainName.templateName' );
* This will try to load src/DomainName/resources/view/templateName.php file
*
* @return string|void
* @throws InvalidArgumentException if template file not exist
*
*/
public static function load(string $view, array $templateParams = [], bool $echo = false)
{
// Get domain and file path
list ($domain, $file) = static::getPaths($view);
$template = GIVE_CLOUDFLARE_TURNSTILE_DIR . "src/{$domain}/resources/views/{$file}.php";

if ( ! file_exists($template)) {
throw new InvalidArgumentException("View template file {$template} does not exist");
}

ob_start();
extract($templateParams);
include $template;
$content = ob_get_clean();

if ( ! $echo) {
return $content;
}

echo $content;
}

/**
* @since 1.0.0
*
* @param array $vars
*
* @param string $view
*/
public static function render(string $view, array $vars = []): void
{
static::load($view, $vars, true);
}

/**
* Get domain and template file path
*
* @since 1.0.0
*
* @param string $path
*
* @return array
*/
private static function getPaths(string $path): array
{
// Check for . delimiter
if (false === strpos($path, '.')) {
return [
self::DEFAULT_DOMAIN,
$path,
];
}

return explode('.', $path, 2);
}
}
5 changes: 5 additions & 0 deletions src/Addon/resources/views/admin/notices/give-inactive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php defined( 'ABSPATH' ) or exit; ?>

<strong><?php _e( 'Activation Error:', 'givewp-cloudflare-turnstile' ); ?></strong>
<?php _e( 'You must have', 'givewp-cloudflare-turnstile' ); ?> <a href="https://givewp.com" target="_blank">Give</a>
<?php printf( __( 'plugin installed and activated for the %s add-on to activate', 'givewp-cloudflare-turnstile' ), GIVE_CLOUDFLARE_TURNSTILE_NAME ); ?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php defined( 'ABSPATH' ) or exit; ?>

<strong><?php _e( 'Activation Error:', 'givewp-cloudflare-turnstile' ); ?></strong>
<?php _e( 'You must have', 'givewp-cloudflare-turnstile' ); ?> <a href="https://givewp.com" target="_blank">Give</a>
<?php _e( 'version', 'givewp-cloudflare-turnstile' ); ?> <?php echo GIVE_CLOUDFLARE_TURNSTILE_MIN_GIVE_VERSION; ?>+
<?php printf( esc_html__( 'for the %1$s add-on to activate', 'givewp-cloudflare-turnstile' ), GIVE_CLOUDFLARE_TURNSTILE_NAME ); ?>
.

8 changes: 8 additions & 0 deletions src/Settings/Repositories/GlobalSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ public function isEnabled(): bool
{
return give_is_setting_enabled(give_get_option('givewp_donation_forms_cloudflare_turnstile_enabled', 'disabled'));
}

/**
* @since 1.0.0
*/
public function getSettingsUrl(): ?string
{
return admin_url('edit.php?post_type=give_forms&page=give-settings&tab=advanced&section=cloudflare_turnstile');
}
}

0 comments on commit 99fb51b

Please sign in to comment.