diff --git a/composer.lock b/composer.lock index 415ce03047..5bb3239066 100644 --- a/composer.lock +++ b/composer.lock @@ -70,16 +70,16 @@ }, { "name": "lucatume/di52", - "version": "3.3.5", + "version": "3.3.7", "source": { "type": "git", "url": "https://github.com/lucatume/di52.git", - "reference": "d39d1cbbc57eb41c7aa21fab106e17b6938ec6b3" + "reference": "76c0c2ad0422ce595e2e38138456f3475888e32c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/lucatume/di52/zipball/d39d1cbbc57eb41c7aa21fab106e17b6938ec6b3", - "reference": "d39d1cbbc57eb41c7aa21fab106e17b6938ec6b3", + "url": "https://api.github.com/repos/lucatume/di52/zipball/76c0c2ad0422ce595e2e38138456f3475888e32c", + "reference": "76c0c2ad0422ce595e2e38138456f3475888e32c", "shasum": "" }, "require": { @@ -109,9 +109,9 @@ "description": "A PHP 5.6 compatible dependency injection container.", "support": { "issues": "https://github.com/lucatume/di52/issues", - "source": "https://github.com/lucatume/di52/tree/3.3.5" + "source": "https://github.com/lucatume/di52/tree/3.3.7" }, - "time": "2023-09-01T08:49:32+00:00" + "time": "2024-04-26T14:46:26+00:00" }, { "name": "monolog/monolog", @@ -2807,16 +2807,16 @@ }, { "name": "phpcsstandards/phpcsutils", - "version": "1.0.10", + "version": "1.0.11", "source": { "type": "git", "url": "https://github.com/PHPCSStandards/PHPCSUtils.git", - "reference": "51609a5b89f928e0c463d6df80eb38eff1eaf544" + "reference": "c457da9dabb60eb7106dd5e3c05132b1a6539c6a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/PHPCSStandards/PHPCSUtils/zipball/51609a5b89f928e0c463d6df80eb38eff1eaf544", - "reference": "51609a5b89f928e0c463d6df80eb38eff1eaf544", + "url": "https://api.github.com/repos/PHPCSStandards/PHPCSUtils/zipball/c457da9dabb60eb7106dd5e3c05132b1a6539c6a", + "reference": "c457da9dabb60eb7106dd5e3c05132b1a6539c6a", "shasum": "" }, "require": { @@ -2891,7 +2891,7 @@ "type": "open_collective" } ], - "time": "2024-03-17T23:44:50+00:00" + "time": "2024-04-24T11:47:18+00:00" }, { "name": "phpdocumentor/reflection-common", @@ -2948,28 +2948,35 @@ }, { "name": "phpdocumentor/reflection-docblock", - "version": "5.3.0", + "version": "5.4.0", "source": { "type": "git", "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", - "reference": "622548b623e81ca6d78b721c5e029f4ce664f170" + "reference": "298d2febfe79d03fe714eb871d5538da55205b1a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/622548b623e81ca6d78b721c5e029f4ce664f170", - "reference": "622548b623e81ca6d78b721c5e029f4ce664f170", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/298d2febfe79d03fe714eb871d5538da55205b1a", + "reference": "298d2febfe79d03fe714eb871d5538da55205b1a", "shasum": "" }, "require": { + "doctrine/deprecations": "^1.1", "ext-filter": "*", - "php": "^7.2 || ^8.0", + "php": "^7.4 || ^8.0", "phpdocumentor/reflection-common": "^2.2", - "phpdocumentor/type-resolver": "^1.3", + "phpdocumentor/type-resolver": "^1.7", + "phpstan/phpdoc-parser": "^1.7", "webmozart/assert": "^1.9.1" }, "require-dev": { - "mockery/mockery": "~1.3.2", - "psalm/phar": "^4.8" + "mockery/mockery": "~1.3.5", + "phpstan/extension-installer": "^1.1", + "phpstan/phpstan": "^1.8", + "phpstan/phpstan-mockery": "^1.1", + "phpstan/phpstan-webmozart-assert": "^1.2", + "phpunit/phpunit": "^9.5", + "vimeo/psalm": "^5.13" }, "type": "library", "extra": { @@ -2993,15 +3000,15 @@ }, { "name": "Jaap van Otterdijk", - "email": "account@ijaap.nl" + "email": "opensource@ijaap.nl" } ], "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", "support": { "issues": "https://github.com/phpDocumentor/ReflectionDocBlock/issues", - "source": "https://github.com/phpDocumentor/ReflectionDocBlock/tree/5.3.0" + "source": "https://github.com/phpDocumentor/ReflectionDocBlock/tree/5.4.0" }, - "time": "2021-10-19T17:43:47+00:00" + "time": "2024-04-09T21:13:58+00:00" }, { "name": "phpdocumentor/type-resolver", @@ -3132,16 +3139,16 @@ }, { "name": "phpstan/phpdoc-parser", - "version": "1.26.0", + "version": "1.28.0", "source": { "type": "git", "url": "https://github.com/phpstan/phpdoc-parser.git", - "reference": "231e3186624c03d7e7c890ec662b81e6b0405227" + "reference": "cd06d6b1a1b3c75b0b83f97577869fd85a3cd4fb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/231e3186624c03d7e7c890ec662b81e6b0405227", - "reference": "231e3186624c03d7e7c890ec662b81e6b0405227", + "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/cd06d6b1a1b3c75b0b83f97577869fd85a3cd4fb", + "reference": "cd06d6b1a1b3c75b0b83f97577869fd85a3cd4fb", "shasum": "" }, "require": { @@ -3173,9 +3180,9 @@ "description": "PHPDoc parser with support for nullable, intersection and generic types", "support": { "issues": "https://github.com/phpstan/phpdoc-parser/issues", - "source": "https://github.com/phpstan/phpdoc-parser/tree/1.26.0" + "source": "https://github.com/phpstan/phpdoc-parser/tree/1.28.0" }, - "time": "2024-02-23T16:05:55+00:00" + "time": "2024-04-03T18:51:33+00:00" }, { "name": "phpunit/php-code-coverage", @@ -4417,16 +4424,16 @@ }, { "name": "sirbrillig/phpcs-variable-analysis", - "version": "v2.11.17", + "version": "v2.11.18", "source": { "type": "git", "url": "https://github.com/sirbrillig/phpcs-variable-analysis.git", - "reference": "3b71162a6bf0cde2bff1752e40a1788d8273d049" + "reference": "ca242a0b7309e0f9d1f73b236e04ecf4ca3248d0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sirbrillig/phpcs-variable-analysis/zipball/3b71162a6bf0cde2bff1752e40a1788d8273d049", - "reference": "3b71162a6bf0cde2bff1752e40a1788d8273d049", + "url": "https://api.github.com/repos/sirbrillig/phpcs-variable-analysis/zipball/ca242a0b7309e0f9d1f73b236e04ecf4ca3248d0", + "reference": "ca242a0b7309e0f9d1f73b236e04ecf4ca3248d0", "shasum": "" }, "require": { @@ -4471,7 +4478,7 @@ "source": "https://github.com/sirbrillig/phpcs-variable-analysis", "wiki": "https://github.com/sirbrillig/phpcs-variable-analysis/wiki" }, - "time": "2023-08-05T23:46:11+00:00" + "time": "2024-04-13T16:42:46+00:00" }, { "name": "slevomat/coding-standard", @@ -4540,16 +4547,16 @@ }, { "name": "squizlabs/php_codesniffer", - "version": "3.9.0", + "version": "3.9.2", "source": { "type": "git", "url": "https://github.com/PHPCSStandards/PHP_CodeSniffer.git", - "reference": "d63cee4890a8afaf86a22e51ad4d97c91dd4579b" + "reference": "aac1f6f347a5c5ac6bc98ad395007df00990f480" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/PHPCSStandards/PHP_CodeSniffer/zipball/d63cee4890a8afaf86a22e51ad4d97c91dd4579b", - "reference": "d63cee4890a8afaf86a22e51ad4d97c91dd4579b", + "url": "https://api.github.com/repos/PHPCSStandards/PHP_CodeSniffer/zipball/aac1f6f347a5c5ac6bc98ad395007df00990f480", + "reference": "aac1f6f347a5c5ac6bc98ad395007df00990f480", "shasum": "" }, "require": { @@ -4616,7 +4623,7 @@ "type": "open_collective" } ], - "time": "2024-02-16T15:06:51+00:00" + "time": "2024-04-23T20:25:34+00:00" }, { "name": "stellarwp/coding-standards", @@ -4903,16 +4910,16 @@ }, { "name": "symfony/deprecation-contracts", - "version": "v2.5.2", + "version": "v2.5.3", "source": { "type": "git", "url": "https://github.com/symfony/deprecation-contracts.git", - "reference": "e8b495ea28c1d97b5e0c121748d6f9b53d075c66" + "reference": "80d075412b557d41002320b96a096ca65aa2c98d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/e8b495ea28c1d97b5e0c121748d6f9b53d075c66", - "reference": "e8b495ea28c1d97b5e0c121748d6f9b53d075c66", + "url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/80d075412b557d41002320b96a096ca65aa2c98d", + "reference": "80d075412b557d41002320b96a096ca65aa2c98d", "shasum": "" }, "require": { @@ -4950,7 +4957,7 @@ "description": "A generic function and convention to trigger deprecation notices", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/deprecation-contracts/tree/v2.5.2" + "source": "https://github.com/symfony/deprecation-contracts/tree/v2.5.3" }, "funding": [ { @@ -4966,7 +4973,7 @@ "type": "tidelift" } ], - "time": "2022-01-02T09:53:40+00:00" + "time": "2023-01-24T14:02:46+00:00" }, { "name": "symfony/dom-crawler", @@ -5884,16 +5891,16 @@ }, { "name": "symfony/service-contracts", - "version": "v2.5.2", + "version": "v2.5.3", "source": { "type": "git", "url": "https://github.com/symfony/service-contracts.git", - "reference": "4b426aac47d6427cc1a1d0f7e2ac724627f5966c" + "reference": "a2329596ddc8fd568900e3fc76cba42489ecc7f3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/service-contracts/zipball/4b426aac47d6427cc1a1d0f7e2ac724627f5966c", - "reference": "4b426aac47d6427cc1a1d0f7e2ac724627f5966c", + "url": "https://api.github.com/repos/symfony/service-contracts/zipball/a2329596ddc8fd568900e3fc76cba42489ecc7f3", + "reference": "a2329596ddc8fd568900e3fc76cba42489ecc7f3", "shasum": "" }, "require": { @@ -5947,7 +5954,7 @@ "standards" ], "support": { - "source": "https://github.com/symfony/service-contracts/tree/v2.5.2" + "source": "https://github.com/symfony/service-contracts/tree/v2.5.3" }, "funding": [ { @@ -5963,7 +5970,7 @@ "type": "tidelift" } ], - "time": "2022-05-30T19:17:29+00:00" + "time": "2023-04-21T15:04:16+00:00" }, { "name": "symfony/translation", @@ -6062,16 +6069,16 @@ }, { "name": "symfony/translation-contracts", - "version": "v2.5.2", + "version": "v2.5.3", "source": { "type": "git", "url": "https://github.com/symfony/translation-contracts.git", - "reference": "136b19dd05cdf0709db6537d058bcab6dd6e2dbe" + "reference": "b0073a77ac0b7ea55131020e87b1e3af540f4664" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/translation-contracts/zipball/136b19dd05cdf0709db6537d058bcab6dd6e2dbe", - "reference": "136b19dd05cdf0709db6537d058bcab6dd6e2dbe", + "url": "https://api.github.com/repos/symfony/translation-contracts/zipball/b0073a77ac0b7ea55131020e87b1e3af540f4664", + "reference": "b0073a77ac0b7ea55131020e87b1e3af540f4664", "shasum": "" }, "require": { @@ -6120,7 +6127,7 @@ "standards" ], "support": { - "source": "https://github.com/symfony/translation-contracts/tree/v2.5.2" + "source": "https://github.com/symfony/translation-contracts/tree/v2.5.3" }, "funding": [ { @@ -6136,7 +6143,7 @@ "type": "tidelift" } ], - "time": "2022-06-27T16:58:25+00:00" + "time": "2024-01-23T13:51:25+00:00" }, { "name": "symfony/yaml", @@ -6215,22 +6222,22 @@ "source": { "type": "git", "url": "https://github.com/the-events-calendar/tec-testing-facilities.git", - "reference": "6c66235042c4389079a0c98ff8e70ad0ac227eb0" + "reference": "0b5909300805b8844ce4720cee72ea7389eb2e58" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/the-events-calendar/tec-testing-facilities/zipball/6c66235042c4389079a0c98ff8e70ad0ac227eb0", - "reference": "6c66235042c4389079a0c98ff8e70ad0ac227eb0", + "url": "https://api.github.com/repos/the-events-calendar/tec-testing-facilities/zipball/0b5909300805b8844ce4720cee72ea7389eb2e58", + "reference": "0b5909300805b8844ce4720cee72ea7389eb2e58", "shasum": "" }, "require": { "lucatume/wp-browser": "^2.0 || ^3.0", "nilportugues/sql-query-formatter": "^1.2", - "php": ">=7.0" + "php": ">=7.4" }, "require-dev": { "codeception/codeception": "^3.0", - "dealerdirect/phpcodesniffer-composer-installer": "^0.5.0", + "dealerdirect/phpcodesniffer-composer-installer": "^0.7.2", "phpunit/phpunit": "^6.0", "vlucas/phpdotenv": "^3.0", "wordpress/wordpress": "dev-master", @@ -6261,7 +6268,7 @@ "issues": "https://github.com/the-events-calendar/tec-testing-facilities/issues", "source": "https://github.com/the-events-calendar/tec-testing-facilities/tree/master" }, - "time": "2022-05-20T15:21:07+00:00" + "time": "2024-04-15T14:14:23+00:00" }, { "name": "theseer/tokenizer", @@ -6739,16 +6746,16 @@ }, { "name": "wp-coding-standards/wpcs", - "version": "3.0.1", + "version": "3.1.0", "source": { "type": "git", "url": "https://github.com/WordPress/WordPress-Coding-Standards.git", - "reference": "b4caf9689f1a0e4a4c632679a44e638c1c67aff1" + "reference": "9333efcbff231f10dfd9c56bb7b65818b4733ca7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/WordPress/WordPress-Coding-Standards/zipball/b4caf9689f1a0e4a4c632679a44e638c1c67aff1", - "reference": "b4caf9689f1a0e4a4c632679a44e638c1c67aff1", + "url": "https://api.github.com/repos/WordPress/WordPress-Coding-Standards/zipball/9333efcbff231f10dfd9c56bb7b65818b4733ca7", + "reference": "9333efcbff231f10dfd9c56bb7b65818b4733ca7", "shasum": "" }, "require": { @@ -6757,16 +6764,16 @@ "ext-tokenizer": "*", "ext-xmlreader": "*", "php": ">=5.4", - "phpcsstandards/phpcsextra": "^1.1.0", - "phpcsstandards/phpcsutils": "^1.0.8", - "squizlabs/php_codesniffer": "^3.7.2" + "phpcsstandards/phpcsextra": "^1.2.1", + "phpcsstandards/phpcsutils": "^1.0.10", + "squizlabs/php_codesniffer": "^3.9.0" }, "require-dev": { "php-parallel-lint/php-console-highlighter": "^1.0.0", "php-parallel-lint/php-parallel-lint": "^1.3.2", "phpcompatibility/php-compatibility": "^9.0", "phpcsstandards/phpcsdevtools": "^1.2.0", - "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0" }, "suggest": { "ext-iconv": "For improved results", @@ -6797,11 +6804,11 @@ }, "funding": [ { - "url": "https://opencollective.com/thewpcc/contribute/wp-php-63406", + "url": "https://opencollective.com/php_codesniffer", "type": "custom" } ], - "time": "2023-09-14T07:06:09+00:00" + "time": "2024-03-25T16:39:00+00:00" }, { "name": "zordius/lightncandy", @@ -6874,5 +6881,5 @@ "platform-overrides": { "php": "7.4.0" }, - "plugin-api-version": "2.6.0" + "plugin-api-version": "2.3.0" } diff --git a/src/Common/Integrations/Plugins/Elementor/Widgets/Contracts/Abstract_Widget.php b/src/Common/Integrations/Plugins/Elementor/Widgets/Contracts/Abstract_Widget.php index 0f3742f2c8..49345a93fa 100644 --- a/src/Common/Integrations/Plugins/Elementor/Widgets/Contracts/Abstract_Widget.php +++ b/src/Common/Integrations/Plugins/Elementor/Widgets/Contracts/Abstract_Widget.php @@ -11,6 +11,7 @@ use TEC\Common\Integrations\Plugins\Elementor\Widgets\Template_Engine; use Elementor\Widget_Base; +use WP_Post; /** * Abstract Widget class @@ -83,6 +84,15 @@ abstract class Abstract_Widget extends Widget_Base { */ protected static bool $has_styles = false; + /** + * Whether the widget has scripts to register/enqueue. + * + * @since TBD + * + * @var bool + */ + protected static bool $has_scripts = false; + /** * Widget categories. * @@ -137,8 +147,8 @@ abstract class Abstract_Widget extends Widget_Base { * * @return string */ - public static function get_elementor_slug(): string { - return static::$slug_prefix . static::get_slug(); + public function get_elementor_slug(): string { + return $this->get_slug_prefix() . static::get_slug(); } /** @@ -149,7 +159,7 @@ public static function get_elementor_slug(): string { * @return string */ public function get_name(): string { - return static::get_elementor_slug(); + return $this->get_elementor_slug(); } /** @@ -163,6 +173,65 @@ public static function get_slug(): string { return static::$slug; } + /** + * Get local widget slug. + * + * @since TBD + * + * @param bool $trim Whether to trim the last underscore from the prefix. + * + * @return string + */ + public function get_slug_prefix( $trim = false ): string { + $prefix = static::$slug_prefix; + + + if ( $trim ) { + $prefix = rtrim( $prefix, '_' ); + } else { + // Ensure our prefix ends with an underscore. + $prefix = rtrim( $prefix, '_' ) . '_'; + } + + /** + * Filters the slug prefix for all tec-elementor widgets. + * + * @since TBD + * + * @param string $prefix The widget slug prefix. + * @param bool $trim Whether to trim the last underscore from the prefix. + * @param object $this The widget instance. + * + * @return string + */ + return (string) apply_filters( 'tec_elementor_widget_slug_prefix', $prefix, $trim, $this ); + } + + public function create_slug( $slug ) { + $prefix = $this->get_slug_prefix(); + + return $prefix . $slug; + } + + /** + * Get local widget slug. + * + * @since TBD + * + * @param bool $trim Whether to trim the last slug. + * + * @return string + */ + public function get_asset_prefix( $trim = false ): string { + $prefix = str_replace( '_', '-', $this->get_slug_prefix() ); + + if ( $trim ) { + $prefix = rtrim( $prefix, '-' ); + } + + return $prefix; + } + /** * Gets the title of the widget. * @@ -180,7 +249,7 @@ public function get_title(): string { * @param string $title The widget title. * @param Abstract_Widget $this The widget instance. */ - $title = apply_filters( static::$slug_prefix . 'title', $title, $this ); + $title = apply_filters( $this->create_slug( 'title' ), $title, $this ); /** * Filters the title of a specific tec-elementor widget, by slug. @@ -190,7 +259,7 @@ public function get_title(): string { * @param string $title The widget title. * @param Abstract_Widget $this The widget instance. */ - return (string) apply_filters( static::$slug_prefix . "_widget_{$slug}_title", $title, $this ); + return (string) apply_filters( $this->get_slug_prefix() . "widget_{$slug}_title", $title, $this ); } /** @@ -257,7 +326,7 @@ public function get_element_classes( string $format = 'attribute' ) { * * @return array */ - $classes = apply_filters( static::$slug_prefix . 'element_classes', (array) $classes, $format, $this ); + $classes = apply_filters( $this->get_slug_prefix() . 'element_classes', (array) $classes, $format, $this ); /** * Filters the widget class list for a specific tec-elementor widget, by slug. @@ -270,7 +339,7 @@ public function get_element_classes( string $format = 'attribute' ) { * * @return array */ - $classes = apply_filters( static::$slug_prefix . "{$slug}_element_classes", (array) $classes, $format, $this ); + $classes = apply_filters( $this->get_slug_prefix() . "{$slug}_element_classes", (array) $classes, $format, $this ); // If we want a string, this is where we convert. if ( 'attribute' === $format ) { @@ -300,7 +369,7 @@ public static function trim_slug(): string { */ public function get_widget_class(): string { $slug = static::get_slug(); - $class = static::$asset_prefix . '__' . static::trim_slug(); + $class = static::get_asset_prefix() . '_' . static::trim_slug(); /** * Filters the widget class for all tec-elementor widgets. @@ -312,7 +381,7 @@ public function get_widget_class(): string { * * @return string */ - $class = apply_filters( static::$slug_prefix . 'class', $class, $this ); + $class = apply_filters( $this->get_slug_prefix() . 'class', $class, $this ); /** * Filters the widget class for a specific tec-elementor widget, by slug. @@ -324,7 +393,7 @@ public function get_widget_class(): string { * * @return string */ - return apply_filters( static::$slug_prefix . "{$slug}_class", $class, $this ); + return apply_filters( $this->get_slug_prefix() . "{$slug}_class", $class, $this ); } /** @@ -336,7 +405,7 @@ public function get_widget_class(): string { */ public function get_icon_class(): string { $slug = static::get_slug(); - $class = static::$asset_prefix . '__icon-' . static::trim_slug(); + $class = static::get_asset_prefix() . '__icon-' . static::trim_slug(); /** * Filters the widget icon class for all tec-elementor widgets. @@ -348,7 +417,7 @@ public function get_icon_class(): string { * * @return string */ - $class = apply_filters( static::$slug_prefix . 'icon_class', $class, $this ); + $class = apply_filters( $this->get_slug_prefix() . 'icon_class', $class, $this ); /** * Filters the widget icon class for a specific tec-elementor widget, by slug. @@ -360,7 +429,7 @@ public function get_icon_class(): string { * * @return string */ - return (string) apply_filters( static::$slug_prefix . "{$slug}icon_class", $class, $this ); + return (string) apply_filters( $this->get_slug_prefix() . "{$slug}icon_class", $class, $this ); } /** @@ -400,7 +469,7 @@ protected function post_id(): ?int { * @param int $post_id The post ID. * @param Abstract_Widget $this The widget instance. */ - $post_id = (int) apply_filters( static::$slug_prefix . 'post_id', (int) $post_id, $this ); + $post_id = (int) apply_filters( $this->get_slug_prefix() . 'post_id', (int) $post_id, $this ); /** * Filters the post ID of the post the widget is used in. @@ -410,7 +479,7 @@ protected function post_id(): ?int { * @param int $post_id The post ID. * @param Abstract_Widget $this The widget instance. */ - $post_id = (int) apply_filters( static::$slug_prefix . "{$slug}_post_id", (int) $post_id, $this ); + $post_id = (int) apply_filters( $this->get_slug_prefix() . "{$slug}_post_id", (int) $post_id, $this ); if ( get_post_type( $post_id ) !== static::get_widget_post_type() ) { return null; @@ -430,6 +499,29 @@ public function get_post_id() { return $this->post_id(); } + /** + * Get the post object for the widget. + * + * @since TBD + * + * @return WP_Post|null + */ + public function get_post() { + $post = get_post( $this->get_post_id() ); + + /** + * Filters the post object for the widget. + * + * @since TBD + * + * @param WP_Post $post The post object. + * @param Abstract_Widget $this The widget instance. + * + * @return WP_Post + */ + return apply_filters( $this->get_slug_prefix() . 'post', $post, $this ); + } + /** * Determines if the widget has a valid post ID associated with it. * @@ -482,6 +574,7 @@ public function get_template(): Template_Engine { */ $template_engine_class = $this->get_template_engine_class(); $this->template = $template_engine_class::with_widget( $this ); + $this->template->set_post( $this->get_post() ); do_action( 'tec_elementor_widget_set_template', $this ); } @@ -489,6 +582,10 @@ public function get_template(): Template_Engine { return $this->template; } + public function get_template_prefix( $file = '' ): string { + return trailingslashit( static::$template_prefix ) . ltrim( $file, '/' ); + } + /** * Set up a self-removing filter for a widget template, it should hook itself on the before and after include hooks * of the template engine. @@ -501,8 +598,7 @@ public function get_template(): Template_Engine { * @param int $accepted_args The number of arguments the filter accepts. */ protected function set_template_filter( string $on, ?callable $callback = null, int $priority = 10, int $accepted_args = 1 ): void { - $template_file = $this->trim_slug(); - $hook_name = trailingslashit( static::$template_prefix ) . $template_file; + $hook_name = $this->get_template_prefix( $this->trim_slug() ); $add = "tribe_template_before_include:{$hook_name}"; $remove = "tribe_template_after_include:{$hook_name}"; @@ -589,7 +685,7 @@ public function get_template_args(): array { * * @return array */ - $args = (array) apply_filters( static::$slug_prefix . 'template_data', $args, false, $this ); + $args = (array) apply_filters( $this->get_slug_prefix() . 'template_data', $args, false, $this ); /** * Filters the template data for a specific (by $slug) Elementor widget templates. @@ -600,7 +696,7 @@ public function get_template_args(): array { * * @return array */ - $args = (array) apply_filters( static::$slug_prefix . "{$slug}_template_data", $args, false, $this ); + $args = (array) apply_filters( $this->get_slug_prefix() . "{$slug}_template_data", $args, false, $this ); // Add the widget to the data array. $args['widget'] = $this; @@ -609,7 +705,7 @@ public function get_template_args(): array { } /** - * Get the asset source for the widget. + * Get the asset source (plugin) for the widget. * * @since TBD * @@ -617,12 +713,87 @@ public function get_template_args(): array { */ abstract protected function get_asset_source(); + /** + * Get the asset base path for the widget. + * + * @since TBD + * + * @return string + */ + public function get_asset_base_path(): string { + /** + * Filters the asset base path for all Elementor widgets. + * + * @since TBD + * + * @param string $path The asset base path. + * @param Abstract_Widget $this The widget instance. + * + * @return string + */ + return (string) apply_filters( $this->get_slug_prefix() . 'asset_base_path', static::$asset_base_path, $this ); + } + + /** + * Get the asset file name for the widget. + * + * @since TBD + * + * @param string $suffix The file suffix. 'js' for a javascript or 'css' for a stylesheet asset. Default is 'css'. + * + * @return string + */ + public function get_asset_file_name( $suffix = 'css' ) { + $file = static::get_asset_base_path() . static::trim_slug(); + $file .= ( 'js' === $suffix ) ? '.js' : '.css'; + + /** + * Filters the asset file name for all Elementor widgets. + * + * @since TBD + * + * @param string $file The asset file name. + * @param string $suffix The file suffix. Should be 'js' or 'css'. + * @param Abstract_Widget $this The widget instance. + * + * @return string + */ + return (string) apply_filters( $this->get_slug_prefix() . 'asset_file_name', $file, $suffix, $this ); + } + + /** + * Get the asset handle for the widget. + * + * @since TBD + * + * @param string $suffix The file suffix. 'js' for a javascript or 'css' for a stylesheet asset. Default is 'css'. + * + * @return string + */ + public function get_asset_handle( $suffix = 'css' ) { + $handle = static::get_asset_prefix() . static::trim_slug(); + $handle .= ( 'js' === $suffix ) ? '-scripts' : '-styles'; + + /** + * Filters the asset handle for all Elementor widgets. + * + * @since TBD + * + * @param string $handle The asset handle. + * @param string $suffix The file suffix. Should be 'js' or 'css'. + * @param Abstract_Widget $this The widget instance. + * + * @return string + */ + return (string) apply_filters( $this->get_slug_prefix() . 'asset_handle', $handle, $suffix, $this ); + } + /** * Register the styles for the widget. * * @since TBD */ - public function register_style(): void { + public function register_assets(): void { if ( ! static::$has_styles ) { return; } @@ -631,18 +802,30 @@ public function register_style(): void { return; } - $slug = static::trim_slug(); - $source = $this->get_asset_source(); - // Register the styles for the widget. - tribe_asset( - tribe( $source ), - static::$asset_prefix . $slug . '-styles', - static::$asset_base_path . $slug . '.css', - [], - null, - [ 'groups' => [ static::$group_key ] ] - ); + if ( static::$has_styles ) { + tribe_asset( + tribe( $this->get_asset_source() ), + $this->get_asset_handle(), + $this->get_asset_file_name(), + [], + null, + [ 'groups' => [ static::$group_key ] ] + ); + } + + // Register the scripts for the widget. + if ( static::$has_scripts ) { + tribe_asset( + tribe( $this->get_asset_source() ), + $this->get_asset_handle( 'js' ), + $this->get_asset_file_name( 'js' ), + [], + null, + [ 'groups' => [ static::$group_key ] ] + ); + } + } /** @@ -655,9 +838,20 @@ public function enqueue_style(): void { return; } - $slug = static::trim_slug(); + tribe_asset_enqueue( static::get_asset_handle() ); + } - tribe_asset_enqueue( static::$asset_prefix . $slug . '-styles' ); + /** + * Enqueue the styles for the widget. + * + * @since TBD + */ + public function enqueue_script(): void { + if ( ! static::$has_scripts ) { + return; + } + + tribe_asset_enqueue( static::get_asset_handle( 'js' ) ); } /** @@ -668,7 +862,12 @@ public function enqueue_style(): void { * @return string */ public function get_output(): string { - $output = $this->get_template()->template( 'widgets/base', $this->get_template_args(), false ); + $template = $this->get_template(); + $output = $template->template( 'widgets/base', $this->get_template_args(), false ); + + if ( ! $template->has_post() && ! $template->get_widget()->should_show_mock_data() ) { + return ''; + } $this->unset_template_filters();