Creator.php
407 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Contextual info creator. |
| 4 | * |
| 5 | * @package Contextual Info |
| 6 | */ |
| 7 | |
| 8 | namespace WPDesk\FS\TableRate\ContextualInfo; |
| 9 | |
| 10 | use Flexible_Shipping_Contextual_Info; |
| 11 | use FSVendor\WPDesk\PluginBuilder\Plugin\HookableCollection; |
| 12 | use FSVendor\WPDesk\PluginBuilder\Plugin\HookableParent; |
| 13 | |
| 14 | /** |
| 15 | * Can create contextual info. |
| 16 | */ |
| 17 | class Creator implements HookableCollection { |
| 18 | |
| 19 | use HookableParent; |
| 20 | |
| 21 | const METHOD_TITLE_ELEMENT = 'woocommerce_flexible_shipping_method_title'; |
| 22 | const METHOD_TITLE_AND_METHOD_DESCRIPTION_ELEMENTS = 'woocommerce_flexible_shipping_method_title,woocommerce_flexible_shipping_method_description'; |
| 23 | |
| 24 | /** |
| 25 | * @var string |
| 26 | */ |
| 27 | private $base_location_country; |
| 28 | |
| 29 | /** |
| 30 | * Flexible_Shipping_Contextual_Info_Creator constructor. |
| 31 | * |
| 32 | * @param string $base_location_country . |
| 33 | */ |
| 34 | public function __construct( $base_location_country ) { |
| 35 | $this->base_location_country = $base_location_country; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Hooks. |
| 40 | */ |
| 41 | public function hooks() { |
| 42 | $this->hooks_on_hookable_objects(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Create contextual info. |
| 47 | */ |
| 48 | public function create_contextual_info() { |
| 49 | $other_phrases_not_in = []; |
| 50 | |
| 51 | $phrases_in = $this->get_dhl_express_phrases(); |
| 52 | $this->create_dhl_express_contextual_info( $phrases_in ); |
| 53 | $other_phrases_not_in = $this->merge_phrases( $other_phrases_not_in, $phrases_in ); |
| 54 | |
| 55 | $phrases_in = [ 'fedex' ]; |
| 56 | $this->create_fedex_contextual_info( $phrases_in ); |
| 57 | $other_phrases_not_in = $this->merge_phrases( $other_phrases_not_in, $phrases_in ); |
| 58 | |
| 59 | $phrases_in = [ 'ups' ]; |
| 60 | $this->create_ups_contextual_info( $phrases_in ); |
| 61 | $other_phrases_not_in = $this->merge_phrases( $other_phrases_not_in, $phrases_in ); |
| 62 | |
| 63 | if ( $this->is_base_location_country_pl() ) { |
| 64 | $phrases_in = [ 'dpd' ]; |
| 65 | $this->create_dpd_contextual_info( $phrases_in ); |
| 66 | $other_phrases_not_in = $this->merge_phrases( $other_phrases_not_in, $phrases_in ); |
| 67 | |
| 68 | $phrases_in = [ 'List', 'poczta polska', 'pocztex', 'polecony', 'poczt', 'enadawca' ]; |
| 69 | $this->create_enadawca_contextual_info( $phrases_in ); |
| 70 | $other_phrases_not_in = $this->merge_phrases( $other_phrases_not_in, $phrases_in ); |
| 71 | |
| 72 | $phrases_in = [ 'dhl', 'parcel' ]; |
| 73 | $this->create_dhl_contextual_info( $phrases_in ); |
| 74 | $other_phrases_not_in = $this->merge_phrases( $other_phrases_not_in, $phrases_in ); |
| 75 | |
| 76 | $phrases_in = [ 'ruch', 'kiosk', 'orlen' ]; |
| 77 | $this->create_pwr_contextual_info( $phrases_in ); |
| 78 | $other_phrases_not_in = $this->merge_phrases( $other_phrases_not_in, $phrases_in ); |
| 79 | |
| 80 | $phrases_in = [ 'paczkomat', 'paczka w weekend', 'inpost' ]; |
| 81 | $this->create_inpost_contextual_info( $phrases_in ); |
| 82 | $other_phrases_not_in = $this->merge_phrases( $other_phrases_not_in, $phrases_in ); |
| 83 | } elseif ( $this->is_base_location_country_gb() ) { |
| 84 | $phrases_in = [ 'air', 'dpd' ]; |
| 85 | $this->create_dpd_uk_contextual_info( $phrases_in ); |
| 86 | $other_phrases_not_in = $this->merge_phrases( $other_phrases_not_in, $phrases_in ); |
| 87 | } elseif ( $this->is_base_location_country_us() ) { |
| 88 | $phrases_in = [ 'usps' ]; |
| 89 | $this->create_usps_contextual_info( $phrases_in ); |
| 90 | $other_phrases_not_in = $this->merge_phrases( $other_phrases_not_in, $phrases_in ); |
| 91 | } |
| 92 | |
| 93 | $this->create_default_contextual_info( $other_phrases_not_in ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @return array |
| 98 | */ |
| 99 | private function get_dhl_express_phrases() { |
| 100 | if ( $this->is_base_location_country_pl() ) { |
| 101 | return [ 'dhl express' ]; |
| 102 | } else { |
| 103 | return [ 'dhl', 'dhl express' ]; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @return bool |
| 109 | */ |
| 110 | private function is_base_location_country_pl() { |
| 111 | return 'PL' === $this->base_location_country; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @return bool |
| 116 | */ |
| 117 | private function is_base_location_country_us() { |
| 118 | return stripos( $this->base_location_country, 'US' ) !== false; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @return bool |
| 123 | */ |
| 124 | private function is_base_location_country_gb() { |
| 125 | return 'GB' === $this->base_location_country; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @param array $phrases1 . |
| 130 | * @param array $phrases2 . |
| 131 | * |
| 132 | * @return array |
| 133 | */ |
| 134 | private function merge_phrases( array $phrases1, array $phrases2 ) { |
| 135 | return array_merge( $phrases1, $phrases2 ); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @param array $phrases_in . |
| 140 | */ |
| 141 | private function create_dhl_express_contextual_info( array $phrases_in ) { |
| 142 | if ( ! defined( 'FLEXIBLE_SHIPPING_DHL_EXPRESS_VERSION' ) && ! defined( 'FLEXIBLE_SHIPPING_DHL_EXPRESS_PRO_VERSION' ) ) { |
| 143 | $this->add_hookable( |
| 144 | new Flexible_Shipping_Contextual_Info( |
| 145 | self::METHOD_TITLE_AND_METHOD_DESCRIPTION_ELEMENTS, |
| 146 | 'dhl_express', |
| 147 | $phrases_in, |
| 148 | sprintf( |
| 149 | // Translators: link. |
| 150 | __( 'Want to show your customers the DHL Express live rates? %1$sCheck our DHL Express plugin%2$s', 'flexible-shipping' ), |
| 151 | '<a class="oct-btn-more" href="https://octol.io/fs-cross-dhl-express" target="_blank">', |
| 152 | '</a>' |
| 153 | ) |
| 154 | ) |
| 155 | ); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @param array $phrases_in . |
| 161 | */ |
| 162 | private function create_fedex_contextual_info( array $phrases_in ) { |
| 163 | if ( ! defined( 'FLEXIBLE_SHIPPING_FEDEX_VERSION' ) && ! defined( 'FLEXIBLE_SHIPPING_FEDEX_PRO_VERSION' ) ) { |
| 164 | $target_url = $this->is_base_location_country_pl() |
| 165 | ? 'https://octol.io/fs-cross-fedex-pl' |
| 166 | : 'https://octol.io/fs-cross-fedex'; |
| 167 | $this->add_hookable( |
| 168 | new Flexible_Shipping_Contextual_Info( |
| 169 | self::METHOD_TITLE_AND_METHOD_DESCRIPTION_ELEMENTS, |
| 170 | 'fedex', |
| 171 | $phrases_in, |
| 172 | sprintf( |
| 173 | // Translators: link. |
| 174 | __( 'Want to show your customers the FedEx live rates? %1$sCheck our FedEx plugin%2$s', 'flexible-shipping' ), |
| 175 | '<a class="oct-btn-more" href="' . $target_url . '" target="_blank">', |
| 176 | '</a>' |
| 177 | ) |
| 178 | ) |
| 179 | ); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @param array $phrases_in . |
| 185 | */ |
| 186 | private function create_ups_contextual_info( array $phrases_in ) { |
| 187 | if ( ! defined( 'WOOCOMMERCE_UPS_LABELS_VERSION' ) ) { |
| 188 | $target_url = get_user_locale() === 'pl_PL' |
| 189 | ? 'https://octol.io/fs-cross-ups-labels-pl' |
| 190 | : 'https://octol.io/fs-cross-ups-labels'; |
| 191 | $this->add_hookable( |
| 192 | new Flexible_Shipping_Contextual_Info( |
| 193 | self::METHOD_TITLE_AND_METHOD_DESCRIPTION_ELEMENTS, |
| 194 | 'ups', |
| 195 | $phrases_in, |
| 196 | sprintf( |
| 197 | // Translators: link. |
| 198 | __( 'Sending your products with UPS? Create the shipments and generate shipping labels directly from your shop using our %1$sUPS Labels%2$s', 'flexible-shipping' ), |
| 199 | '<a class="oct-btn-more" href="' . $target_url . '" target="_blank">', |
| 200 | '</a>' |
| 201 | ) |
| 202 | ) |
| 203 | ); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * @param array $phrases_in . |
| 209 | */ |
| 210 | private function create_usps_contextual_info( array $phrases_in ) { |
| 211 | if ( ! defined( 'FLEXIBLE_SHIPPING_USPS_PRO_VERSION' ) ) { |
| 212 | $target_url = 'https://octol.io/fs-cross-usps'; |
| 213 | |
| 214 | $this->add_hookable( |
| 215 | new Flexible_Shipping_Contextual_Info( |
| 216 | self::METHOD_TITLE_AND_METHOD_DESCRIPTION_ELEMENTS, |
| 217 | 'usps', |
| 218 | $phrases_in, |
| 219 | sprintf( |
| 220 | // Translators: link. |
| 221 | __( 'Want to show your customers the USPS live rates? %1$sCheck our USPS plugin%2$s', 'flexible-shipping' ), |
| 222 | '<a class="oct-btn-more" href="' . esc_url( $target_url ) . '" target="_blank">', |
| 223 | '</a>' |
| 224 | ) |
| 225 | ) |
| 226 | ); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * @param array $phrases_in . |
| 232 | */ |
| 233 | private function create_dpd_contextual_info( array $phrases_in ) { |
| 234 | if ( ! defined( 'WOOCOMMERCE_DPD_VERSION' ) ) { |
| 235 | $this->add_hookable( |
| 236 | new Flexible_Shipping_Contextual_Info( |
| 237 | self::METHOD_TITLE_AND_METHOD_DESCRIPTION_ELEMENTS, |
| 238 | 'dpd', |
| 239 | $phrases_in, |
| 240 | sprintf( |
| 241 | // Translators: link. |
| 242 | __( 'Sending your products via DPD? Create the shipments and generate shipping labels directly from your shop using our %1$sDPD integration%2$s', 'flexible-shipping' ), |
| 243 | '<a class="oct-btn-more" href="https://octol.io/fs-context-cross-dpd-pl" target="_blank">', |
| 244 | '</a>' |
| 245 | ) |
| 246 | ) |
| 247 | ); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * @param array $phrases_in . |
| 253 | */ |
| 254 | private function create_enadawca_contextual_info( array $phrases_in ) { |
| 255 | if ( ! defined( 'WOOCOMMERCE_ENADAWCA_VERSION' ) ) { |
| 256 | $this->add_hookable( |
| 257 | new Flexible_Shipping_Contextual_Info( |
| 258 | self::METHOD_TITLE_AND_METHOD_DESCRIPTION_ELEMENTS, |
| 259 | 'enadawca', |
| 260 | $phrases_in, |
| 261 | sprintf( |
| 262 | // Translators: link. |
| 263 | __( 'Sending your products via Poczta Polska? Create the shipments and generate shipping labels directly from your shop using our %1$sPoczta Polska eNadawca integration%2$s', 'flexible-shipping' ), |
| 264 | '<a class="oct-btn-more" href="https://octol.io/fs-context-cross-enadawca-pl" target="_blank">', |
| 265 | '</a>' |
| 266 | ) |
| 267 | ) |
| 268 | ); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * @param array $phrases_in . |
| 274 | */ |
| 275 | private function create_dhl_contextual_info( array $phrases_in ) { |
| 276 | if ( ! defined( 'WOOCOMMERCE_DHL_VERSION' ) ) { |
| 277 | $this->add_hookable( |
| 278 | new Flexible_Shipping_Contextual_Info( |
| 279 | self::METHOD_TITLE_AND_METHOD_DESCRIPTION_ELEMENTS, |
| 280 | 'dhl', |
| 281 | $phrases_in, |
| 282 | sprintf( |
| 283 | // Translators: link. |
| 284 | __( 'Sending your products via DHL? Create the shipments and generate shipping labels directly from your shop using our %1$sDHL integration%2$s', 'flexible-shipping' ), |
| 285 | '<a class="oct-btn-more" href="https://octol.io/fs-context-cross-dhl-pl" target="_blank">', |
| 286 | '</a>' |
| 287 | ) |
| 288 | ) |
| 289 | ); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * @param array $phrases_in . |
| 295 | */ |
| 296 | private function create_pwr_contextual_info( array $phrases_in ) { |
| 297 | if ( ! defined( 'WOOCOMMERCE_PACZKA_W_RUCHU_VERSION' ) ) { |
| 298 | $this->add_hookable( |
| 299 | new Flexible_Shipping_Contextual_Info( |
| 300 | self::METHOD_TITLE_AND_METHOD_DESCRIPTION_ELEMENTS, |
| 301 | 'pwr', |
| 302 | $phrases_in, |
| 303 | sprintf( |
| 304 | // Translators: link. |
| 305 | __( 'Sending your products via Orlen Paczka? Create the shipments and generate shipping labels directly from your shop using our %1$sOrlen Paczka integration%2$s', 'flexible-shipping' ), |
| 306 | '<a class="oct-btn-more" href="https://octol.io/fs-context-cross-op-pl" target="_blank">', |
| 307 | '</a>' |
| 308 | ) |
| 309 | ) |
| 310 | ); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * @param array $phrases_in . |
| 316 | */ |
| 317 | private function create_inpost_contextual_info( array $phrases_in ) { |
| 318 | if ( ! defined( 'WOOCOMMERCE_PACZKOMATY_INPOST_VERSION' ) ) { |
| 319 | $this->add_hookable( |
| 320 | new Flexible_Shipping_Contextual_Info( |
| 321 | self::METHOD_TITLE_AND_METHOD_DESCRIPTION_ELEMENTS, |
| 322 | 'inpost', |
| 323 | $phrases_in, |
| 324 | sprintf( |
| 325 | // Translators: link. |
| 326 | __( 'Sending your products via InPost? Create the shipments and generate shipping labels directly from your shop using our %1$sInPost integration%2$s', 'flexible-shipping' ), |
| 327 | '<a class="oct-btn-more" href="https://octol.io/fs-context-cross-inpost-pl" target="_blank">', |
| 328 | '</a>' |
| 329 | ) |
| 330 | ) |
| 331 | ); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * @param array $phrases_in . |
| 337 | */ |
| 338 | private function create_dpd_uk_contextual_info( array $phrases_in ) { |
| 339 | if ( ! defined( 'WOOCOMMERCE_DPD_UK_VERSION' ) ) { |
| 340 | $this->add_hookable( |
| 341 | new Flexible_Shipping_Contextual_Info( |
| 342 | self::METHOD_TITLE_AND_METHOD_DESCRIPTION_ELEMENTS, |
| 343 | 'dpd_uk', |
| 344 | $phrases_in, |
| 345 | sprintf( |
| 346 | // Translators: link. |
| 347 | __( 'Sending your products via DPD UK? Create the shipments and generate shipping labels directly from your shop using our %1$sDPD UK integration%2$s', 'flexible-shipping' ), |
| 348 | '<a class="oct-btn-more" href="https://octol.io/fs-cross-dpd-uk" target="_blank">', |
| 349 | '</a>' |
| 350 | ) |
| 351 | ) |
| 352 | ); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Crate default contextual info. |
| 358 | * |
| 359 | * @param array $phrases_not_in . |
| 360 | */ |
| 361 | private function create_default_contextual_info( array $phrases_not_in ) { |
| 362 | $this->add_hookable( |
| 363 | new Flexible_Shipping_Contextual_Info( |
| 364 | self::METHOD_TITLE_ELEMENT, |
| 365 | 'other', |
| 366 | [], |
| 367 | $this->create_html_for_default_contextual_info(), |
| 368 | $phrases_not_in |
| 369 | ) |
| 370 | ); |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * @return string |
| 375 | */ |
| 376 | private function create_html_for_default_contextual_info() { |
| 377 | if ( $this->is_base_location_country_pl() ) { |
| 378 | return __( 'Check our further shipping integrations with DPD, DHL, InPost, eNadawca and Orlen Paczka.', 'flexible-shipping' ) . ' ' . |
| 379 | sprintf( |
| 380 | // Translators: link. |
| 381 | __( '%1$sAdd integrations%2$s', 'flexible-shipping' ), |
| 382 | '<a class="oct-btn-more" href="https://octol.io/fs-context-cross-shipping-integrations-pl" target="_blank">', |
| 383 | '</a>' |
| 384 | ); |
| 385 | } elseif ( $this->is_base_location_country_gb() ) { |
| 386 | return __( 'Check our further shipping integration with DPD UK and FedEx / UPS live rates plugins.', 'flexible-shipping' ) . ' ' . |
| 387 | sprintf( |
| 388 | // Translators: link. |
| 389 | __( '%1$sAdd integration%2$s', 'flexible-shipping' ), |
| 390 | '<a class="oct-btn-more" href="https://octol.io/fs-integrations" target="_blank">', |
| 391 | '</a>' |
| 392 | ); |
| 393 | } elseif ( ! defined( 'FLEXIBLE_SHIPPING_PRO_VERSION' ) ) { |
| 394 | return __( 'Upgrade from the free version to the Flexible Shipping PRO and create even the most complex shipping scenarios with ease.', 'flexible-shipping' ) . ' ' . |
| 395 | sprintf( |
| 396 | // Translators: link. |
| 397 | __( '%1$sUpgrade Now%2$s', 'flexible-shipping' ), |
| 398 | '<a class="oct-btn-more" href="https://octol.io/fs-context-fs-pro" target="_blank">', |
| 399 | '</a>' |
| 400 | ); |
| 401 | } else { |
| 402 | return ''; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | } |
| 407 |