AllLanguagesHooks.php
51 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\Exporter; |
| 4 | |
| 5 | use WCML\Terms\SuspendWpmlFiltersFactory as SuspendTermsFilterFactory; |
| 6 | use WCML\Utilities\Suspend\PostsQueryFiltersFactory as SuspendPostsQueryFiltersFactory; |
| 7 | use WPML\API\Sanitize; |
| 8 | use WPML\FP\Obj; |
| 9 | use WPML\LIB\WP\Hooks; |
| 10 | use function WPML\FP\spreadArgs; |
| 11 | use function WPML\FP\tap; |
| 12 | |
| 13 | class AllLanguagesHooks implements \IWPML_Backend_Action, \IWPML_DIC_Action { |
| 14 | |
| 15 | const KEY_EXPORT_ALL_LANGUAGES = 'wpml_export_all_languages'; |
| 16 | |
| 17 | private $sitepress; |
| 18 | |
| 19 | public function __construct( \SitePress $sitepress ) { |
| 20 | $this->sitepress = $sitepress; |
| 21 | } |
| 22 | |
| 23 | public function add_hooks() { |
| 24 | Hooks::onAction( 'woocommerce_product_export_row' ) |
| 25 | ->then( [ $this, 'addLanguageField' ] ); |
| 26 | |
| 27 | Hooks::onFilter( 'woocommerce_product_export_product_query_args' ) |
| 28 | ->then( spreadArgs( tap( [ $this, 'suspendWpmlLanguageFilters' ] ) ) ); |
| 29 | } |
| 30 | |
| 31 | public function addLanguageField() { |
| 32 | if ( 'all' === $this->sitepress->get_current_language() ) { |
| 33 | echo '<input type="hidden" name="' . self::KEY_EXPORT_ALL_LANGUAGES . '" value="1" />'; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | public function suspendWpmlLanguageFilters() { |
| 38 | if ( $this->isExportingAllLanguages() ) { |
| 39 | SuspendPostsQueryFiltersFactory::create(); |
| 40 | SuspendTermsFilterFactory::create(); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | private function isExportingAllLanguages() { |
| 45 | $formQueryString = Sanitize::string( (string) Obj::prop( 'form', $_POST ) ); |
| 46 | wp_parse_str( $formQueryString, $queryArgs ); |
| 47 | |
| 48 | return isset( $queryArgs[ self::KEY_EXPORT_ALL_LANGUAGES ] ); |
| 49 | } |
| 50 | } |
| 51 |