From 5e54a11709233305ec52a649d6ff21d513f4b58c Mon Sep 17 00:00:00 2001 From: Leon Stafford Date: Thu, 29 Dec 2016 16:20:49 +1300 Subject: [PATCH] WIP separate ajaxy generation function --- library/StaticHtmlOutput.php | 50 ++++++++++++++++++++++++------------ views/options-page.phtml | 19 ++++++++------ wp-static-html-output.php | 9 +++++++ 3 files changed, 54 insertions(+), 24 deletions(-) diff --git a/library/StaticHtmlOutput.php b/library/StaticHtmlOutput.php index 32ab725b..950e57aa 100644 --- a/library/StaticHtmlOutput.php +++ b/library/StaticHtmlOutput.php @@ -132,7 +132,13 @@ class StaticHtmlOutput */ public function registerOptionsPage() { + /* + add_submenu_page( string $parent_slug, string $page_title, string $menu_title, string $capability, string $menu_slug, callable $function = '' ) + */ $page = add_submenu_page('tools.php', __('WP Static HTML Output', 'static-html-output-plugin'), __('WP Static HTML Output', 'static-html-output-plugin'), 'manage_options', self::HOOK . '-options', array($this, 'renderOptionsPage')); + /* + add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 ) + */ add_action('admin_print_styles-' . $page, array($this, 'enqueueAdminStyles')); } @@ -172,20 +178,21 @@ class StaticHtmlOutput else { do_action(self::HOOK . '-saveOptions'); - - $this->_view - ->setTemplate('options-page') - ->assign('exportLog', $this->_exportLog) - ->assign('baseUrl', $this->_options->getOption('baseUrl')) - ->assign('additionalUrls', $this->_options->getOption('additionalUrls')) - ->assign('cleanMeta', $this->_options->getOption('cleanMeta')) - ->assign('retainStaticFiles', $this->_options->getOption('retainStaticFiles')) - ->assign('sendViaFTP', $this->_options->getOption('sendViaFTP')) - ->assign('ftpServer', $this->_options->getOption('ftpServer')) - ->assign('ftpUsername', $this->_options->getOption('ftpUsername')) - ->assign('ftpRemotePath', $this->_options->getOption('ftpRemotePath')) - ->assign('onceAction', self::HOOK . '-options') - ->render(); + + $this->_view + ->setTemplate('options-page') + ->assign('exportLog', $this->_exportLog) + ->assign('baseUrl', $this->_options->getOption('baseUrl')) + ->assign('additionalUrls', $this->_options->getOption('additionalUrls')) + ->assign('cleanMeta', $this->_options->getOption('cleanMeta')) + ->assign('retainStaticFiles', $this->_options->getOption('retainStaticFiles')) + ->assign('sendViaFTP', $this->_options->getOption('sendViaFTP')) + ->assign('ftpServer', $this->_options->getOption('ftpServer')) + ->assign('ftpUsername', $this->_options->getOption('ftpUsername')) + ->assign('ftpRemotePath', $this->_options->getOption('ftpRemotePath')) + ->assign('onceAction', self::HOOK . '-options') + ->render(); + } } @@ -223,9 +230,18 @@ class StaticHtmlOutput $this->_view->setTemplate('message') ->assign('message', $message) ->render(); + } -/* + public function genArch() + { + // Protection + if (!isset($_POST['action']) || 'genArchive' != $_POST['action']) + { + return; + } + + // Generate archive $archiveUrl = $this->_generateArchive(); @@ -243,11 +259,13 @@ class StaticHtmlOutput } } + echo 'Archive has been created... log...'; +/* $this->_view->setTemplate('message') ->assign('message', $message) ->render(); */ - } + } /** * Generates ZIP archive diff --git a/views/options-page.phtml b/views/options-page.phtml index ab3744a7..5ced0b31 100644 --- a/views/options-page.phtml +++ b/views/options-page.phtml @@ -10,14 +10,18 @@ jQuery(document).ready(function($){ $('#generateArchiveButton').click(function() { console.log('Generating archive...'); - $.ajax({ - type: "POST", - url: '', - data: $('#general-options').serialize(), - success: function(response) { + + // set form action for generation vs saving options + $('#formActionHiddenField').val('my_action') + + data = $('#general-options').serialize(); + + jQuery.post(ajaxurl, data, function(response) { $('#results').html(response); - } }); + + // revert form action for saving options vs generation + $('#formActionHiddenField').val('generate') }); }); @@ -98,7 +102,7 @@ jQuery(document).ready(function($){

onceAction) ?> - +

@@ -121,7 +125,6 @@ jQuery(document).ready(function($){

onceAction) ?> -

diff --git a/wp-static-html-output.php b/wp-static-html-output.php index c01fc6d2..013b11d7 100644 --- a/wp-static-html-output.php +++ b/wp-static-html-output.php @@ -42,3 +42,12 @@ function myplugin_init() { load_plugin_textdomain( 'static-html-output-plugin', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); } add_action('plugins_loaded', 'myplugin_init'); + +add_action( 'wp_ajax_my_action', 'my_action_callback' ); + +function my_action_callback() { + + echo 'some response this is!'; + + wp_die(); // this is required to terminate immediately and return a proper response +}