From d0e6e18135b3c0762d975bfc8a91cf6ff9a3ca3f Mon Sep 17 00:00:00 2001 From: Scott Kennedy Date: Wed, 6 Dec 2023 22:46:04 +0000 Subject: [PATCH] Setup helpers --- assets/admin.js | 47 +-- plugin.json | 6 +- woonuxt.php | 837 +++++++++++++++++++++++++++--------------------- 3 files changed, 508 insertions(+), 382 deletions(-) diff --git a/assets/admin.js b/assets/admin.js index a644907..ae31573 100644 --- a/assets/admin.js +++ b/assets/admin.js @@ -1,26 +1,26 @@ 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).slice(2, 11); - // product_attributes is an object that contains the product attributes + // product_attributes is an object that contains the product attributes // deploy-button FROM build_hook - const buildUrl = $('#build_url'); - $('#deploy-button').click(function (e) { + const buildUrl = $("#build_url"); + $("#deploy-button").click(function (e) { e.preventDefault(); $.ajax({ url: buildUrl.val(), - type: 'POST', - success: function (data) { - alert('Build triggered successfully'); + type: "POST", + success(data) { + alert("Build triggered successfully"); }, }); }); // add global attribute - $('.add_global_attribute').click(function (e) { + $(".add_global_attribute").click(function (e) { e.preventDefault(); - console.log('add global attribute'); + console.log("add global attribute"); const newAttribute = ` @@ -52,37 +52,40 @@ jQuery(document).ready(function ($) { `; - globalAttributes.find('tbody').append(newAttribute); + globalAttributes.find("tbody").append(newAttribute); - uniqueId = Math.random().toString(36).substr(2, 9); + uniqueId = Math.random().toString(36).slice(2, 11); }); // remove global attribute - $(document).on('click', '.remove_global_attribute', function (e) { + $(document).on("click", ".remove_global_attribute", function (e) { e.preventDefault(); - console.log('remove'); - // find parent tr - $(this).closest('tr').remove(); + $(this).closest("tr").remove(); }); // move global attribute - $(document).on('click', '.move_global_attribute_up', function (e) { + $(document).on("click", ".move_global_attribute_up", function (e) { e.preventDefault(); - console.log('move up'); - const currentRow = $(this).closest('tr'); + const currentRow = $(this).closest("tr"); const prevRow = currentRow.prev(); if (prevRow.length) { currentRow.insertBefore(prevRow); } }); - $(document).on('click', '.move_global_attribute_down', function (e) { + $(document).on("click", ".move_global_attribute_down", function (e) { e.preventDefault(); - console.log('move down'); - const currentRow = $(this).closest('tr'); + const currentRow = $(this).closest("tr"); const nextRow = currentRow.next(); if (nextRow.length) { currentRow.insertAfter(nextRow); } }); + + // Handle color picker + $("#primary-color-setting input").on("change, input", function () { + $("#woonuxt_options\\[primary_color\\]").val($(this).val()); + $("#color-preview").css("background-color", $(this).val()); + $("#primary_color_picker").val($(this).val()); + }); }); diff --git a/plugin.json b/plugin.json index 38cfda2..dc29a06 100644 --- a/plugin.json +++ b/plugin.json @@ -1,7 +1,7 @@ { "name": "woonuxt Settings", - "version": "1.0.46", - "download_url": "https://github.com/scottyzen/woonuxt-settings/releases/download/1.0.46/woonuxt-settings.zip", + "version": "1.0.47", + "download_url": "https://github.com/scottyzen/woonuxt-settings/releases/download/1.0.47/woonuxt-settings.zip", "homepage": "https://woonuxt.com/", "tested": "6.3.2", @@ -13,7 +13,7 @@ "sections": { "description": "WooNuxt is unmatched when it comes to performance and scalability. Reap the benefits of having a online store that out performs all of your competitors.", "installation": "(Recommended) Installation instructions.", - "changelog": "

1.0.46

Add functionality to increase max query amount if there are more than 100 products

