| @@ -1,0 +1,142 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Functions | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Formatter\Functions | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | +// Exit if accessed directly | |
| 9 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * Get the number formats | |
| 13 | + * | |
| 14 | + * @since 1.0.0 | |
| 15 | + * | |
| 16 | + * @return array | |
| 17 | + */ | |
| 18 | +function automatorwp_formatter_get_number_formats( ) { | |
| 19 | + | |
| 20 | + $number_formats = array( | |
| 21 | + '1' => __( 'Round to integer', 'automatorwp' ), | |
| 22 | + '2' => __( 'Round to one decimal', 'automatorwp' ), | |
| 23 | + '3' => __( 'Round to two decimals', 'automatorwp' ), | |
| 24 | + '4' => __( 'Round down', 'automatorwp' ), | |
| 25 | + '5' => __( 'Round up', 'automatorwp' ), | |
| 26 | + ); | |
| 27 | + | |
| 28 | + return $number_formats; | |
| 29 | +} | |
| 30 | + | |
| 31 | +/** | |
| 32 | + * Get the formatted number | |
| 33 | + * | |
| 34 | + * @since 1.0.0 | |
| 35 | + * | |
| 36 | + * @param int $format_id | |
| 37 | + * @param string $number | |
| 38 | + * | |
| 39 | + * @return string|null | |
| 40 | + */ | |
| 41 | +function automatorwp_formatter_get_formatted_number( $format_id, $number ) { | |
| 42 | + | |
| 43 | + // Empty title if no ID provided | |
| 44 | + if( absint( $format_id ) === 0 ) { | |
| 45 | + return ''; | |
| 46 | + } | |
| 47 | + | |
| 48 | + switch ( $format_id ) { | |
| 49 | + case 1: | |
| 50 | + return round( $number, 0 ); | |
| 51 | + break; | |
| 52 | + case 2: | |
| 53 | + return round( $number, 1 ); | |
| 54 | + break; | |
| 55 | + case 3: | |
| 56 | + return round( $number, 2 ); | |
| 57 | + break; | |
| 58 | + case 4: | |
| 59 | + return floor( $number ); | |
| 60 | + break; | |
| 61 | + case 5: | |
| 62 | + return ceil( $number ); | |
| 63 | + break; | |
| 64 | + | |
| 65 | + } | |
| 66 | + | |
| 67 | +} | |
| 68 | + | |
| 69 | +/** | |
| 70 | + * Get the string formats | |
| 71 | + * | |
| 72 | + * @since 1.0.0 | |
| 73 | + * | |
| 74 | + * @return array | |
| 75 | + */ | |
| 76 | +function automatorwp_formatter_get_string_formats( ) { | |
| 77 | + | |
| 78 | + $string_formats = array( | |
| 79 | + '1' => __( 'Lowercase', 'automatorwp' ), | |
| 80 | + '2' => __( 'Uppercase', 'automatorwp' ), | |
| 81 | + '3' => __( 'Capitalize', 'automatorwp' ), | |
| 82 | + '4' => __( 'Capitalize all words', 'automatorwp' ), | |
| 83 | + '5' => __( 'Reverse', 'automatorwp' ), | |
| 84 | + '6' => __( 'Shuffle', 'automatorwp' ), | |
| 85 | + '7' => __( 'Shuffle words', 'automatorwp' ), | |
| 86 | + '8' => __( 'Slugify', 'automatorwp' ), | |
| 87 | + ); | |
| 88 | + | |
| 89 | + return $string_formats; | |
| 90 | +} | |
| 91 | + | |
| 92 | + | |
| 93 | +/** | |
| 94 | + * Get the formatted string | |
| 95 | + * | |
| 96 | + * @since 1.0.0 | |
| 97 | + * | |
| 98 | + * @param int $format_id | |
| 99 | + * @param string $string | |
| 100 | + * | |
| 101 | + * @return string|null | |
| 102 | + */ | |
| 103 | +function automatorwp_formatter_get_formatted_string( $format_id, $string ) { | |
| 104 | + | |
| 105 | + // Empty title if no ID provided | |
| 106 | + if( absint( $format_id ) === 0 ) { | |
| 107 | + return ''; | |
| 108 | + } | |
| 109 | + | |
| 110 | + switch ( $format_id ) { | |
| 111 | + case 1: | |
| 112 | + return strtolower( $string ); | |
| 113 | + break; | |
| 114 | + case 2: | |
| 115 | + return strtoupper( $string ); | |
| 116 | + break; | |
| 117 | + case 3: | |
| 118 | + return ucfirst( $string ); | |
| 119 | + break; | |
| 120 | + case 4: | |
| 121 | + return ucwords( $string ); | |
| 122 | + break; | |
| 123 | + case 5: | |
| 124 | + return strrev( $string ); | |
| 125 | + break; | |
| 126 | + case 6: | |
| 127 | + return str_shuffle( $string ); | |
| 128 | + break; | |
| 129 | + case 7: | |
| 130 | + $string_array = explode(' ', trim( $string ) ); | |
| 131 | + shuffle( $string_array ); | |
| 132 | + return implode( ' ', $string_array ); | |
| 133 | + case 8: | |
| 134 | + $string = strtolower( $string ); | |
| 135 | + $string = preg_replace('/[^a-z0-9]+/', '-', $string); | |
| 136 | + $slug = trim($string, '-'); | |
| 137 | + return $slug; | |
| 138 | + break; | |
| 139 | + | |
| 140 | + } | |
| 141 | + | |
| 142 | +} | |