Skip to content

Commit

Permalink
only show button to create optimized endpoint if file location is wri…
Browse files Browse the repository at this point in the history
…table
  • Loading branch information
dannyvankooten committed Oct 30, 2023
1 parent e0e674a commit 260ffb5
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion koko-analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@
}

add_action('widgets_init', 'KokoAnalytics\widgets_init');
add_action('koko_analytics_test_custom_endpoint', 'KokoAnalytics\install_and_test_custom_endpoint');
add_action('koko_analytics_test_custom_endpoint', 'KokoAnalytics\test_custom_endpoint');
8 changes: 7 additions & 1 deletion src/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function init(): void
add_action('admin_menu', array( $this, 'register_menu' ), 10, 0);
add_action('admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 10, 1);
add_action('wp_dashboard_setup', array( $this, 'register_dashboard_widget' ), 10, 0);
add_action('koko_analytics_install_optimized_endpoint', 'KokoAnalytics\install_and_test_custom_endpoint', 10, 0);
add_action('koko_analytics_install_optimized_endpoint', array( $this, 'install_optimized_endpoint' ), 10, 0);
add_action('koko_analytics_save_settings', array( $this, 'save_settings' ), 10, 0);
add_action('koko_analytics_reset_statistics', array( $this, 'reset_statistics' ), 10, 0);

Expand Down Expand Up @@ -351,4 +351,10 @@ private function get_usage_tip(): string
];
return $tips[array_rand($tips)];
}

public function install_optimized_endpoint(): void
{
$installer = new Endpoint_Installer();
$installer->run();
}
}
6 changes: 4 additions & 2 deletions src/class-endpoint-installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

class Endpoint_Installer
{
public function run(): void
public function run(): bool
{
update_option('koko_analytics_use_custom_endpoint', $this->create_and_test(), true);
$successfully_installed = $this->create_and_test();
update_option('koko_analytics_use_custom_endpoint', $successfully_installed, true);
return $successfully_installed;
}

public function verify(): bool
Expand Down
12 changes: 6 additions & 6 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,6 @@ function using_custom_endpoint(): bool
return (bool) get_option('koko_analytics_use_custom_endpoint', false);
}

function install_and_test_custom_endpoint()
{
$endpoint_installer = new Endpoint_Installer();
$endpoint_installer->run();
}

function fmt_large_number(float $number): string
{
if ($number < 10000) {
Expand All @@ -243,3 +237,9 @@ function fmt_large_number(float $number): string
}
return rtrim(rtrim(number_format($number, 1), '0'), '.') . 'K';
}

function test_custom_endpoint(): void
{
$endpoint_installer = new Endpoint_Installer();
$endpoint_installer->verify();
}
8 changes: 5 additions & 3 deletions views/settings-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@
<p><?php esc_html_e('The plugin is currently using an optimized tracking endpoint. Great!', 'koko-analytics'); ?></p>
<?php } else { ?>
<p><?php esc_html_e('The plugin is currently not using an optimized tracking endpoint.', 'koko-analytics'); ?></p>
<?php if (is_writable($endpoint_installer->get_file_name())) { ?>
<form method="POST" action="">
<?php wp_nonce_field('koko_analytics_install_optimized_endpoint'); ?>
<input type="hidden" name="koko_analytics_action" value="install_optimized_endpoint" />
<input type="submit" value="<?php esc_attr_e('Create optimized endpoint file', 'koko-analytics'); ?>" class="button button-secondary" />
<input type="hidden" name="koko_analytics_action" value="install_optimized_endpoint">
<input type="submit" value="<?php esc_attr_e('Create optimized endpoint file', 'koko-analytics'); ?>" class="button button-secondary">
</form>
<p><?php printf(__('Alternatively, create the file %1s with the following file contents: ', 'koko-analytics'), '<code>' . $endpoint_installer->get_file_name() . '</code>'); ?></p>
<?php } ?>
<p><?php printf(__('To use one, create the file %1s with the following file contents: ', 'koko-analytics'), '<code>' . $endpoint_installer->get_file_name() . '</code>'); ?></p>
<textarea readonly="readonly" class="widefat" rows="18" onfocus="this.select();" spellcheck="false"><?php echo esc_html($endpoint_installer->get_file_contents()); ?></textarea>
<p><?php esc_html_e('Please note that this is entirely optional and only recommended for high-traffic websites.', 'koko-analytics'); ?></p>
<?php } ?>
Expand Down

0 comments on commit 260ffb5

Please sign in to comment.