1.0.45

Fix error when WooCommerce is disabled or not installed

" + "changelog": "

1.0.47

Add functionality to increase max query amount if there are more than 100 products

1.0.45

Fix error when WooCommerce is disabled or not installed

" }, "icons": { diff --git a/woonuxt.php b/woonuxt.php index d7064c5..c21684b 100644 --- a/woonuxt.php +++ b/woonuxt.php @@ -5,263 +5,268 @@ Author: Scott Kennedy Author URI: http://scottyzen.com Plugin URI: https://github.com/scottyzen/woonuxt-settings -Version: 1.0.46 +Version: 1.0.47 Text Domain: woonuxt GitHub Plugin URI: scottyzen/woonuxt-settings GitHub Plugin URI: https://github.com/scottyzen/woonuxt-settings */ // Exit if accessed directly -if ( ! defined( 'ABSPATH' ) ) exit; +if (!defined('ABSPATH')) { + exit(); +} -define( 'WOONUXT_SETTINGS_VERSION', '1.0.46' ); -define( 'WPGraphQL_version', '1.17.0' ); -define( 'WooGraphQL_version', '0.18.2' ); -define( 'WPGraphQL_CORS_version', '2.1' ); -define( 'WooCommerce_version', '8.2.1' ); +define('WOONUXT_SETTINGS_VERSION', '1.0.47'); +define('MY_WOOCOMMERCE_VERSION', '8.2.1' ); +define('WP_GRAPHQL_VERSION', '1.19.0'); +define('WOO_GRAPHQL_VERSION', '0.18.2'); +define('WP_GRAPHQL_CORS_VERSION', '2.1'); // Define Globals global $plugin_list; global $github_version; -add_action('admin_enqueue_scripts', 'load_admin_style_woonuxt'); -function load_admin_style_woonuxt() { - wp_enqueue_style('admin_css_woonuxt', plugins_url('assets/styles.css', __FILE__, false, WOONUXT_SETTINGS_VERSION)); - wp_enqueue_script('admin_js', plugins_url('/assets/admin.js', __FILE__), array('jquery'), WOONUXT_SETTINGS_VERSION, true); +add_action('admin_enqueue_scripts', 'loadAdminStyleWoonuxt'); +function loadAdminStyleWoonuxt() { + if (isset($_GET['page']) && $_GET['page'] === 'woonuxt') { + wp_enqueue_style( + 'admin_css_woonuxt', + plugins_url('assets/styles.css', __FILE__), + false, + WOONUXT_SETTINGS_VERSION, + ); + wp_enqueue_script( + 'admin_js', + plugins_url('/assets/admin.js', __FILE__), + ['jquery'], + WOONUXT_SETTINGS_VERSION, + true, + ); + } } -require 'plugin-update-checker/plugin-update-checker.php'; +require_once 'plugin-update-checker/plugin-update-checker.php'; use YahnisElsts\PluginUpdateChecker\v5\PucFactory; $myUpdateChecker = PucFactory::buildUpdateChecker( 'https://raw.githubusercontent.com/scottyzen/woonuxt-settings/master/plugin.json', __FILE__, //Full path to the main plugin file or functions.php. 'woonuxt-settings', - 6 // <-- Check every 6 hours. + 6, // <-- Check every 6 hours. ); - // Add filter to add the settings link to the plugins page -add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'woonuxt_plugin_action_links'); -function woonuxt_plugin_action_links($links) { - $admin_url = get_admin_url(null, 'options-general.php?page=woonuxt'); - if (is_array($links)) { - if (is_string($admin_url)) { - $links[] = 'Settings'; - return $links; - } else { - error_log('WooNuxt: admin_url is not a string'); - } - } else { - error_log('WooNuxt: $links is not an array'); - } +add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'pluginActionLinksWoonuxt'); +function pluginActionLinksWoonuxt($links) { + $admin_url = get_admin_url(null, 'options-general.php?page=woonuxt'); + if (is_array($links)) { + if (is_string($admin_url)) { + $links[] = 'Settings'; + return $links; + } else { + error_log('WooNuxt: admin_url is not a string'); + } + } else { + error_log('WooNuxt: $links is not an array'); + } } require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/plugin.php'; - $plugin_list = [ - 'wp-graphql' => [ - 'name' => 'WPGraphQL', - 'description' => 'A GraphQL API for WordPress and installs the WPGraphQL playground (GraphiQL)', - 'url' => 'https://downloads.wordpress.org/plugin/wp-graphql.'. WPGraphQL_version.'.zip', - 'file' => 'wp-graphql/wp-graphql.php', - 'icon' => 'https://www.wpgraphql.com/logo-wpgraphql.svg', - 'slug' => 'wp-graphql', - ], - 'woographql' => [ - 'name' => 'WooGraphQL', - 'description' => 'Extend WPGraphQL with WooCommerce types, mutations, and queries', - 'url' => 'https://github.com/wp-graphql/wp-graphql-woocommerce/releases/download/v'. WooGraphQL_version.'/wp-graphql-woocommerce.zip', - 'file' => 'wp-graphql-woocommerce/wp-graphql-woocommerce.php', - 'icon' => 'https://woographql.com/_next/image?url=https%3A%2F%2Fadasmqnzur.cloudimg.io%2Fsuperduper.axistaylor.com%2Fapp%2Fuploads%2Fsites%2F4%2F2022%2F08%2Flogo-1.png%3Ffunc%3Dbound%26w%3D300%26h%3D300&w=384&q=75', - 'slug' => 'woographql', - ], - 'wp-graphql-cors' => [ - 'name' => 'WPGraphQL CORS', - 'description' => 'Add CORS headers to your WPGraphQL API & the login/logout mutataions.', - 'url' => 'https://github.com/funkhaus/wp-graphql-cors/archive/refs/tags/2.1.zip', - 'file' => 'wp-graphql-cors-2.1/wp-graphql-cors.php', - 'icon' => 'https://avatars.githubusercontent.com/u/8369076?s=200&v=4', - 'slug' => 'wp-graphql-cors', - ], - 'woocommerce' => [ - 'name' => 'WooCommerce', - 'description' => 'An eCommerce toolkit that helps you sell anything. Beautifully.', - 'url' => 'https://downloads.wordpress.org/plugin/woocommerce.7.9.0.zip', - 'file' => 'woocommerce/woocommerce.php', - 'icon' => 'https://ps.w.org/woocommerce/assets/icon-256x256.gif', - 'slug' => 'woocommerce', - ], + 'woocommerce' => [ + 'name' => 'WooCommerce', + 'description' => 'An eCommerce toolkit that helps you sell anything.', + 'url' => 'https://downloads.wordpress.org/plugin/woocommerce.' . MY_WOOCOMMERCE_VERSION . '.zip', + 'file' => 'woocommerce/woocommerce.php', + 'icon' => 'https://ps.w.org/woocommerce/assets/icon-256x256.gif', + 'slug' => 'woocommerce', + ], + 'wp-graphql' => [ + 'name' => 'WPGraphQL', + 'description' => 'A GraphQL API for WordPress with a built-in GraphiQL playground.', + 'url' => 'https://downloads.wordpress.org/plugin/wp-graphql.' . WP_GRAPHQL_VERSION . '.zip', + 'file' => 'wp-graphql/wp-graphql.php', + 'icon' => 'https://www.wpgraphql.com/logo-wpgraphql.svg', + 'slug' => 'wp-graphql', + ], + 'woographql' => [ + 'name' => 'WooGraphQL', + 'description' => 'Enables GraphQL to work with WooCommerce.', + 'url' => + 'https://github.com/wp-graphql/wp-graphql-woocommerce/releases/download/v' . + WOO_GRAPHQL_VERSION . + '/wp-graphql-woocommerce.zip', + 'file' => 'wp-graphql-woocommerce/wp-graphql-woocommerce.php', + 'icon' => + 'https://woographql.com/_next/image?url=https%3A%2F%2Fadasmqnzur.cloudimg.io%2Fsuperduper.axistaylor.com%2Fapp%2Fuploads%2Fsites%2F4%2F2022%2F08%2Flogo-1.png%3Ffunc%3Dbound%26w%3D300%26h%3D300&w=384&q=75', + 'slug' => 'woographql', + ], + 'wp-graphql-cors' => [ + 'name' => 'WPGraphQL CORS', + 'description' => 'Add CORS headers to your WPGraphQL API.', + 'url' => 'https://github.com/funkhaus/wp-graphql-cors/archive/refs/tags/2.1.zip', + 'file' => 'wp-graphql-cors-2.1/wp-graphql-cors.php', + 'icon' => 'https://avatars.githubusercontent.com/u/8369076?s=200&v=4', + 'slug' => 'wp-graphql-cors', + ], ]; - /** * Get the latest version number from Github. - * * @return string $github_version */ -function github_version_number( ) { - $github_url = 'https://raw.githubusercontent.com/scottyzen/woonuxt-settings/master/woonuxt.php'; - $github_file = file_get_contents( $github_url ); - if ( false === $github_file ) { - return '0.0.0'; - } - preg_match( '/WOONUXT_SETTINGS_VERSION\', \'(.*?)\'/', $github_file, $matches ); - if ( ! isset( $matches[1] ) ) { - return '0.0.0'; - } - $github_version = $matches[1]; - return $github_version; +function getGithubVersionNumber() { + $github_url = 'https://raw.githubusercontent.com/scottyzen/woonuxt-settings/master/woonuxt.php'; + $github_file = file_get_contents($github_url); + if (false === $github_file) { + return '0.0.0'; + } + preg_match('/WOONUXT_SETTINGS_VERSION\', \'(.*?)\'/', $github_file, $matches); + if (!isset($matches[1])) { + return '0.0.0'; + } + return $matches[1]; } /** * Check if an update is available. - * * @return bool */ -function WooNuxtUpdateAvailable() { - try { - $current_version = WOONUXT_SETTINGS_VERSION; - $github_version = github_version_number(); - return ( $current_version < $github_version ); - } catch ( \Exception $e ) { - return false; - } +function woonuxtUpdateAvailable() { + try { + $current_version = WOONUXT_SETTINGS_VERSION; + $github_version = getGithubVersionNumber(); + return $current_version < $github_version; + } catch (\Exception $e) { + return false; + } } - /** * Add the options page */ -add_action( 'admin_menu', 'woonuxt_options_page' ); -function woonuxt_options_page() { - add_options_page( - 'WooNuxt Options', - 'WooNuxt', - 'manage_options', - 'woonuxt', - 'woonuxt_options_page_html' - ); -} +add_action('admin_menu', function () { + add_options_page('WooNuxt Options', 'WooNuxt', 'manage_options', 'woonuxt', 'woonuxt_options_page_html'); +}); // Options page HTML function woonuxt_options_page_html() { - $options = get_option( 'woonuxt_options' ); - ?> + $options = get_option('woonuxt_options'); ?>
-

