Skip to content

Commit

Permalink
Fix many bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
scottyzen committed Mar 2, 2023
1 parent 9d66069 commit 67c4535
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 165 deletions.
173 changes: 79 additions & 94 deletions assets/admin.js
Original file line number Diff line number Diff line change
@@ -1,103 +1,88 @@
jQuery(document).ready(function ($) {
const globalAttributes = $('.global_attribute_table');
let uniqueId = Math.random().toString(36).substr(2, 9);
const globalAttributes = $('.global_attribute_table');
let uniqueId = Math.random().toString(36).substr(2, 9);

// deploy-button FROM build_hook
const buildUrl = $('#build_url');
$('#deploy-button').click(function (e) {
e.preventDefault();
$.ajax({
url: buildUrl.val(),
type: 'POST',
success: function (data) {
alert('Build triggered successfully');
},
});
});
// product_attributes is an object that contains the product attributes

// add global attribute
$('.add_global_attribute').click(function (e) {
e.preventDefault();
console.log('add global attribute');
const newAttribute = `
<tr>
<td>
<input type="text"
class="flex-1"
name="woonuxt_options[global_attributes][${uniqueId}][label]"
value=""
placeholder="e.g. Filter by Color"
/>
</td>
<td>
<select name="woonuxt_options[global_attributes][${uniqueId}][slug]" required>
<?php foreach ( $product_attributes as $attribute ) : ?>
<option value="" disabled selected>Select Attribute</option>
<option value="pa_<?php echo $attribute->attribute_name; ?>">
<?php echo $attribute->attribute_label; ?>
</option>
<?php endforeach; ?>
</select>
</td>
<td>
<input type="checkbox"
name="woonuxt_options[global_attributes][${uniqueId}][showCount]"
value="1"
/>
</td>
<td>
<input type="checkbox"
name="woonuxt_options[global_attributes][${uniqueId}][hideEmpty]"
value="1"
/>
</td>
<td>
<input type="checkbox"
name="woonuxt_options[global_attributes][${uniqueId}][openByDefault]"
value="1"
/>
</td>
<td>
<div class="text-right row-actions">
<a href="#" class="text-danger remove_global_attribute">Delete</a> |
<a href="#" title="Move Up" class="text-primary move_global_attribute_up">▲</a> |
<a href="#" title="Move Down" class="text-primary move_global_attribute_down">▼</a>
</div>
</td>
</tr>
`;
// deploy-button FROM build_hook
const buildUrl = $('#build_url');
$('#deploy-button').click(function (e) {
e.preventDefault();
$.ajax({
url: buildUrl.val(),
type: 'POST',
success: function (data) {
alert('Build triggered successfully');
},
});
});

globalAttributes.find('tbody').append(newAttribute);
// add global attribute
$('.add_global_attribute').click(function (e) {
e.preventDefault();
console.log('add global attribute');
const newAttribute = `
<tr>
<td>
<input type="text" class="flex-1" name="woonuxt_options[global_attributes][${uniqueId}][label]" value="" placeholder="e.g. Filter by Color"/>
</td>
<td>
<select name="woonuxt_options[global_attributes][${uniqueId}][slug]" required>
${Object.keys(product_attributes).map((key) => {
return `<option value="pa_${product_attributes[key].attribute_name}">${product_attributes[key].attribute_label}</option>`;
})}
</select>
</td>
<td>
<input type="checkbox" name="woonuxt_options[global_attributes][${uniqueId}][showCount]" value="1" />
</td>
<td>
<input type="checkbox" name="woonuxt_options[global_attributes][${uniqueId}][hideEmpty]"value="1" />
</td>
<td>
<input type="checkbox" name="woonuxt_options[global_attributes][${uniqueId}][openByDefault]" value="1" />
</td>
<td>
<div class="text-right row-actions">
<a href="#" class="text-danger remove_global_attribute">Delete</a> |
<a href="#" title="Move Up" class="text-primary move_global_attribute_up">▲</a> |
<a href="#" title="Move Down" class="text-primary move_global_attribute_down">▼</a>
</div>
</td>
</tr>
`;

uniqueId = Math.random().toString(36).substr(2, 9);
});
globalAttributes.find('tbody').append(newAttribute);

// remove global attribute
$(document).on('click', '.remove_global_attribute', function (e) {
e.preventDefault();
console.log('remove');
// find parent tr
$(this).closest('tr').remove();
});
uniqueId = Math.random().toString(36).substr(2, 9);
});

// move global attribute
$(document).on('click', '.move_global_attribute_up', function (e) {
e.preventDefault();
console.log('move up');
const currentRow = $(this).closest('tr');
const prevRow = currentRow.prev();
if (prevRow.length) {
currentRow.insertBefore(prevRow);
}
});
// remove global attribute
$(document).on('click', '.remove_global_attribute', function (e) {
e.preventDefault();
console.log('remove');
// find parent tr
$(this).closest('tr').remove();
});

$(document).on('click', '.move_global_attribute_down', function (e) {
e.preventDefault();
console.log('move down');
const currentRow = $(this).closest('tr');
const nextRow = currentRow.next();
if (nextRow.length) {
currentRow.insertAfter(nextRow);
}
});
// move global attribute
$(document).on('click', '.move_global_attribute_up', function (e) {
e.preventDefault();
console.log('move up');
const currentRow = $(this).closest('tr');
const prevRow = currentRow.prev();
if (prevRow.length) {
currentRow.insertBefore(prevRow);
}
});

$(document).on('click', '.move_global_attribute_down', function (e) {
e.preventDefault();
console.log('move down');
const currentRow = $(this).closest('tr');
const nextRow = currentRow.next();
if (nextRow.length) {
currentRow.insertAfter(nextRow);
}
});
});
135 changes: 64 additions & 71 deletions woonuxt.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Author: Scott Kennedy
Author URI: http://scottyzen.com
Plugin URI: http://woonuxt.com
Version: 1.0.26
Version: 1.0.27
Text Domain: woonuxt
GitHub Plugin URI: scottyzen/woonuxt-settings
GitHub Plugin URI: https://github.com/scottyzen/woonuxt-settings
Expand All @@ -14,7 +14,7 @@
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;

