class-activation.php
3 years ago
class-admin-interface.php
3 years ago
class-common-methods.php
3 years ago
class-content-management.php
3 years ago
class-custom-code.php
3 years ago
class-deactivation.php
3 years ago
class-disable-components.php
3 years ago
class-login-logout.php
3 years ago
class-optimizations.php
3 years ago
class-security.php
3 years ago
class-settings-fields-render.php
3 years ago
class-settings-sanitization.php
3 years ago
class-settings-sections-fields.php
3 years ago
class-utilities.php
3 years ago
class-common-methods.php
171 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ASENHA\Classes; |
| 4 | |
| 5 | /** |
| 6 | * Class that provides common methods used throughout the plugin |
| 7 | * |
| 8 | * @since 2.5.0 |
| 9 | */ |
| 10 | class Common_Methods |
| 11 | { |
| 12 | /** |
| 13 | * Get IP of the current visitor/user. In use by at least the Limit Login Attempts feature. |
| 14 | * |
| 15 | * @since 2.5.0 |
| 16 | */ |
| 17 | public function get_user_ip_address() |
| 18 | { |
| 19 | |
| 20 | if ( isset( $_SERVER['REMOTE_ADDR'] ) ) { |
| 21 | $ip_address = sanitize_text_field( $_SERVER['REMOTE_ADDR'] ); |
| 22 | } elseif ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { |
| 23 | $ip_address = sanitize_text_field( $_SERVER['HTTP_X_FORWARDED_FOR'] ); |
| 24 | } elseif ( isset( $_SERVER['HTTP_CLIENT_IP'] ) ) { |
| 25 | $ip_address = sanitize_text_field( $_SERVER['HTTP_CLIENT_IP'] ); |
| 26 | } else { |
| 27 | $ip_address = '0.0.0.0'; |
| 28 | } |
| 29 | |
| 30 | return $ip_address; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Convert number of seconds into hours, minutes, seconds. In use by at least the Limit Login Attempts feature. |
| 35 | * |
| 36 | * @since 2.5.0 |
| 37 | */ |
| 38 | public function seconds_to_period( $seconds, $conversion_type ) |
| 39 | { |
| 40 | $period_start = new \DateTime( '@0' ); |
| 41 | $period_end = new \DateTime( "@{$seconds}" ); |
| 42 | |
| 43 | if ( $conversion_type == 'to-days-hours-minutes-seconds' ) { |
| 44 | return $period_start->diff( $period_end )->format( '%a days, %h hours, %i minutes and %s seconds' ); |
| 45 | } elseif ( $conversion_type == 'to-hours-minutes-seconds' ) { |
| 46 | return $period_start->diff( $period_end )->format( '%h hours, %i minutes and %s seconds' ); |
| 47 | } elseif ( $conversion_type == 'to-minutes-seconds' ) { |
| 48 | return $period_start->diff( $period_end )->format( '%i minutes and %s seconds' ); |
| 49 | } else { |
| 50 | return $period_start->diff( $period_end )->format( '%a days, %h hours, %i minutes and %s seconds' ); |
| 51 | } |
| 52 | |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Remove html tags and content inside the tags from a string |
| 57 | * |
| 58 | * @since 3.0.3 |
| 59 | */ |
| 60 | public function strip_html_tags_and_content( $string ) |
| 61 | { |
| 62 | // Strip HTML tags and content inside them. Ref: https://stackoverflow.com/a/39320168 |
| 63 | $string = preg_replace( '@<(\\w+)\\b.*?>.*?</\\1>@si', '', $string ); |
| 64 | // Strip any remaining HTML or PHP tags |
| 65 | $string = strip_tags( $string ); |
| 66 | return $string; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get menu hidden by toggle |
| 71 | * |
| 72 | * @since 5.1.0 |
| 73 | */ |
| 74 | public function get_menu_hidden_by_toggle() |
| 75 | { |
| 76 | $menu_hidden_by_toggle = array(); |
| 77 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 78 | |
| 79 | if ( array_key_exists( 'custom_menu_hidden', $options ) ) { |
| 80 | $menu_hidden = $options['custom_menu_hidden']; |
| 81 | $menu_hidden = explode( ',', $menu_hidden ); |
| 82 | $menu_hidden_by_toggle = array(); |
| 83 | foreach ( $menu_hidden as $menu_id ) { |
| 84 | $menu_hidden_by_toggle[] = $this->restore_menu_item_id( $menu_id ); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return $menu_hidden_by_toggle; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Get user capabilities for which the "Show All/Less" menu toggle should be shown for |
| 93 | * |
| 94 | * @since 5.1.0 |
| 95 | */ |
| 96 | public function get_user_capabilities_to_show_menu_toggle_for() |
| 97 | { |
| 98 | global $menu ; |
| 99 | $menu_always_hidden = array(); |
| 100 | $user_capabilities_menus_are_hidden_for = array(); |
| 101 | $menu_hidden_by_toggle = $this->get_menu_hidden_by_toggle(); |
| 102 | // indexed array |
| 103 | foreach ( $menu as $menu_key => $menu_info ) { |
| 104 | foreach ( $menu_hidden_by_toggle as $hidden_menu_id ) { |
| 105 | |
| 106 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 107 | $menu_item_id = $menu_info[2]; |
| 108 | } else { |
| 109 | $menu_item_id = $menu_info[5]; |
| 110 | } |
| 111 | |
| 112 | $menu_item_id_transformed = $this->transform_menu_item_id( $menu_item_id ); |
| 113 | if ( $menu_item_id_transformed == $hidden_menu_id ) { |
| 114 | $user_capabilities_menus_are_hidden_for[] = $menu_info[1]; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | $user_capabilities_menus_are_hidden_for = array_unique( $user_capabilities_menus_are_hidden_for ); |
| 119 | return $user_capabilities_menus_are_hidden_for; |
| 120 | // indexed array |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Transform menu item's ID |
| 125 | * |
| 126 | * @since 5.1.0 |
| 127 | */ |
| 128 | public function transform_menu_item_id( $menu_item_id ) |
| 129 | { |
| 130 | // Transform e.g. edit.php?post_type=page ==> edit__php___post_type____page |
| 131 | $menu_item_id_transformed = str_replace( array( |
| 132 | ".", |
| 133 | "?", |
| 134 | "=", |
| 135 | "&", |
| 136 | "/" |
| 137 | ), array( |
| 138 | "__", |
| 139 | "___", |
| 140 | "____", |
| 141 | "_____", |
| 142 | "______" |
| 143 | ), $menu_item_id ); |
| 144 | return $menu_item_id_transformed; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Transform menu item's ID |
| 149 | * |
| 150 | * @since 5.1.0 |
| 151 | */ |
| 152 | public function restore_menu_item_id( $menu_item_id_transformed ) |
| 153 | { |
| 154 | // Transform e.g. edit__php___post_type____page ==> edit.php?post_type=page |
| 155 | $menu_item_id = str_replace( array( |
| 156 | "______", |
| 157 | "_____", |
| 158 | "____", |
| 159 | "___", |
| 160 | "__" |
| 161 | ), array( |
| 162 | "/", |
| 163 | "&", |
| 164 | "=", |
| 165 | "?", |
| 166 | "." |
| 167 | ), $menu_item_id_transformed ); |
| 168 | return $menu_item_id; |
| 169 | } |
| 170 | |
| 171 | } |