WooNuxt

- +
+ settings_fields('woonuxt_options'); + do_settings_sections('woonuxt'); + submit_button();?>
install( $plugin_url ); - if ( $result ) { - activate_plugin( $plugin_slug ); - wp_send_json_success( 'Plugin updated' ); - } else { - wp_send_json_error( 'Plugin update failed' ); - } + $plugin_url = + 'https://github.com/scottyzen/woonuxt-settings/releases/download/' . + github_version_number() . + '/woonuxt-settings.zip'; + $plugin_slug = 'woonuxt-settings/woonuxt.php'; + $plugin_path = WP_PLUGIN_DIR . '/' . $plugin_slug; + $plugin_file = $plugin_path . '/woonuxt.php'; + + // Disable and delete the plugin + deactivate_plugins($plugin_slug); + delete_plugins([$plugin_slug]); + + $upgrader = new Plugin_Upgrader(); + $result = $upgrader->install($plugin_url); + if ($result) { + activate_plugin($plugin_slug); + wp_send_json_success('Plugin updated'); + } else { + wp_send_json_error('Plugin update failed'); + } } - - // Register settings -add_action( 'admin_init', 'woonuxt_register_settings' ); -function woonuxt_register_settings() { - - global $WPGraphQL_CORS_version; - global $plugin_list; - - register_setting( 'woonuxt_options', 'woonuxt_options' ); - - if (WooNuxtUpdateAvailable()) { - add_settings_section( 'update_available', 'Update Available', 'update_available_callback', 'woonuxt' ); - } - - // Return true if all plugins are active - $is_all_plugins_active = array_reduce( $plugin_list, function( $carry, $plugin ) { - return $carry && is_plugin_active( $plugin['file'] ); - }, true ); - - - // if all plugins are active don't show required plugins section - if (!$is_all_plugins_active ) { - add_settings_section( 'required_plugins', 'Required Plugins', 'required_plugins_callback', 'woonuxt' ); - } else { - add_settings_section( 'deploy_button', 'Deploy', 'deploy_button_callback', 'woonuxt' ); - } - - if ( class_exists( 'WooCommerce' ) ) { - add_settings_section( 'global_setting', 'Global Settings', 'global_setting_callback', 'woonuxt'); - } +add_action('admin_init', 'registerWoonuxtSettings'); +function registerWoonuxtSettings() { + global $plugin_list; + + register_setting('woonuxt_options', 'woonuxt_options'); + + if (woonuxtUpdateAvailable()) { + add_settings_section('update_available', 'Update Available', 'update_available_callback', 'woonuxt'); + } + + // Return true if all plugins are active + $is_all_plugins_active = array_reduce( + $plugin_list, + function ($carry, $plugin) { + return $carry && is_plugin_active($plugin['file']); + }, + true, + ); + + // if all plugins are active don't show required plugins section + if (!$is_all_plugins_active) { + add_settings_section('required_plugins', 'Required Plugins', 'required_plugins_callback', 'woonuxt'); + } else { + add_settings_section('deploy_button', 'Deploy', 'deployButtonCallback', 'woonuxt'); + } + + if (class_exists('WooCommerce')) { + add_settings_section('global_setting', 'Global Settings', 'globalSettingCallback', 'woonuxt'); + } } - /** * Callback function to display the update available notice and handle the plugin update. */ function update_available_callback() { - $github_version = github_version_number(); - - if (empty($github_version)) { - return; - } - - $current_version = WOONUXT_SETTINGS_VERSION; - - if (version_compare($current_version, $github_version, '>=')) { - return; - } - - $plugin_slug = plugin_basename(__FILE__); - $update_url = 'https://github.com/scottyzen/woonuxt-settings/releases/download/' . $github_version . '/woonuxt-settings.zip'; - $update_text = 'Update WooNuxt Settings Plugin'; - - echo '
'; - printf( - '

