<?php
/**
 * Autoload file for some common functions used all over the WooCommerce SMS Notification addon
 *
 * @package miniorange-sms-order-notification-otp-verification/notifications/wcsmsnotification
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

define( 'WC_MSN_DIR', plugin_dir_path( __FILE__ ) );
define( 'WC_MSN_URL', plugin_dir_url( __FILE__ ) );
define( 'WC_MSN_VERSION', '1.0.0' );
define( 'WC_MSN_CSS_URL', WC_MSN_URL . 'includes/css/settings.min.css?version=' . WC_MSN_VERSION );
define( 'MOWC_MSN_JS_URL', WC_MSN_URL . 'includes/js/mo_wc_settings.min.js?version=' . WC_MSN_VERSION );

/*
|------------------------------------------------------------------------------------------------------
| SOME COMMON FUNCTIONS USED ALL OVER THE ADD-ON
|------------------------------------------------------------------------------------------------------
 */


/**
 * This function is used to handle the add-ons get option call. A separate function has been created so that
 * we can manage getting of database values all from one place. Any changes need to be made can be made here
 * rather than having to make changes in all of the add-on files.
 *
 * Calls the mains plugins get_mo_option function.
 *
 * @param string      $option_string - option name.
 * @param bool|string $prefix - prefix for database keys.
 * @return mixed The option value or empty string if not found.
 */
function mowc_get_mowc_option( $option_string, $prefix = null ) {
	if ( ! is_string( $option_string ) || empty( $option_string ) ) {
		return '';
	}
	$option_string = sanitize_key( $option_string );
	$prefix        = ( null === $prefix ? 'mo_wc_sms_customer_' : ( is_string( $prefix ) ? sanitize_key( $prefix ) : '' ) );
	$option_name   = $prefix . $option_string;
	return get_mo_wc_option( $option_name, '' );
}

/**
 * This function is used to handle the add-ons update option call. A separate function has been created so that
 * we can manage getting of database values all from one place. Any changes need to be made can be made here
 * rather than having to make changes in all of the add-on files.
 *
 * Calls the mains plugins update_mo_option function.
 *
 * @param string      $option_name - key of the option name.
 * @param mixed       $value - value of the option.
 * @param null|string $prefix - prefix of the key for database entry.
 * @return bool True if option value has changed, false if not or if update failed.
 */
function mowc_update_mowc_option( $option_name, $value, $prefix = null ) {
	if ( ! is_string( $option_name ) || empty( $option_name ) ) {
		return false;
	}
	$option_name      = sanitize_key( $option_name );
	$prefix           = ( null === $prefix ? 'mo_wc_sms_customer_' : ( is_string( $prefix ) ? sanitize_key( $prefix ) : '' ) );
	$full_option_name = $prefix . $option_name;
	return update_mo_wc_option( $full_option_name, $value, '' );
}