define( 'WOONUXT_SETTINGS_VERSION', '1.0.26' );
define( 'WOONUXT_SETTINGS_VERSION', '1.0.27' );

add_action('admin_enqueue_scripts', 'load_admin_style_woonuxt');
function load_admin_style_woonuxt() {
Expand All @@ -36,7 +36,7 @@ function woonuxt_plugin_action_links($links) {
'wp-graphql' => [
'name' => 'WPGraphQL',
'description' => 'A GraphQL API for WordPress and installs the WPGraphQL playground (GraphiQL)',
'url' => 'https://downloads.wordpress.org/plugin/wp-graphql.1.13.7.zip',
'url' => 'https://downloads.wordpress.org/plugin/wp-graphql.1.13.8.zip',
'file' => 'wp-graphql/wp-graphql.php',
'icon' => 'https://www.wpgraphql.com/logo-wpgraphql.svg',
'slug' => 'wp-graphql',
Expand All @@ -59,6 +59,18 @@ function woonuxt_plugin_action_links($links) {
],
];

function prepareSettingsAfterPluginInstall(){
$cors_settings = get_option( 'graphql_cors_settings' );
$cors_settings['login_mutation'] = 'on';
$cors_settings['logout_mutation'] = 'on';
$cors_settings['acao'] = 'http://localhost:3000';
update_option( 'graphql_cors_settings', $cors_settings );

$graph_settings = get_option( 'graphql_general_settings' );
$graph_settings['public_introspection_enabled'] = 'on';
update_option( 'graphql_general_settings', $graph_settings );
}

// Add options page
add_action( 'admin_menu', 'woonuxt_options_page' );
function woonuxt_options_page() {
Expand All @@ -73,15 +85,15 @@ function woonuxt_options_page() {

// Options page HTML
function woonuxt_options_page_html() {
$options = get_option( 'woonuxt_options' );
?>
<div class="acf-admin-toolbar">
<a href="https://woonuxt.com" class="acf-logo"><img src="<?php echo plugins_url( 'assets/colored-logo.svg', __FILE__ ); ?>" alt="WooNuxt"
target="_blank"></a>
<h2 style="display: block;">WooNuxt</h2>
<?php if( $options['build_hook'] ) : ?>
<button id="build-button" class="acf-button button button-primary button-large" style="display: block;">Build</button>
<button id="deploy-button" class="acf-button button button-primary button-large" style="display: block;">Deploy</button>
<?php endif; ?>
<button id="deploy-button" class="acf-button button button-primary button-large" style="display: block;">Deploy</button>
</div>
<div class="wrap">
<form action="options.php" method="post">
Expand Down Expand Up @@ -204,18 +216,14 @@ function deploy_button_callback() {
// Field callback
function global_setting_callback() {
$options = get_option( 'woonuxt_options' );
$product_attributes = wc_get_attribute_taxonomies();
echo '<script>var product_attributes = ' . json_encode( $product_attributes ) . ';</script>';


if ( isset( $_GET['prepare'] ) ) {
$cors_settings = get_option( 'graphql_cors_settings' );
$cors_settings['login_mutation'] = 'on';
$cors_settings['logout_mutation'] = 'on';
$cors_settings['acao'] = 'http://localhost:3000';
update_option( 'graphql_cors_settings', $cors_settings );

$graph_settings = get_option( 'graphql_general_settings' );
$graph_settings['public_introspection_enabled'] = 'on';
update_option( 'graphql_general_settings', $graph_settings );
prepareSettingsAfterPluginInstall();
}

?>

<div class="global_setting woonuxt-section">
Expand Down Expand Up @@ -320,62 +328,42 @@ class="widefat"
</tr>
</thead>
<tbody id="the-list">
<?php if ( $options['global_attributes'] ) :
$product_attributes = wc_get_attribute_taxonomies();

foreach ( $options['global_attributes'] as $key => $value ) : ?>
<tr>
<td>
<input type="text"
class="flex-1"
name="woonuxt_options[global_attributes][<?php echo $key; ?>][label]"
value="<?php echo $value['label']; ?>"
placeholder="e.g. Filter by Color"
/>
</td>
<td>
<select name="woonuxt_options[global_attributes][<?php echo $key; ?>][slug]">
<?php foreach ( $product_attributes as $attribute ) :
$slected_attribute = $value['slug'] == 'pa_' . $attribute->attribute_name ? 'selected' : '' ;
?>
<option value="pa_<?php echo $attribute->attribute_name; ?>" <?php echo $slected_attribute; ?>>
<?php echo $attribute->attribute_label; ?>
</option>
<?php endforeach; ?>
</select>
</td>
<td>
<input type="checkbox"
name="woonuxt_options[global_attributes][<?php echo $key; ?>][showCount]"
value="1"
<?php echo $value['showCount'] ? 'checked' : ''; ?>
/>
</td>
<td>
<input type="checkbox"
name="woonuxt_options[global_attributes][<?php
echo $key; ?>][hideEmpty]"
value="1"
<?php echo $value['hideEmpty'] ? 'checked' : ''; ?>
/>
</td>
<td>
<input type="checkbox"
name="woonuxt_options[global_attributes][<?php echo $key; ?>][openByDefault]"
value="1"
<?php echo $value['openByDefault'] ? 'checked' : ''; ?>
/>
</td>
<td>
<div class="text-right row-actions">
<a href="#" class="text-danger remove_global_attribute">Delete</a> |
<a href="#" title="Move Up" class="text-primary move_global_attribute_up">▲</a> |
<a href="#" title="Move Down" class="text-primary move_global_attribute_down">▼</a>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
<?php if ( isset( $options['global_attributes'] ) ) :
foreach ( $options['global_attributes'] as $key => $value ) : ?>
<tr>
<td>
<input type="text" class="flex-1" name="woonuxt_options[global_attributes][<?php echo $key; ?>][label]" value="<?php echo $value['label']; ?>" placeholder="e.g. Filter by Color" />
</td>
<td>
<select name="woonuxt_options[global_attributes][<?php echo $key; ?>][slug]">
<?php foreach ( $product_attributes as $attribute ) :
$slected_attribute = $value['slug'] == 'pa_' . $attribute->attribute_name ? 'selected' : '' ;
?>
<option value="pa_<?php echo $attribute->attribute_name; ?>" <?php echo $slected_attribute; ?>>
<?php echo $attribute->attribute_label; ?>
</option>
<?php endforeach; ?>
</select>
</td>
<td>
<input type="checkbox" name="woonuxt_options[global_attributes][<?php echo $key; ?>][showCount]" value="1" <?php echo isset( $value['showCount']) ? 'checked' : ''; ?> />
</td>
<td>
<input type="checkbox" name="woonuxt_options[global_attributes][<?php echo $key; ?>][hideEmpty]" value="1" <?php echo isset( $value['hideEmpty'] ) ? 'checked' : '';?> />
</td>
<td>
<input type="checkbox" name="woonuxt_options[global_attributes][<?php echo $key; ?>][openByDefault]" value="1" <?php echo isset( $value['openByDefault'] ) ? 'checked' : '';?> />
</td>
<td>
<div class="text-right row-actions">
<a href="#" class="text-danger remove_global_attribute">Delete</a> |
<a href="#" title="Move Up" class="text-primary move_global_attribute_up">▲</a> |
<a href="#" title="Move Down" class="text-primary move_global_attribute_down">▼</a>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
<tfoot>
<tr>
Expand All @@ -384,7 +372,12 @@ class="flex-1"
<th class="manage-column column-primary" scope="col"></th>
<th class="manage-column column-primary" scope="col"></th>
<th class="manage-column column-primary" scope="col"></th>
<th class="manage-column column-primary" scope="col"><button class="add_global_attribute button button-primary">Add New</button></th>
<th class="manage-column column-primary" scope="col">
<button
class="add_global_attribute button button-primary"
type="button"
>Add New</button>
</th>
</tr>
</table>
<p class="description">This will be used to manage the filters on the product listing page.</p>
Expand Down

0 comments on commit 67c4535

Please sign in to comment.