|
| 1 | +<?php |
| 2 | +namespace Sgdg\Frontend; |
| 3 | + |
| 4 | +require_once 'class-integeroption.php'; |
| 5 | + |
| 6 | +class IntegerSelectorOption extends Option { |
| 7 | + public function __construct( $name, $default_value, $default_unit, $section, $title ) { |
| 8 | + parent::__construct( $name, [ |
| 9 | + 'value' => $default_value, |
| 10 | + 'unit' => $default_unit, |
| 11 | + ], $section, $title ); |
| 12 | + } |
| 13 | + |
| 14 | + public function register() { |
| 15 | + register_setting( 'sgdg', $this->name . '_value', [ // TODO: Upgrade hook: https://codex.wordpress.org/Plugin_API/Action_Reference/upgrader_process_complete |
| 16 | + 'type' => 'integer', |
| 17 | + 'sanitize_callback' => [ $this, 'sanitize' ], |
| 18 | + ]); |
| 19 | + register_setting( 'sgdg', $this->name . '_unit', [ |
| 20 | + 'type' => 'string', |
| 21 | + 'sanitize_callback' => [ $this, 'sanitize_unit' ], |
| 22 | + ]); |
| 23 | + } |
| 24 | + |
| 25 | + public function sanitize( $value ) { |
| 26 | + if ( is_numeric( $value ) ) { |
| 27 | + return intval( $value ); |
| 28 | + } |
| 29 | + return $this->default_value['value']; |
| 30 | + } |
| 31 | + |
| 32 | + public function sanitize_unit( $value ) { |
| 33 | + if ( 'px' === $value ) { |
| 34 | + return 'px'; |
| 35 | + } |
| 36 | + if ( 'cols' === $value ) { |
| 37 | + return 'cols'; |
| 38 | + } |
| 39 | + return $this->default_value['unit']; |
| 40 | + } |
| 41 | + |
| 42 | + public function html() { |
| 43 | + echo( '<input type="text" name="' . esc_attr( $this->name ) . '_value" value="' . esc_attr( $this->getValue() ) . '" class="regular-text">' ); |
| 44 | + echo( '<select name="' . esc_attr($this->name) . '_unit">' ); |
| 45 | + echo( '<option value="px"' . ( $this->getUnit() === 'px' ? ' selected' : '' ) . '>px</option>' ); |
| 46 | + echo( '<option value="cols"' . ( $this->getUnit() === 'cols' ? ' selected' : '' ) . '>' . esc_html__( 'columns', 'skaut-google-drive-gallery' ) . '</option>' ); |
| 47 | + echo( '</select>' ); |
| 48 | + } |
| 49 | + |
| 50 | + public function getValue( $default_value = null ) { |
| 51 | + return get_option( $this->name . '_value', ( isset( $default_value ) ? $default_value : $this->default_value['value'] ) ); |
| 52 | + } |
| 53 | + |
| 54 | + public function getUnit( $default_value = null ) { |
| 55 | + return get_option( $this->name . '_unit', ( isset( $default_value ) ? $default_value : $this->default_value['unit'] ) ); |
| 56 | + } |
| 57 | + |
| 58 | + public function getSize( $default_value = null ) { |
| 59 | + if ( $this->getUnit( $default_value['unit'] ) === 'cols' ) { |
| 60 | + return floor( 1920 / get_option( $this->name . '_value', ( isset( $default_value ) ? $default_value : $this->default_value['value'] ) ) ); |
| 61 | + } |
| 62 | + return get_option( $this->name . '_value', ( isset( $default_value ) ? $default_value : $this->default_value['value'] ) ); |
| 63 | + } |
| 64 | + |
| 65 | + public function get( $default_value = null ) { |
| 66 | + if ( $this->getUnit( $default_value['unit'] ) === 'cols' ) { |
| 67 | + return floor( 95 / $this->getValue( $default_value['value'] ) ) . '%'; |
| 68 | + } |
| 69 | + return $this->getValue( $default_value['value'] ) . $this->getUnit( $default_value['unit'] ); |
| 70 | + } |
| 71 | +} |
0 commit comments