Skip to content

Commit 1f97d03

Browse files
committed
Fix Undefined array key.
1 parent 7410fe3 commit 1f97d03

File tree

3 files changed

+73
-57
lines changed

3 files changed

+73
-57
lines changed

changelog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
*** WooCommerce API Manager PHP Library for Plugins and Themes Changelog ***
22

3+
2023.08.14 - version 2.9.1
4+
* Fix: Process custom menu array using if > elseif > else rather than switch to fix PHP notices: Undefined index, Undefined array key.
5+
36
2023.04.28 - version 2.9
47
* New: Added ability to create custom menus and submenus for license/API key activation form.
58
* New: Added activation stats, total activations, total purchased activations, total activations remaining.

wc-am-client.php

Lines changed: 61 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* but are not limited to, the working concept, function, and behavior of this software,
99
* the logical code structure and expression as written.
1010
*
11-
* @version 2.9
11+
* @version 2.9.1
1212
* @author Todd Lahman LLC https://www.toddlahman.com/
1313
* @copyright Copyright (c) Todd Lahman LLC (support@toddlahman.com)
1414
* @package WooCommerce API Manager plugin and theme library
@@ -17,8 +17,8 @@
1717

1818
defined( 'ABSPATH' ) || exit;
1919

20-
if ( ! class_exists( 'WC_AM_Client_2_9' ) ) {
21-
class WC_AM_Client_2_9 {
20+
if ( ! class_exists( 'WC_AM_Client_2_9_1' ) ) {
21+
class WC_AM_Client_2_9_1 {
2222

2323
/**
2424
* Class args
@@ -77,7 +77,13 @@ public function __construct( $file, $product_id, $software_version, $plugin_or_t
7777

7878
if ( $this->no_product_id ) {
7979
$this->identifier = $this->plugin_or_theme == 'plugin' ? dirname( untrailingslashit( plugin_basename( $file ) ) ) : basename( dirname( plugin_basename( $file ) ) );
80-
$product_id = strtolower( str_ireplace( array( ' ', '_', '&', '?', '-' ), '_', $this->identifier ) );
80+
$product_id = strtolower( str_ireplace( array(
81+
' ',
82+
'_',
83+
'&',
84+
'?',
85+
'-'
86+
), '_', $this->identifier ) );
8187
$this->wc_am_product_id = 'wc_am_product_id_' . $product_id;
8288
$this->product_id_chosen = get_option( $this->wc_am_product_id );
8389
} else {
@@ -104,7 +110,13 @@ public function __construct( $file, $product_id, $software_version, $plugin_or_t
104110
/**
105111
* If the product_id is a pre 2.0 string, format it to be used as an option key, otherwise it will be an integer if >= 2.0.
106112
*/
107-
$this->data_key = 'wc_am_client_' . strtolower( str_ireplace( array( ' ', '_', '&', '?', '-' ), '_', $product_id ) );
113+
$this->data_key = 'wc_am_client_' . strtolower( str_ireplace( array(
114+
' ',
115+
'_',
116+
'&',
117+
'?',
118+
'-'
119+
), '_', $product_id ) );
108120
$this->wc_am_activated_key = $this->data_key . '_activated';
109121

110122
if ( is_admin() ) {
@@ -157,7 +169,10 @@ public function __construct( $file, $product_id, $software_version, $plugin_or_t
157169
*
158170
* $this->wc_am_domain = str_ireplace( array( 'http://', 'https://' ), '', home_url() ); // blog domain name
159171
*/
160-
$this->wc_am_domain = str_ireplace( array( 'http://', 'https://' ), '', home_url() ); // blog domain name
172+
$this->wc_am_domain = str_ireplace( array(
173+
'http://',
174+
'https://'
175+
), '', home_url() ); // blog domain name
161176
$this->wc_am_software_version = $this->software_version; // The software version
162177

163178
/**
@@ -230,26 +245,23 @@ public function register_menu() {
230245
$icon_url = ! empty( $this->menu[ 'icon_url' ] ) ? $this->menu[ 'icon_url' ] : '';
231246
$position = ! empty( $this->menu[ 'position' ] ) ? $this->menu[ 'position' ] : null;
232247

233-
switch ( $this->menu[ 'menu_type' ] ) {
234-
case 'add_submenu_page':
248+
if ( is_array( $this->menu ) && ! empty( $this->menu[ 'menu_type' ] ) ) {
249+
if ( $this->menu[ 'menu_type' ] == 'add_submenu_page' ) {
235250
// add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $callback = '', $position = null )
236251
add_submenu_page( $this->menu[ 'parent_slug' ], $page_title, $menu_title, $capability, $menu_slug, $callback, $position );
237-
break;
238-
case 'add_options_page':
252+
} elseif ( $this->menu[ 'menu_type' ] == 'add_options_page' ) {
239253
// add_options_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $position = null )
240254
add_options_page( $page_title, $menu_title, $capability, $menu_slug, $callback, $position );
241-
break;
242-
case 'add_menu_page':
255+
} elseif ( $this->menu[ 'menu_type' ] == 'add_menu_page' ) {
243256
// add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $icon_url = '', $position = null )
244257
add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $callback, $icon_url, $position );
245-
break;
246-
default:
247-
// add_options_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $position = null )
248-
add_options_page( sprintf( __( '%s', $this->text_domain ), $this->wc_am_settings_menu_title ), sprintf( __( '%s', $this->text_domain ), $this->wc_am_settings_menu_title ), 'manage_options', $this->wc_am_activation_tab_key, array(
249-
$this,
250-
'config_page'
251-
) );
252-
break;
258+
}
259+
} else {
260+
// add_options_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $position = null )
261+
add_options_page( sprintf( __( '%s', $this->text_domain ), $this->wc_am_settings_menu_title ), sprintf( __( '%s', $this->text_domain ), $this->wc_am_settings_menu_title ), 'manage_options', $this->wc_am_activation_tab_key, array(
262+
$this,
263+
'config_page'
264+
) );
253265
}
254266
}
255267

@@ -519,9 +531,9 @@ public function inactive_notice() { ?>
519531
<?php if ( isset( $_GET[ 'page' ] ) && $this->wc_am_activation_tab_key == $_GET[ 'page' ] ) {
520532
return;
521533
} ?>
522-
<div class="notice notice-error">
523-
<p><?php printf( __( 'The <strong>%s</strong> API Key has not been activated, so the %s is inactive! %sClick here%s to activate <strong>%s</strong>.', $this->text_domain ), esc_attr( $this->software_title ), esc_attr( $this->plugin_or_theme ), '<a href="' . esc_url( admin_url( 'options-general.php?page=' . $this->wc_am_activation_tab_key ) ) . '">', '</a>', esc_attr( $this->software_title ) ); ?></p>
524-
</div>
534+
<div class="notice notice-error">
535+
<p><?php printf( __( 'The <strong>%s</strong> API Key has not been activated, so the %s is inactive! %sClick here%s to activate <strong>%s</strong>.', $this->text_domain ), esc_attr( $this->software_title ), esc_attr( $this->plugin_or_theme ), '<a href="' . esc_url( admin_url( 'options-general.php?page=' . $this->wc_am_activation_tab_key ) ) . '">', '</a>', esc_attr( $this->software_title ) ); ?></p>
536+
</div>
525537
<?php }
526538
}
527539

@@ -536,9 +548,9 @@ public function check_external_blocking() {
536548

537549
if ( ! defined( 'WP_ACCESSIBLE_HOSTS' ) || stristr( WP_ACCESSIBLE_HOSTS, $host ) === false ) {
538550
?>
539-
<div class="notice notice-error">
540-
<p><?php printf( __( '<b>Warning!</b> You\'re blocking external requests which means you won\'t be able to get %s updates. Please add %s to %s.', $this->text_domain ), $this->software_title, '<strong>' . $host . '</strong>', '<code>WP_ACCESSIBLE_HOSTS</code>' ); ?></p>
541-
</div>
551+
<div class="notice notice-error">
552+
<p><?php printf( __( '<b>Warning!</b> You\'re blocking external requests which means you won\'t be able to get %s updates. Please add %s to %s.', $this->text_domain ), $this->software_title, '<strong>' . $host . '</strong>', '<code>WP_ACCESSIBLE_HOSTS</code>' ); ?></p>
553+
</div>
542554
<?php
543555
}
544556
}
@@ -553,18 +565,18 @@ public function config_page() {
553565
$current_tab = isset( $_GET[ 'tab' ] ) ? $_GET[ 'tab' ] : $this->wc_am_activation_tab_key;
554566
$tab = isset( $_GET[ 'tab' ] ) ? $_GET[ 'tab' ] : $this->wc_am_activation_tab_key;
555567
?>
556-
<div class='wrap'>
557-
<h2><?php esc_html_e( $this->wc_am_settings_title, $this->text_domain ); ?></h2>
558-
<h2 class="nav-tab-wrapper">
568+
<div class='wrap'>
569+
<h2><?php esc_html_e( $this->wc_am_settings_title, $this->text_domain ); ?></h2>
570+
<h2 class="nav-tab-wrapper">
559571
<?php
560572
foreach ( $settings_tabs as $tab_page => $tab_name ) {
561573
$active_tab = $current_tab == $tab_page ? 'nav-tab-active' : '';
562574
echo '<a class="nav-tab ' . esc_attr( $active_tab ) . '" href="?page=' . esc_attr( $this->wc_am_activation_tab_key ) . '&tab=' . esc_attr( $tab_page ) . '">' . esc_attr( $tab_name ) . '</a>';
563575
}
564576
?>
565-
</h2>
566-
<form action='options.php' method='post'>
567-
<div class="main">
577+
</h2>
578+
<form action='options.php' method='post'>
579+
<div class="main">
568580
<?php
569581
if ( $tab == $this->wc_am_activation_tab_key ) {
570582
settings_fields( $this->data_key );
@@ -576,9 +588,9 @@ public function config_page() {
576588
submit_button( esc_html__( 'Save Changes', $this->text_domain ) );
577589
}
578590
?>
579-
</div>
580-
</form>
581-
</div>
591+
</div>
592+
</form>
593+
</div>
582594
<?php
583595
}
584596

@@ -591,11 +603,11 @@ public function load_settings() {
591603
add_settings_section( $this->wc_am_api_key_key, esc_html__( 'API Key Activation', $this->text_domain ), array(
592604
$this,
593605
'wc_am_api_key_text'
594-
), $this->wc_am_activation_tab_key );
606+
), $this->wc_am_activation_tab_key );
595607
add_settings_field( $this->wc_am_api_key_key, esc_html__( 'API Key', $this->text_domain ), array(
596608
$this,
597609
'wc_am_api_key_field'
598-
), $this->wc_am_activation_tab_key, $this->wc_am_api_key_key );
610+
), $this->wc_am_activation_tab_key, $this->wc_am_api_key_key );
599611

600612
/**
601613
* @since 2.3
@@ -604,7 +616,7 @@ public function load_settings() {
604616
add_settings_field( 'product_id', esc_html__( 'Product ID', $this->text_domain ), array(
605617
$this,
606618
'wc_am_product_id_field'
607-
), $this->wc_am_activation_tab_key, $this->wc_am_api_key_key );
619+
), $this->wc_am_activation_tab_key, $this->wc_am_api_key_key );
608620
}
609621

610622
/**
@@ -619,11 +631,11 @@ public function load_settings() {
619631
add_settings_field( 'status', esc_html__( 'API Key Status', $this->text_domain ), array(
620632
$this,
621633
'wc_am_api_key_status'
622-
), $this->wc_am_activation_tab_key, $this->wc_am_api_key_key );
634+
), $this->wc_am_activation_tab_key, $this->wc_am_api_key_key );
623635
add_settings_field( 'info', esc_html__( 'Activation Info', $this->text_domain ), array(
624636
$this,
625637
'wc_am_activation_info'
626-
), $this->wc_am_activation_tab_key, $this->wc_am_api_key_key );
638+
), $this->wc_am_activation_tab_key, $this->wc_am_api_key_key );
627639
// Activation settings
628640
register_setting( $this->wc_am_deactivate_checkbox_key, $this->wc_am_deactivate_checkbox_key, array(
629641
$this,
@@ -632,15 +644,15 @@ public function load_settings() {
632644
add_settings_section( 'deactivate_button', esc_html__( 'API Deactivation', $this->text_domain ), array(
633645
$this,
634646
'wc_am_deactivate_text'
635-
), $this->wc_am_deactivation_tab_key );
647+
), $this->wc_am_deactivation_tab_key );
636648
add_settings_field( 'deactivate_button', esc_html__( 'Deactivate API Key', $this->text_domain ), array(
637649
$this,
638650
'wc_am_deactivate_textarea'
639-
), $this->wc_am_deactivation_tab_key, 'deactivate_button' );
651+
), $this->wc_am_deactivation_tab_key, 'deactivate_button' );
640652
}
641653

642654
// Provides text for api key section
643-
public function wc_am_api_key_text() { }
655+
public function wc_am_api_key_text() {}
644656

645657
// Returns the API Key status from the WooCommerce API Manager on the server
646658
public function wc_am_api_key_status() {
@@ -702,8 +714,8 @@ public function get_api_key_status( $live = false ) {
702714
*/
703715
public function wc_am_activation_info() {
704716
$result_error = get_option( 'wc_am_' . $this->product_id . '_activate_error' );
705-
$live_status = json_decode( $this->status(), true );
706-
$line_break = wp_kses_post( '<br>' );
717+
$live_status = json_decode( $this->status(), true );
718+
$line_break = wp_kses_post( '<br>' );
707719

708720
if ( ! empty( $live_status ) && $live_status[ 'success' ] == false ) {
709721
echo esc_html( 'Error: ' . $live_status[ 'data' ][ 'error' ] );
@@ -712,7 +724,7 @@ public function wc_am_activation_info() {
712724
if ( $this->get_api_key_status() ) {
713725
$result_success = get_option( 'wc_am_' . $this->product_id . '_activate_success' );
714726

715-
if ( ! empty( $live_status ) && $live_status[ 'status_check' ] == 'active' ) {
727+
if ( ! empty( $live_status ) && $live_status[ 'status_check' ] == 'active' ) {
716728
echo esc_html( 'Activations purchased: ' . $live_status[ 'data' ][ 'total_activations_purchased' ] );
717729
echo $line_break;
718730
echo esc_html( 'Total Activations: ' . $live_status[ 'data' ][ 'total_activations' ] );
@@ -910,13 +922,14 @@ public function replace_license_key( $current_api_key ) {
910922
$this->deactivate( $args );
911923
}
912924

913-
public function wc_am_deactivate_text() { }
925+
public function wc_am_deactivate_text() {}
914926

915927
public function wc_am_deactivate_textarea() {
916928
echo '<input type="checkbox" id="' . esc_attr( $this->wc_am_deactivate_checkbox_key ) . '" name="' . esc_attr( $this->wc_am_deactivate_checkbox_key ) . '" value="on"';
917929
echo checked( get_option( $this->wc_am_deactivate_checkbox_key ), 'on' );
918930
echo '/>';
919-
?><span class="description"><?php esc_html_e( 'Deactivates an API Key so it can be used on another blog.', $this->text_domain ); ?></span>
931+
?>
932+
<span class="description"><?php esc_html_e( 'Deactivates an API Key so it can be used on another blog.', $this->text_domain ); ?></span>
920933
<?php
921934
}
922935

wc-api-manager-php-library.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Plugin Name: WooCommerce API Manager PHP Library for Plugins and Themes Example plugin
55
* Plugin URI: https://toddlahman.com/shop/woocommerce-api-manager-php-library-for-plugins-and-themes/
66
* Description: Drop the wc-am-client.php library into a plugin or theme, and use the example code below after line 26.
7-
* Version: 2.9
7+
* Version: 2.9.1
88
* Author: Todd Lahman LLC
99
* Author URI: https://www.toddlahman.com/
1010
* License: Copyright Todd Lahman LLC
@@ -29,7 +29,7 @@
2929
*/
3030

3131
// Load WC_AM_Client class if it exists.
32-
if ( ! class_exists( 'WC_AM_Client_2_9' ) ) {
32+
if ( ! class_exists( 'WC_AM_Client_2_9_1' ) ) {
3333
/*
3434
* |---------------------------------------------------------------------
3535
* | This must be exactly the same for both plugins and themes.
@@ -39,7 +39,7 @@
3939
}
4040

4141
// Instantiate WC_AM_Client class object if the WC_AM_Client class is loaded.
42-
if ( class_exists( 'WC_AM_Client_2_9' ) ) {
42+
if ( class_exists( 'WC_AM_Client_2_9_1' ) ) {
4343
/**
4444
* This file is only an example that includes a plugin header, and this code used to instantiate the client object. The variable $wcam_lib
4545
* can be used to access the public properties from the WC_AM_Client class, but $wcam_lib must have a unique name. To find data saved by
@@ -62,11 +62,11 @@
6262
*
6363
* Example:
6464
*
65-
* $wcam_lib = new WC_AM_Client_2_9( $file, $product_id, $software_version, $plugin_or_theme, $api_url, $software_title );
65+
* $wcam_lib = new WC_AM_Client_2_9_1( $file, $product_id, $software_version, $plugin_or_theme, $api_url, $software_title );
6666
*/
6767

6868
// Default menu Theme example.
69-
//$wcam_lib = new WC_AM_Client_2_9( __FILE__, 234, '1.0', 'theme', 'http://wc/', 'WooCommerce API Manager PHP Library for Plugins and Themes' );
69+
//$wcam_lib = new WC_AM_Client_2_9_1( __FILE__, 234, '1.0', 'theme', 'http://wc/', 'WooCommerce API Manager PHP Library for Plugins and Themes' );
7070

7171
/*
7272
* Default Menu Plugin example.
@@ -82,13 +82,13 @@
8282
* with a different Product ID, then do not set the Product ID here.
8383
*/
8484

85-
//$wcam_lib = new WC_AM_Client_2_9( __FILE__, 32960, '1.2', 'plugin', 'http://wc/', 'WooCommerce API Manager PHP Library for Plugins and Themes' );
86-
$wcam_lib = new WC_AM_Client_2_9( __FILE__, '', '1.2', 'plugin', 'http://wc/', 'WooCommerce API Manager PHP Library for Plugins and Themes', 'wc-am-text' );
85+
//$wcam_lib = new WC_AM_Client_2_9_1( __FILE__, 32960, '1.2', 'plugin', 'http://wc/', 'WooCommerce API Manager PHP Library for Plugins and Themes' );
86+
$wcam_lib = new WC_AM_Client_2_9_1( __FILE__, '', '1.2', 'plugin', 'http://wc/', 'WooCommerce API Manager PHP Library for Plugins and Themes', 'wc-am-text' );
8787

8888
/**
8989
* Custom top level or top level submenu.
9090
*
91-
* Last argument to the WC_AM_Client_2_9 class is to prevent the not activated yet admin message from being displayed, which may not be necessary with a custom menu.
91+
* Last argument to the WC_AM_Client_2_9_1 class is to prevent the not activated yet admin message from being displayed, which may not be necessary with a custom menu.
9292
*
9393
* Example using add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $callback = '', $position = null );
9494
*
@@ -107,5 +107,5 @@
107107

108108
// $wcam_lib_custom_menu = array( 'menu_type' => 'add_submenu_page', 'parent_slug' => 'my-plugin.php', 'page_title' => 'My Plugin License Activation', 'menu_title' => 'API Key' );
109109

110-
// $wcam_lib = new WC_AM_Client_2_9( __FILE__, 168804, '1.2', 'plugin', 'http://wc/', 'WooCommerce API Manager PHP Library for Plugins and Themes', 'wc-am-text', $wcam_lib_custom_menu, false );
110+
// $wcam_lib = new WC_AM_Client_2_9_1( __FILE__, 168804, '1.2', 'plugin', 'http://wc/', 'WooCommerce API Manager PHP Library for Plugins and Themes', 'wc-am-text', $wcam_lib_custom_menu, false );
111111
}

0 commit comments

Comments
 (0)