Skip to content

Commit d57134a

Browse files
committed
Make historical import field disabled when the value is 'Yes'
1 parent 4515c7a commit d57134a

File tree

2 files changed

+123
-30
lines changed

2 files changed

+123
-30
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
/**
3+
* Field for allowing the import of historical data.
4+
*
5+
* @since 2019-06-05
6+
* @author Zach Owen <zach@webdevstudios>
7+
* @package cc-woo-view-admin-field
8+
*/
9+
10+
namespace WebDevStudios\CCForWoo\View\Admin\Field;
11+
12+
/**
13+
* ImportHistoricalData clss
14+
*
15+
* @since 2019-06-05
16+
* @author Zach Owen <zach@webdevstudios>
17+
* @package cc-woo-view-admin-field
18+
*/
19+
class ImportHistoricalData {
20+
/**
21+
* Historical customer data import field.
22+
*
23+
* @since 2019-03-12
24+
*/
25+
const OPTION_FIELD_NAME = 'cc_woo_customer_data_allow_import';
26+
27+
/**
28+
* Returns the form field configuration.
29+
*
30+
* @since 2019-06-05
31+
* @author Zach Owen <zach@webdevstudios>
32+
* @return array.
33+
*/
34+
public function get_form_field() : array {
35+
return [
36+
'title' => esc_html__( 'Import historical customer data', 'cc-woo' ),
37+
'desc' => $this->get_description(),
38+
'type' => 'select',
39+
'id' => self::OPTION_FIELD_NAME,
40+
'default' => '',
41+
'custom_attributes' => $this->get_custom_attributes(),
42+
'options' => [
43+
'' => '----',
44+
'false' => esc_html__( 'No', 'cc-woo' ),
45+
'true' => esc_html__( 'Yes', 'cc-woo' ),
46+
],
47+
];
48+
}
49+
50+
/**
51+
* Get the field's custom attribute configuration.
52+
*
53+
* @TODO "$configuration" should probably be an object.
54+
* @since 2019-06-05
55+
* @author Zach Owen <zach@webdevstudios>
56+
*
57+
* @return array
58+
*/
59+
private function get_custom_attributes() : array {
60+
$attributes = [];
61+
62+
if ( $this->is_required() ) {
63+
$attributes['required'] = true;
64+
}
65+
66+
if ( $this->is_readonly() ) {
67+
$attributes['readonly'] = 'readonly';
68+
$attributes['disabled'] = 'disabled';
69+
}
70+
71+
return $attributes;
72+
}
73+
74+
/**
75+
* Return whether the field is required.
76+
*
77+
* @since 2019-06-05
78+
* @author Zach Owen <zach@webdevstudios>
79+
* @return bool
80+
*/
81+
protected function is_required() : bool {
82+
return true;
83+
}
84+
85+
/**
86+
* Returns the value of the field option.
87+
*
88+
* @since 2019-06-05
89+
* @author Zach Owen <zach@webdevstudios>
90+
* @return bool
91+
*/
92+
protected function is_readonly() : bool {
93+
return 'true' === get_option( self::OPTION_FIELD_NAME, 'false' );
94+
}
95+
96+
/**
97+
* Get the field description based on the option's readonly state.
98+
*
99+
* @since 2019-06-05
100+
* @author Zach Owen <zach@webdevstudios>
101+
* @return string
102+
*/
103+
private function get_description() : string {
104+
return $this->is_readonly() ? '' : esc_html__( 'Selecting Yes here will enable the ability to import your historical customer information to Constant Contact.', 'cc-woo' );
105+
}
106+
}

src/View/Admin/WooTab.php

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,6 @@ class WooTab extends WC_Settings_Page implements Hookable {
7676
*/
7777
const EMAIL_FIELD = 'cc_woo_store_information_contact_email';
7878

79-
/**
80-
* Historical customer data import field.
81-
*
82-
* @since 2019-03-12
83-
*/
84-
const ALLOW_HISTORICAL_CUSTOMER_IMPORT_FIELD = 'cc_woo_customer_data_allow_import';
85-
8679
/**
8780
* Settings section ID.
8881
*
@@ -503,32 +496,26 @@ private function get_customer_data_settings() {
503496
'title' => '',
504497
'type' => 'title',
505498
'desc' => esc_html__( 'All contacts must agree to receive marketing messages in order to be added to your mailing list. Therefore, when you import contacts, you are agreeing that you have permission to send them marketing messages.', 'cc-woo' ),
506-
],
499+
]
500+
];
501+
502+
$historical_import_field = new \WebDevStudios\CCForWoo\View\Admin\Field\ImportHistoricalData();
503+
504+
$settings[] = array_merge(
505+
$settings,
506+
$historical_import_field->get_form_field(),
507507
[
508-
'title' => esc_html__( 'Import historical customer data', 'cc-woo' ),
509-
'desc' => esc_html__( 'Selecting Yes here will enable the ability to import your historical customer information to Constant Contact.', 'cc-woo' ),
510-
'type' => 'select',
511-
'id' => self::ALLOW_HISTORICAL_CUSTOMER_IMPORT_FIELD,
512-
'default' => '',
513-
'custom_attributes' => [
514-
'required' => true,
508+
[
509+
'title' => '',
510+
'type' => 'cc_woo_anti_spam_notice',
511+
'id' => 'anti-spam-notice',
515512
],
516-
'options' => [
517-
'' => '----',
518-
'false' => esc_html__( 'No', 'cc-woo' ),
519-
'true' => esc_html__( 'Yes', 'cc-woo' ),
513+
[
514+
'type' => 'sectionend',
515+
'id' => 'cc_woo_customer_data_settings',
520516
],
521-
],
522-
[
523-
'title' => '',
524-
'type' => 'cc_woo_anti_spam_notice',
525-
'id' => 'anti-spam-notice',
526-
],
527-
[
528-
'type' => 'sectionend',
529-
'id' => 'cc_woo_customer_data_settings',
530-
],
531-
];
517+
]
518+
);
532519

533520
return $settings;
534521
}

0 commit comments

Comments
 (0)