| 1 |
<?php |
| 2 |
/** |
| 3 |
* Elementor Blocks |
| 4 |
* |
| 5 |
* @package CTC |
| 6 |
* @since 5.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace CTC\Elementor; |
| 10 |
|
| 11 |
/** |
| 12 |
* Blocks |
| 13 |
* |
| 14 |
* @since 5.0.0 |
| 15 |
*/ |
| 16 |
class Blocks { |
| 17 |
|
| 18 |
/** |
| 19 |
* Instance |
| 20 |
* |
| 21 |
* @var Blocks|null |
| 22 |
*/ |
| 23 |
private static $instance = null; |
| 24 |
|
| 25 |
/** |
| 26 |
* Registered widgets |
| 27 |
* |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
private $widgets = []; |
| 31 |
|
| 32 |
/** |
| 33 |
* Get instance. |
| 34 |
* |
| 35 |
* @return Blocks |
| 36 |
*/ |
| 37 |
public static function get() { |
| 38 |
if ( null === self::$instance ) { |
| 39 |
self::$instance = new self(); |
| 40 |
} |
| 41 |
return self::$instance; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Constructor |
| 46 |
*/ |
| 47 |
private function __construct() { |
| 48 |
add_action( 'elementor/widgets/register', [ $this, 'register_widgets' ] ); |
| 49 |
add_action( 'elementor/elements/categories_registered', [ $this, 'register_category' ] ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Get widget definitions. |
| 54 |
* |
| 55 |
* @return array |
| 56 |
*/ |
| 57 |
private function get_widget_definitions() { |
| 58 |
return [ |
| 59 |
'email-sample' => [ |
| 60 |
'name' => __( 'Email Sample', 'ctc' ), |
| 61 |
'description' => __( 'Create the email sample and allow users to copy it.', 'ctc' ), |
| 62 |
'class' => Block\Email\Sample::class, |
| 63 |
], |
| 64 |
'email-address' => [ |
| 65 |
'name' => __( 'Email Address', 'ctc' ), |
| 66 |
'description' => __( 'Add the email address and allow users to copy it.', 'ctc' ), |
| 67 |
'class' => Block\Email\Address::class, |
| 68 |
], |
| 69 |
'phone-number' => [ |
| 70 |
'name' => __( 'Phone Number', 'ctc' ), |
| 71 |
'description' => __( 'Add the phone number and allow users to copy it.', 'ctc' ), |
| 72 |
'class' => Block\PhoneNumber::class, |
| 73 |
], |
| 74 |
'copy-button' => [ |
| 75 |
'name' => __( 'Copy Button', 'ctc' ), |
| 76 |
'description' => __( 'Add the copy button and add the hidden content which you want to copy.', 'ctc' ), |
| 77 |
'class' => Block\CopyButton::class, |
| 78 |
], |
| 79 |
'copy-icon' => [ |
| 80 |
'name' => __( 'Copy Icon', 'ctc' ), |
| 81 |
'description' => __( 'Add the copy icon and add the hidden content which you want to copy.', 'ctc' ), |
| 82 |
'class' => Block\CopyIcon::class, |
| 83 |
], |
| 84 |
'blockquote' => [ |
| 85 |
'name' => __( 'Blockquote', 'ctc' ), |
| 86 |
'description' => __( 'Create the blockquote and allow users to copy the text.', 'ctc' ), |
| 87 |
'class' => Block\Blockquote::class, |
| 88 |
], |
| 89 |
'code-snippet' => [ |
| 90 |
'name' => __( 'Code Snippet', 'ctc' ), |
| 91 |
'description' => __( 'Create the code snippet and allow users to copy the text.', 'ctc' ), |
| 92 |
'class' => Block\CodeSnippet::class, |
| 93 |
], |
| 94 |
'message' => [ |
| 95 |
'name' => __( 'Message', 'ctc' ), |
| 96 |
'description' => __( 'Create a message and allow users to copy it.', 'ctc' ), |
| 97 |
'class' => Block\Message::class, |
| 98 |
], |
| 99 |
'wish' => [ |
| 100 |
'name' => __( 'Wish', 'ctc' ), |
| 101 |
'description' => __( 'Create a wish and allow users to copy it.', 'ctc' ), |
| 102 |
'class' => Block\Wish::class, |
| 103 |
], |
| 104 |
'shayari' => [ |
| 105 |
'name' => __( 'Shayari', 'ctc' ), |
| 106 |
'description' => __( 'Create shayari and allow users to copy it.', 'ctc' ), |
| 107 |
'class' => Block\Shayari::class, |
| 108 |
], |
| 109 |
'sms' => [ |
| 110 |
'name' => __( 'SMS', 'ctc' ), |
| 111 |
'description' => __( 'Create an SMS template and allow users to copy it.', 'ctc' ), |
| 112 |
'class' => Block\SMS::class, |
| 113 |
], |
| 114 |
'deal' => [ |
| 115 |
'name' => __( 'Deal', 'ctc' ), |
| 116 |
'description' => __( 'Create a deal card with copy functionality.', 'ctc' ), |
| 117 |
'class' => Block\Deal::class, |
| 118 |
], |
| 119 |
'coupon' => [ |
| 120 |
'name' => __( 'Coupon', 'ctc' ), |
| 121 |
'description' => __( 'Create a coupon code and allow users to copy it.', 'ctc' ), |
| 122 |
'class' => Block\Coupon::class, |
| 123 |
], |
| 124 |
'ai-prompt-generator' => [ |
| 125 |
'name' => __( 'AI Prompt Generator', 'ctc' ), |
| 126 |
'description' => __( 'Generate AI prompts and allow users to copy them.', 'ctc' ), |
| 127 |
'class' => Block\AI\Prompt\Generator::class, |
| 128 |
], |
| 129 |
'table' => [ |
| 130 |
'name' => __( 'Table', 'ctc' ), |
| 131 |
'description' => __( 'Create a table with copy functionality.', 'ctc' ), |
| 132 |
'class' => Block\Table::class, |
| 133 |
], |
| 134 |
'contact-information' => [ |
| 135 |
'name' => __( 'Contact Information', 'ctc' ), |
| 136 |
'description' => __( 'Display contact information with copy functionality.', 'ctc' ), |
| 137 |
'class' => Block\ContactInformation::class, |
| 138 |
], |
| 139 |
]; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Register category |
| 144 |
* |
| 145 |
* @param \Elementor\Elements_Manager $elements_manager Elements manager. |
| 146 |
* |
| 147 |
* @since 5.0.0 |
| 148 |
*/ |
| 149 |
public function register_category( $elements_manager ) { |
| 150 |
$elements_manager->add_category( |
| 151 |
'ctc', |
| 152 |
[ |
| 153 |
'title' => esc_html__( 'Copy Anything to Clipboard', 'ctc' ), |
| 154 |
'icon' => 'fa fa-code', |
| 155 |
] |
| 156 |
); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Register widgets |
| 161 |
* |
| 162 |
* @param \Elementor\Widgets_Manager $widgets_manager Widgets manager. |
| 163 |
* |
| 164 |
* @since 5.0.0 |
| 165 |
*/ |
| 166 |
public function register_widgets( $widgets_manager ) { |
| 167 |
$definitions = $this->get_widget_definitions(); |
| 168 |
|
| 169 |
foreach ( $definitions as $id => $widget ) { |
| 170 |
$class = $widget['class']; |
| 171 |
|
| 172 |
// Check if class exists (via autoloader). |
| 173 |
if ( ! class_exists( $class ) ) { |
| 174 |
// Fallback: require the widget file. |
| 175 |
$file = CTC_ELEMENTOR_DIR . 'widgets/' . $id . '/widget.php'; |
| 176 |
if ( file_exists( $file ) ) { |
| 177 |
require_once $file; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
if ( class_exists( $class ) ) { |
| 182 |
$widgets_manager->register( new $class() ); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Get blocks for external use (e.g., admin UI). |
| 189 |
* |
| 190 |
* @return array |
| 191 |
* |
| 192 |
* @since 5.0.0 |
| 193 |
*/ |
| 194 |
public static function get_blocks() { |
| 195 |
$instance = self::get(); |
| 196 |
$definitions = $instance->get_widget_definitions(); |
| 197 |
$blocks = []; |
| 198 |
|
| 199 |
foreach ( $definitions as $id => $widget ) { |
| 200 |
$blocks[] = [ |
| 201 |
'id' => $id, |
| 202 |
'name' => $widget['name'], |
| 203 |
'description' => $widget['description'], |
| 204 |
]; |
| 205 |
} |
| 206 |
|
| 207 |
return $blocks; |
| 208 |
} |
| 209 |
} |
| 210 |
|