Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 20 | class Give_Email_Notification_Util { |
||
|
|
|||
| 21 | /** |
||
| 22 | * Instance. |
||
| 23 | * |
||
| 24 | * @since 2.0 |
||
| 25 | * @access static |
||
| 26 | * @var |
||
| 27 | */ |
||
| 28 | static private $instance; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Singleton pattern. |
||
| 32 | * |
||
| 33 | * @since 2.0 |
||
| 34 | * @access private |
||
| 35 | * Give_Email_Notification_Util constructor. |
||
| 36 | */ |
||
| 37 | private function __construct() { |
||
| 39 | |||
| 40 | |||
| 41 | /** |
||
| 42 | * Get instance. |
||
| 43 | * |
||
| 44 | * @since 2.0 |
||
| 45 | * @access static |
||
| 46 | * @return static |
||
| 47 | */ |
||
| 48 | static function get_instance() { |
||
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * Check if notification has preview field or not. |
||
| 59 | * |
||
| 60 | * @since 2.0 |
||
| 61 | * @access public |
||
| 62 | * |
||
| 63 | * @param Give_Email_Notification $email |
||
| 64 | * |
||
| 65 | * @return bool |
||
| 66 | */ |
||
| 67 | public static function has_preview( Give_Email_Notification $email ) { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Check if notification has recipient field or not. |
||
| 73 | * |
||
| 74 | * @since 2.0 |
||
| 75 | * @access public |
||
| 76 | * |
||
| 77 | * @param Give_Email_Notification $email |
||
| 78 | * |
||
| 79 | * @return bool |
||
| 80 | */ |
||
| 81 | public static function has_recipient_field( Give_Email_Notification $email ) { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Check if admin can edit notification status or not. |
||
| 87 | * |
||
| 88 | * @since 2.0 |
||
| 89 | * @access public |
||
| 90 | * |
||
| 91 | * @param Give_Email_Notification $email |
||
| 92 | * |
||
| 93 | * @return bool |
||
| 94 | */ |
||
| 95 | public static function is_notification_status_editable( Give_Email_Notification $email ) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Check if admin can edit notification status or not. |
||
| 101 | * |
||
| 102 | * @since 2.0 |
||
| 103 | * @access public |
||
| 104 | * |
||
| 105 | * @param Give_Email_Notification $email |
||
| 106 | * |
||
| 107 | * @return bool |
||
| 108 | */ |
||
| 109 | public static function is_content_type_editable( Give_Email_Notification $email ) { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Check email preview header active or not. |
||
| 115 | * |
||
| 116 | * @since 2.0 |
||
| 117 | * @access public |
||
| 118 | * |
||
| 119 | * @param Give_Email_Notification $email |
||
| 120 | * |
||
| 121 | * @return bool |
||
| 122 | */ |
||
| 123 | public static function is_email_preview_has_header( Give_Email_Notification $email ) { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Check email preview header active or not. |
||
| 129 | * |
||
| 130 | * @since 2.0 |
||
| 131 | * @access public |
||
| 132 | * |
||
| 133 | * @param Give_Email_Notification $email |
||
| 134 | * |
||
| 135 | * @return bool |
||
| 136 | */ |
||
| 137 | public static function is_email_preview( Give_Email_Notification $email ) { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Check if email notification setting appear on emails setting page or not. |
||
| 143 | * |
||
| 144 | * @since 2.0 |
||
| 145 | * @access public |
||
| 146 | * |
||
| 147 | * @param Give_Email_Notification $email |
||
| 148 | * |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | public static function is_show_on_emails_setting_page( Give_Email_Notification $email ){ |
||
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * Check email active or not. |
||
| 158 | * |
||
| 159 | * @since 2.0 |
||
| 160 | * @access public |
||
| 161 | * |
||
| 162 | * @param Give_Email_Notification $email |
||
| 163 | * @param int $form_id |
||
| 164 | * |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | public static function is_email_notification_active( Give_Email_Notification $email, $form_id = null ) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Check if admin preview email or not |
||
| 181 | * |
||
| 182 | * @since 2.0 |
||
| 183 | * @access public |
||
| 184 | * @return bool $is_preview |
||
| 185 | */ |
||
| 186 | View Code Duplication | public static function can_preview_email() { |
|
| 187 | $is_preview = false; |
||
| 188 | |||
| 189 | if ( |
||
| 190 | current_user_can( 'manage_give_settings' ) |
||
| 191 | && ! empty( $_GET['give_action'] ) |
||
| 192 | && 'preview_email' === $_GET['give_action'] |
||
| 193 | ) { |
||
| 194 | $is_preview = true; |
||
| 195 | } |
||
| 196 | |||
| 197 | return $is_preview; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Check if admin preview email or not |
||
| 202 | * |
||
| 203 | * @since 2.0 |
||
| 204 | * @access public |
||
| 205 | * @return bool $is_preview |
||
| 206 | */ |
||
| 207 | View Code Duplication | public static function can_send_preview_email() { |
|
| 208 | $is_preview = false; |
||
| 209 | |||
| 210 | if ( |
||
| 211 | current_user_can( 'manage_give_settings' ) |
||
| 212 | && ! empty( $_GET['give_action'] ) |
||
| 213 | && 'send_preview_email' === $_GET['give_action'] |
||
| 214 | ) { |
||
| 215 | $is_preview = true; |
||
| 216 | } |
||
| 217 | |||
| 218 | return $is_preview; |
||
| 219 | } |
||
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * Get formatted text for email content type. |
||
| 224 | * |
||
| 225 | * @since 2.0 |
||
| 226 | * @access public |
||
| 227 | * |
||
| 228 | * @param string $content_type |
||
| 229 | * |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | public static function get_formatted_email_type( $content_type ) { |
||
| 240 | |||
| 241 | |||
| 242 | /** |
||
| 243 | * Get email notification option value. |
||
| 244 | * |
||
| 245 | * @since 2.0 |
||
| 246 | * @access public |
||
| 247 | * |
||
| 248 | * @param Give_Email_Notification $email |
||
| 249 | * @param string $option_name |
||
| 250 | * @param int $form_id |
||
| 251 | * @param mixed $default |
||
| 252 | * |
||
| 253 | * @return mixed |
||
| 254 | */ |
||
| 255 | public static function get_value( Give_Email_Notification $email, $option_name, $form_id = null, $default = false ) { |
||
| 279 | } |
||
| 280 |