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-deactivation.php
3 years ago
class-disable-components.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
109 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 | * Re-enqueue CodeMirror syntax highlighter from WordPress core |
| 14 | * |
| 15 | * @since 2.3.0 |
| 16 | */ |
| 17 | public function enqueue_codemirror_assets() { |
| 18 | |
| 19 | // Only enqueue scripts on the plugin's settings page |
| 20 | if ( is_asenha() ) { |
| 21 | |
| 22 | // CodeMirror JS |
| 23 | wp_enqueue_script( 'asenha-codemirror', ASENHA_URL . 'assets/js/codemirror/codemirror.min.js', array(), ASENHA_VERSION, true ); |
| 24 | |
| 25 | // CodeMirror CSS |
| 26 | wp_enqueue_style( 'asenha-codemirror', ASENHA_URL . 'assets/css/codemirror/codemirror.min.css', array(), ASENHA_VERSION ); |
| 27 | |
| 28 | // CSS mode for CodeMirror |
| 29 | wp_enqueue_script( 'asenha-codemirror-css-mode', ASENHA_URL . 'assets/js/codemirror/css.js', array( 'asenha-codemirror' ), ASENHA_VERSION, true ); |
| 30 | |
| 31 | } |
| 32 | |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Re-enqueue CodeMirror syntax highlighter from WordPress core |
| 37 | * |
| 38 | * @since 2.3.0 |
| 39 | */ |
| 40 | public function enqueue_datatables_assets() { |
| 41 | |
| 42 | // Only enqueue scripts on the plugin's settings page |
| 43 | if ( is_asenha() ) { |
| 44 | |
| 45 | wp_enqueue_style( 'asenha-datatables', ASENHA_URL . 'assets/css/datatables/datatables.min.css', array(), ASENHA_VERSION ); |
| 46 | wp_enqueue_script( 'asenha-datatables', ASENHA_URL . 'assets/js/datatables/datatables.min.js', array( 'jquery' ), ASENHA_VERSION, false ); |
| 47 | |
| 48 | } |
| 49 | |
| 50 | } |
| 51 | /** |
| 52 | * Get IP of the current visitor/user |
| 53 | * |
| 54 | * @since 2.5.0 |
| 55 | */ |
| 56 | public function get_user_ip_address() { |
| 57 | |
| 58 | if ( isset( $_SERVER['REMOTE_ADDR'] ) ) { |
| 59 | |
| 60 | $ip_address = sanitize_text_field( $_SERVER['REMOTE_ADDR'] ); |
| 61 | |
| 62 | } elseif ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { |
| 63 | |
| 64 | $ip_address = sanitize_text_field( $_SERVER['HTTP_X_FORWARDED_FOR'] ); |
| 65 | |
| 66 | } elseif ( isset( $_SERVER['HTTP_CLIENT_IP'] ) ) { |
| 67 | |
| 68 | $ip_address = sanitize_text_field( $_SERVER['HTTP_CLIENT_IP'] ); |
| 69 | |
| 70 | } else { |
| 71 | |
| 72 | $ip_address = '0.0.0.0'; |
| 73 | |
| 74 | } |
| 75 | |
| 76 | return $ip_address; |
| 77 | |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Convert number of seconds into hours, minutes, seconds |
| 82 | * |
| 83 | * @since 2.5.0 |
| 84 | */ |
| 85 | public function seconds_to_period( $seconds, $conversion_type ) { |
| 86 | |
| 87 | $period_start = new \DateTime('@0'); |
| 88 | $period_end = new \DateTime("@$seconds"); |
| 89 | |
| 90 | if ( $conversion_type == 'to-days-hours-minutes-seconds' ) { |
| 91 | |
| 92 | return $period_start->diff($period_end)->format('%a days, %h hours, %i minutes and %s seconds'); |
| 93 | |
| 94 | } elseif ( $conversion_type == 'to-hours-minutes-seconds' ) { |
| 95 | |
| 96 | return $period_start->diff($period_end)->format('%h hours, %i minutes and %s seconds'); |
| 97 | |
| 98 | } elseif ( $conversion_type == 'to-minutes-seconds' ) { |
| 99 | |
| 100 | return $period_start->diff($period_end)->format('%i minutes and %s seconds'); |
| 101 | |
| 102 | } else { |
| 103 | |
| 104 | return $period_start->diff($period_end)->format('%a days, %h hours, %i minutes and %s seconds'); |
| 105 | |
| 106 | } |
| 107 | |
| 108 | } |
| 109 | } |