| 1 |
<?php |
| 2 |
namespace Sellkit\Blocks\Render; |
| 3 |
defined( 'ABSPATH' ) || die(); |
| 4 |
|
| 5 |
use Sellkit\Blocks\Helpers\Optin\Helper; |
| 6 |
use Sellkit\Blocks\Sellkit_Blocks; |
| 7 |
|
| 8 |
/** |
| 9 |
* Optin block. |
| 10 |
* |
| 11 |
* @since 2.3.0 |
| 12 |
*/ |
| 13 |
class Optin { |
| 14 |
/** |
| 15 |
* Post ID. |
| 16 |
* |
| 17 |
* @var int |
| 18 |
* @since 2.3.0 |
| 19 |
* @access private |
| 20 |
*/ |
| 21 |
private $post_id; |
| 22 |
|
| 23 |
/** |
| 24 |
* Optin constructor. |
| 25 |
* |
| 26 |
* @since 2.3.0 |
| 27 |
*/ |
| 28 |
public function __construct( $post_id ) { |
| 29 |
$this->post_id = $post_id; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Check block activation. |
| 34 |
* |
| 35 |
* @since 2.3.0 |
| 36 |
* @return boolean |
| 37 |
*/ |
| 38 |
public function is_active() { |
| 39 |
return true; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Register block from meta. |
| 44 |
* |
| 45 |
* @since 2.3.0 |
| 46 |
*/ |
| 47 |
public function register_block_meta() { |
| 48 |
if ( ! $this->is_active() ) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
register_block_type_from_metadata( |
| 53 |
__DIR__, |
| 54 |
[ |
| 55 |
'render_callback' => [ $this, 'render' ], |
| 56 |
] |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get localize data. |
| 62 |
* |
| 63 |
* @since 2.3.0 |
| 64 |
* @return array |
| 65 |
*/ |
| 66 |
private function get_localize_data() { |
| 67 |
$messages = |
| 68 |
class_exists( 'Sellkit\Blocks\Helpers\Optin\Helper' ) ? |
| 69 |
( new Helper )->get_messages() : |
| 70 |
[ |
| 71 |
'success' => esc_html__( 'The form was sent successfully!', 'sellkit' ), |
| 72 |
'error' => esc_html__( 'Please check the errors.', 'sellkit' ), |
| 73 |
'required' => esc_html__( 'Required', 'sellkit' ), |
| 74 |
]; |
| 75 |
|
| 76 |
return [ |
| 77 |
'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
| 78 |
'nonce' => wp_create_nonce( 'sellkit_block' ), |
| 79 |
'sellkitOptinValidationsTranslations' => [ |
| 80 |
'general' => [ |
| 81 |
'errorExists' => $messages['error'], |
| 82 |
'required' => $messages['required'], |
| 83 |
'invalidEmail' => esc_html__( 'Invalid Email address.', 'sellkit' ), |
| 84 |
'invalidPhone' => esc_html__( 'The value should only consist numbers and phone characters (-, +, (), etc).', 'sellkit' ), |
| 85 |
'invalidNumber' => esc_html__( 'Invalid number.', 'sellkit' ), |
| 86 |
'invalidMaxValue' => esc_html__( 'Value must be less than or equal to MAX_VALUE.', 'sellkit' ), |
| 87 |
'invalidMinValue' => esc_html__( 'Value must be greater than or equal to MIN_VALUE.', 'sellkit' ), |
| 88 |
], |
| 89 |
// Validation messages specific to Intelligent Tel Input plugin. |
| 90 |
'itiValidation' => [ |
| 91 |
'invalidCountryCode' => esc_html__( 'Invalid country code.', 'sellkit' ), |
| 92 |
'tooShort' => esc_html__( 'Phone number is too short.', 'sellkit' ), |
| 93 |
'tooLong' => esc_html__( 'Phone number is too long.', 'sellkit' ), |
| 94 |
'areaCodeMissing' => esc_html__( 'Area code is required..', 'sellkit' ), |
| 95 |
'invalidLength' => esc_html__( 'Phone number has an invalid length.', 'sellkit' ), |
| 96 |
'invalidGeneral' => esc_html__( 'Invalid phone number.', 'sellkit' ), |
| 97 |
'typeMismatch' => [ |
| 98 |
'0' => esc_html__( 'Phone number must be of type: ', 'sellkit' ) . esc_html__( 'Fixed Line', 'sellkit' ) . '.', |
| 99 |
'1' => esc_html__( 'Phone number must be of type: ', 'sellkit' ) . esc_html__( 'Mobile', 'sellkit' ) . '.', |
| 100 |
'2' => esc_html__( 'Phone number must be of type: ', 'sellkit' ) . esc_html__( 'Fixed Line or Mobile', 'sellkit' ) . '.', |
| 101 |
'3' => esc_html__( 'Phone number must be of type: ', 'sellkit' ) . esc_html__( 'Toll Free', 'sellkit' ) . '.', |
| 102 |
'4' => esc_html__( 'Phone number must be of type: ', 'sellkit' ) . esc_html__( 'Premium Rate', 'sellkit' ) . '.', |
| 103 |
'5' => esc_html__( 'Phone number must be of type: ', 'sellkit' ) . esc_html__( 'Shared Cost', 'sellkit' ) . '.', |
| 104 |
'6' => esc_html__( 'Phone number must be of type: ', 'sellkit' ) . esc_html__( 'VOIP', 'sellkit' ) . '.', |
| 105 |
'7' => esc_html__( 'Phone number must be of type: ', 'sellkit' ) . esc_html__( 'Personal Number', 'sellkit' ) . '.', |
| 106 |
'8' => esc_html__( 'Phone number must be of type: ', 'sellkit' ) . esc_html__( 'Pager', 'sellkit' ) . '.', |
| 107 |
'9' => esc_html__( 'Phone number must be of type: ', 'sellkit' ) . esc_html__( 'UAN', 'sellkit' ) . '.', |
| 108 |
'10' => esc_html__( 'Phone number must be of type: ', 'sellkit' ) . esc_html__( 'Voicemail', 'sellkit' ) . '.', |
| 109 |
], |
| 110 |
], |
| 111 |
] |
| 112 |
]; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Render block in front-end. |
| 117 |
* |
| 118 |
* @param array $attributes Block attributes. |
| 119 |
* @param string $content Block content. |
| 120 |
* @param \WP_Block $block Block object. |
| 121 |
* @since 2.3.0 |
| 122 |
* @return string |
| 123 |
*/ |
| 124 |
public function render( $attributes, $content, $block ) { |
| 125 |
if ( ! is_admin() ) { |
| 126 |
$localize_data = $this->get_localize_data(); |
| 127 |
|
| 128 |
$blocks = new Sellkit_Blocks(); |
| 129 |
|
| 130 |
$google_map_api_key = sellkit_get_option( 'google_api_key', '' ); |
| 131 |
|
| 132 |
if ( ! empty( $google_map_api_key ) ) { |
| 133 |
$blocks->load_google_map( $google_map_api_key ); |
| 134 |
} |
| 135 |
|
| 136 |
$blocks->load_flatpicker(); |
| 137 |
Sellkit_Blocks::load_scripts( 'optin', 'optin-frontend', [ 'sellkit-flatpickr' ], $localize_data ); |
| 138 |
} |
| 139 |
|
| 140 |
return $content; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|