flexible-shipping
/
src
/
WPDesk
/
FS
/
TableRate
/
Rule
/
SpecialAction
/
SpecialActionFieldsFactory.php
SpecialActionFieldsFactory.php
86 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class RuleSpecialActionFieldsFactory |
| 4 | * |
| 5 | * @package WPDesk\FS\TableRate\Rule\SpecialAction |
| 6 | */ |
| 7 | |
| 8 | namespace WPDesk\FS\TableRate\Rule\SpecialAction; |
| 9 | |
| 10 | use FSVendor\WPDesk\Forms\Field; |
| 11 | use FSVendor\WPDesk\Forms\Field\InputTextField; |
| 12 | use FSVendor\WPDesk\Forms\FieldProvider; |
| 13 | use FSVendor\WPDesk\Forms\Renderer\JsonNormalizedRenderer; |
| 14 | use WPDesk\FS\TableRate\Rule\SpecialAction\SpecialAction; |
| 15 | |
| 16 | /** |
| 17 | * Can create special action fields. |
| 18 | */ |
| 19 | class SpecialActionFieldsFactory implements FieldProvider { |
| 20 | |
| 21 | /** |
| 22 | * @var SpecialAction[] |
| 23 | */ |
| 24 | private $available_special_actions; |
| 25 | |
| 26 | /** |
| 27 | * RuleSpecialActionFieldsFactory constructor. |
| 28 | * |
| 29 | * @param SpecialAction[] $available_special_actions . |
| 30 | */ |
| 31 | public function __construct( array $available_special_actions ) { |
| 32 | $this->available_special_actions = $available_special_actions; |
| 33 | } |
| 34 | |
| 35 | |
| 36 | /** |
| 37 | * @return Field[] |
| 38 | */ |
| 39 | public function get_fields() { |
| 40 | return apply_filters( 'flexible_shipping_rule_special_action_fields', $this->get_built_in_rule_special_actions_fields() ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * . |
| 45 | * |
| 46 | * @return array |
| 47 | */ |
| 48 | public function get_normalized_cost_fields() { |
| 49 | $normalized_cost_fields = array(); |
| 50 | $renderer = new JsonNormalizedRenderer(); |
| 51 | |
| 52 | return $renderer->render_fields( $this, array() ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @return Field[] |
| 57 | */ |
| 58 | public function get_built_in_rule_special_actions_fields() { |
| 59 | return array( |
| 60 | ( new Field\SelectField() ) |
| 61 | ->set_name( 'special_action' ) |
| 62 | ->set_options( $this->get_special_action_options() ) |
| 63 | ->add_class( 'hs-beacon-search' ) |
| 64 | ->add_class( 'special-action' ) |
| 65 | ->add_data( 'beacon_search', __( 'special action', 'flexible-shipping' ) ), |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @return array |
| 71 | */ |
| 72 | private function get_special_action_options() { |
| 73 | $options = array(); |
| 74 | foreach ( $this->available_special_actions as $special_action ) { |
| 75 | $options[] = array( |
| 76 | 'value' => $special_action->get_special_action_id(), |
| 77 | 'label' => $special_action->get_name(), |
| 78 | 'description' => $special_action->get_description(), |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | return $options; |
| 83 | } |
| 84 | |
| 85 | } |
| 86 |