There is an update available for the WooNuxt Settings Plugin. Click %s to update from version %s to %s

', - esc_url($update_url), - esc_html($update_text), - esc_html($current_version), - esc_html($github_version) - ); - echo '
'; - ?> + $github_version = github_version_number(); + + if (empty($github_version)) { + return; + } + + $current_version = WOONUXT_SETTINGS_VERSION; + + if (version_compare($current_version, $github_version, '>=')) { + return; + } + + $update_url = + 'https://github.com/scottyzen/woonuxt-settings/releases/download/' . $github_version . '/woonuxt-settings.zip'; + $update_text = 'Update WooNuxt Settings Plugin'; + + echo '
'; + printf( + '

There is an update available for the WooNuxt Settings Plugin. Click %s to update from version %s to %s

', + esc_url($update_url), + esc_html($update_text), + esc_html($current_version), + esc_html($github_version), + ); + echo '
';?> -install( $plugin['url'] ); - if ( ! is_wp_error( $result ) ) { - activate_plugin( $plugin['file']); - } - } - } - } +install($plugin['url']); + if (!is_wp_error($result)) { + activate_plugin($plugin['file']); + } + } + } +} } -function deploy_button_callback() { - $site_name = get_bloginfo( 'name' ); - $gql_endpoint = isset( get_option( 'graphql_general_settings' )['graphql_endpoint'] ) ? get_option( 'graphql_general_settings' )['graphql_endpoint'] : 'graphql'; - $endpoint = get_site_url() . '/' . $gql_endpoint; - $cors_settings = get_option( 'graphql_cors_settings' ); - $login_mutation_is_enabled = isset( $cors_settings['login_mutation'] ) ? $cors_settings['login_mutation'] : false; - $logout_mutation_is_enabled = isset( $cors_settings['logout_mutation'] ) ? $cors_settings['logout_mutation'] : false; - $both_login_and_logout_mutation_is_enabled = $login_mutation_is_enabled === 'on' && $logout_mutation_is_enabled === 'on' ? true : false; +function deployButtonCallback() { + $site_name = get_bloginfo('name'); + $gql_settings = get_option('graphql_general_settings'); + $gql_endpoint = isset($gql_settings['graphql_endpoint']) ? $gql_settings['graphql_endpoint'] : 'graphql'; + $endpoint = get_site_url() . '/' . $gql_endpoint; + $cors_settings = get_option('graphql_cors_settings'); + + // Enable Public Introspection + $publicIntrospectionEnabled = isset($gql_settings['public_introspection_enabled']) ? $gql_settings['public_introspection_enabled'] == 'on' : false; + // graphql_cors_settings[login_mutation]: + $login_mutation_is_enabled = isset($cors_settings['login_mutation']) ? $cors_settings['login_mutation'] == 'on' : false; + // graphql_cors_settings[logout_mutation] + $logout_mutation_is_enabled = isset($cors_settings['logout_mutation']) ? $cors_settings['logout_mutation'] == 'on' : false; + // graphql_cors_settings[acao_use_site_address] + $acao_use_site_address = isset($cors_settings['acao_use_site_address']) ? $cors_settings['acao_use_site_address'] == 'on' : false; + // graphql_cors_settings[acac] not + $acao = isset($cors_settings['acac']) ? $cors_settings['acac'] == 'on' : false; + // Extend "Access-Control-Allow-Origin” header + $extendHeaders = isset($cors_settings['acao']) ? $cors_settings['acao'] != '*' : false; + + // Has at least on product attribute + $product_attributes = wc_get_attribute_taxonomies(); + $hasProductAttributes = count($product_attributes) > 0; + + $allSettingHaveBeenMet = + $publicIntrospectionEnabled && + $login_mutation_is_enabled && + $logout_mutation_is_enabled && + $acao_use_site_address && + $acao && + $extendHeaders && + $hasProductAttributes; ?> @@ -350,24 +423,49 @@ function deploy_button_callback() { @@ -375,33 +473,17 @@ class="mr-8" var product_attributes = ' . json_encode( $product_attributes ) . ';'; - ?> +function globalSettingCallback() { + $options = get_option('woonuxt_options'); + $product_attributes = wc_get_attribute_taxonomies(); + echo ''; + $primary_color = isset($options['primary_color']) ? $options['primary_color'] : '#7F54B2'; + ?>
-
- - - - - - @@ -410,7 +492,7 @@ class="widefat"

You can upload the logo in the Media Library and copy the URL here.

@@ -426,20 +508,22 @@ class="widefat" id="woonuxt_options[primary_color]" type="text" name="woonuxt_options[primary_color]" - value="" - oninput="document.getElementById('primary_color_picker').value = this.value; document.getElementById('color-preview').style.backgroundColor = this.value;" + value="" /> -

- This is an example of how the elements on the frontend will look like with the selected color. -

+

This is an example of how the elements on the frontend will look like with the selected color.

- Color Picker + Color Picker @@ -449,7 +533,7 @@ class="widefat" @@ -486,42 +570,64 @@ class="widefat" - $value ) : ?> + $value): ?> - + @@ -547,96 +653,113 @@ class="add_global_attribute button button-primary" __( 'Woonuxt Global attributes for filtering', 'woonuxt' ), - 'fields' => [ - 'label' => [ 'type' => 'String' ], - 'slug' => [ 'type' => 'String' ], - 'showCount' => [ 'type' => 'Boolean' ], - 'hideEmpty' => [ 'type' => 'Boolean' ], - 'openByDefault' => [ 'type' => 'Boolean' ], - ], - ]); - register_graphql_object_type( 'woonuxtOptionsStripeSettings', [ - 'fields' => [ - 'enabled' => [ 'type' => 'String' ], - 'testmode' => [ 'type' => 'String' ], - 'test_publishable_key' => [ 'type' => 'String' ], - 'publishable_key' => [ 'type' => 'String' ], - ], - ]); - register_graphql_object_type( 'woonuxtOptions', [ - 'description' => __( 'Woonuxt Settings', 'woonuxt' ), - 'fields' => [ - 'primary_color' => [ 'type' => 'String' ], - 'logo' => [ 'type' => 'String' ], - 'maxPrice' => [ 'type' => 'Int' ], - 'productsPerPage' => [ 'type' => 'Int' ], - 'frontEndUrl' => [ 'type' => 'String' ], - 'domain' => [ 'type' => 'String' ], - 'global_attributes' => [ 'type' => [ 'list_of' => 'woonuxtOptionsGlobalAttributes' ] ], - 'publicIntrospectionEnabled' => [ 'type' => 'String', 'default' => 'off' ], - 'stripeSettings' => [ 'type' => 'woonuxtOptionsStripeSettings' ], - 'currencyCode' => [ 'type' => 'String' ], - 'wooCommerceSettingsVersion' => [ 'type' => 'String' ], - ], - ]); - register_graphql_field( 'RootQuery', 'woonuxtSettings', [ - 'type' => 'woonuxtOptions', - 'resolve' => function() { - // woonuxt_options - $options = get_option( 'woonuxt_options' ); - - // Extra options - $options['publicIntrospectionEnabled'] = get_option( 'graphql_general_settings' )['public_introspection_enabled']; - - // Get max price - $max_price = 0; - $loop = new WP_Query( [ 'post_type' => 'product', 'posts_per_page' => 1, 'orderby' => 'meta_value_num', 'order' => 'DESC', 'meta_key' => '_price' ]); - while ( $loop->have_posts() ) : $loop->the_post(); - global $product; - $options['maxPrice'] = $product->get_price(); - endwhile; - wp_reset_query(); - - // Get woocommerce_stripe_settings from wp_options - $stripe_settings = get_option( 'woocommerce_stripe_settings' ); - $options['stripeSettings'] = $stripe_settings; - - // Get WooCommerce currency code - if ( ! function_exists( 'get_woocommerce_currency' ) && function_exists( 'WC' ) ) { - require_once WC()->plugin_path() . '/includes/wc-core-functions.php'; - } - $options['currencyCode'] = get_woocommerce_currency(); - - $options['domain'] = $_SERVER['HTTP_HOST']; - $options['wooCommerceSettingsVersion'] = WOONUXT_SETTINGS_VERSION; - return $options; - }, - ]); - }); - - // Allow plugins to be queried by id - add_filter( 'graphql_data_is_private', function( $is_private, $model_name, $data, $visibility, $owner, $current_user ) { - if ( 'PluginObject' === $model_name ) { return false; } - return $is_private; +add_action('init', function () { + if (!class_exists('\WPGraphQL')) { + return; + } // Check if WP GraphQl is Active + if (!class_exists('WooCommerce')) { + return; + } // Check if WooCommerce is Active + + add_action('graphql_register_types', function () { + register_graphql_object_type('woonuxtOptionsGlobalAttributes', [ + 'description' => __('Woonuxt Global attributes for filtering', 'woonuxt'), + 'fields' => [ + 'label' => ['type' => 'String'], + 'slug' => ['type' => 'String'], + 'showCount' => ['type' => 'Boolean'], + 'hideEmpty' => ['type' => 'Boolean'], + 'openByDefault' => ['type' => 'Boolean'], + ], + ]); + register_graphql_object_type('woonuxtOptionsStripeSettings', [ + 'fields' => [ + 'enabled' => ['type' => 'String'], + 'testmode' => ['type' => 'String'], + 'test_publishable_key' => ['type' => 'String'], + 'publishable_key' => ['type' => 'String'], + ], + ]); + register_graphql_object_type('woonuxtOptions', [ + 'description' => __('Woonuxt Settings', 'woonuxt'), + 'fields' => [ + 'primary_color' => ['type' => 'String'], + 'logo' => ['type' => 'String'], + 'maxPrice' => ['type' => 'Int'], + 'productsPerPage' => ['type' => 'Int'], + 'frontEndUrl' => ['type' => 'String'], + 'domain' => ['type' => 'String'], + 'global_attributes' => ['type' => ['list_of' => 'woonuxtOptionsGlobalAttributes']], + 'publicIntrospectionEnabled' => ['type' => 'String', 'default' => 'off'], + 'stripeSettings' => ['type' => 'woonuxtOptionsStripeSettings'], + 'currencyCode' => ['type' => 'String'], + 'wooCommerceSettingsVersion' => ['type' => 'String'], + ], + ]); + register_graphql_field('RootQuery', 'woonuxtSettings', [ + 'type' => 'woonuxtOptions', + 'resolve' => function () { + // woonuxt_options + $options = get_option('woonuxt_options'); + + // Extra options + $gql_settings = get_option('graphql_general_settings'); + $options['publicIntrospectionEnabled'] = $gql_settings['public_introspection_enabled']; + + $loop = new WP_Query([ + 'post_type' => 'product', + 'posts_per_page' => 1, + 'orderby' => 'meta_value_num', + 'order' => 'DESC', + 'meta_key' => '_price', + ]); + while ($loop->have_posts()): + $loop->the_post(); + global $product; + $options['maxPrice'] = $product->get_price(); + endwhile; + wp_reset_query(); + + // Get woocommerce_stripe_settings from wp_options + $stripe_settings = get_option('woocommerce_stripe_settings'); + $options['stripeSettings'] = $stripe_settings; + + // Get WooCommerce currency code + if (!function_exists('get_woocommerce_currency') && function_exists('WC')) { + require_once WC()->plugin_path() . '/includes/wc-core-functions.php'; + } + $options['currencyCode'] = get_woocommerce_currency(); + + $options['domain'] = $_SERVER['HTTP_HOST']; + $options['wooCommerceSettingsVersion'] = WOONUXT_SETTINGS_VERSION; + return $options; + }, + ]); + }); + + // Allow plugins to be queried by id + add_filter('graphql_data_is_private', function ($is_private, $model_name) { + return 'PluginObject' === $model_name ? false : $is_private; }, 10, 6 ); - // Increase the max query amount if there are more than 100 products - add_filter( 'graphql_connection_max_query_amount', function( $amount, $source, $args, $context, $info ) { - $total_number_of_products = wp_count_posts( 'product' )->publish; + // Increase the max query amount if there are more than 100 products + add_filter('graphql_connection_max_query_amount', function ($amount) { + $total_number_of_products = wp_count_posts('product')->publish; return $amount = $total_number_of_products > 100 ? $total_number_of_products : $amount; }, 10, 5 ); }); +/** + * Check if a plugin is active + */ +add_action('wp_ajax_check_plugin_status', function() { + check_ajax_referer('my_nonce_action', 'security'); + // Get the plugin slug and file from the AJAX request + $plugin_file = sanitize_text_field($_POST['file']); + echo is_plugin_active($plugin_file) ? 'installed' : 'not_installed'; + wp_die(); +});