Conditions | 11 |
Paths | 384 |
Total Lines | 55 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
80 | public function save_and_associate_contact() { |
||
81 | check_ajax_referer( 'save_and_associate_contact' ); |
||
82 | |||
83 | $third_party_id = ! empty( $_POST['parent_id'] ) ? (int) $_POST['parent_id'] : 0; |
||
84 | $contact = ! empty( $_POST['contact'] ) ? (array) $_POST['contact'] : array(); |
||
85 | $contact['id'] = ! empty( $_POST['contact']['id'] ) ? (int) $_POST['contact']['id'] : 0; |
||
86 | |||
87 | if ( empty( $contact['id'] ) ) { |
||
88 | // Dans le cas ou c'est un contact ajouté depuis la recherche. |
||
89 | $contact['id'] = ! empty( $_POST['contact_id'] ) ? (int) $_POST['contact_id'] : 0; |
||
90 | } |
||
91 | |||
92 | if ( empty( $third_party_id ) ) { |
||
93 | wp_send_json_error(); |
||
94 | } |
||
95 | |||
96 | $third_party = Third_Party::g()->get( array( 'id' => $third_party_id ), true ); |
||
97 | |||
98 | if ( empty( $contact['id'] ) ) { |
||
99 | $email = explode( '@', $contact['email'] ); |
||
100 | $contact['login'] = $email[0]; |
||
101 | $contact['user_pass'] = wp_generate_password(); |
||
102 | |||
103 | $contact = apply_filters( 'wps_save_and_associate_contact', $contact, $third_party ); |
||
104 | $contact = Contact::g()->update( $contact ); |
||
105 | } else { |
||
106 | $contact = Contact::g()->get( array( 'id' => $contact['id'] ), true ); |
||
107 | } |
||
108 | |||
109 | if ( ! in_array( $contact->data['id'], $third_party->data['contact_ids'] ) ) { |
||
110 | $third_party->data['contact_ids'][] = $contact->data['id']; |
||
111 | $contact->data['third_party'] = $third_party->data['external_id']; |
||
112 | |||
113 | $third_party = Third_Party::g()->update( $third_party->data ); |
||
114 | $contact = Contact::g()->update( $contact->data ); |
||
115 | } |
||
116 | |||
117 | do_action( 'wps_saved_and_associated_contact', $third_party, $contact, empty( $contact->data['id'] ) ? true : false ); |
||
118 | |||
119 | ob_start(); |
||
120 | $contacts = array(); |
||
121 | if ( ! empty( $third_party->data['contact_ids'] ) ) { |
||
122 | $contacts = Contact::g()->get( array( 'include' => $third_party->data['contact_ids'] ) ); |
||
123 | } |
||
124 | \eoxia\View_Util::exec( 'wpshop', 'third-parties', 'metaboxes/metabox-contacts', array( |
||
125 | 'third_party' => $third_party, |
||
126 | 'contacts' => $contacts, |
||
127 | ) ); |
||
128 | wp_send_json_success( array( |
||
129 | 'namespace' => 'wpshop', |
||
130 | 'module' => 'thirdParties', |
||
131 | 'callback_success' => 'associatedContactSuccess', |
||
132 | 'view' => ob_get_clean(), |
||
133 | ) ); |
||
134 | } |
||
135 | |||
208 |