| @@ -1067,21 +1067,32 @@ | ||
| 1067 | 1067 | function automatorwp_utilities_number_condition_field() { |
| 1068 | 1068 | return array( |
| 1069 | 1069 | 'name' => __( 'Condition:', 'automatorwp' ), |
| 1070 | 1070 | 'type' => 'select', |
| 1071 | - 'options' => array( | |
| 1072 | - 'equal' => __( 'equal to', 'automatorwp' ), | |
| 1073 | - 'not_equal' => __( 'not equal to', 'automatorwp' ), | |
| 1074 | - 'less_than' => __( 'less than', 'automatorwp' ), | |
| 1075 | - 'greater_than' => __( 'greater than', 'automatorwp' ), | |
| 1076 | - 'less_or_equal' => __( 'less or equal to', 'automatorwp' ), | |
| 1077 | - 'greater_or_equal' => __( 'greater or equal to', 'automatorwp' ), | |
| 1078 | - ), | |
| 1071 | + 'options' => automatorwp_number_condition_options(), | |
| 1079 | 1072 | 'default' => 'equal' |
| 1080 | 1073 | ); |
| 1081 | 1074 | } |
| 1082 | 1075 | |
| 1083 | 1076 | /** |
| 1077 | + * Utility function to get the number condition options | |
| 1078 | + * | |
| 1079 | + * @since 6.0.0 | |
| 1080 | + * | |
| 1081 | + * @return array | |
| 1082 | + */ | |
| 1083 | +function automatorwp_number_condition_options() { | |
| 1084 | + return array( | |
| 1085 | + 'equal' => __( 'equal to', 'automatorwp' ), | |
| 1086 | + 'not_equal' => __( 'not equal to', 'automatorwp' ), | |
| 1087 | + 'less_than' => __( 'less than', 'automatorwp' ), | |
| 1088 | + 'greater_than' => __( 'greater than', 'automatorwp' ), | |
| 1089 | + 'less_or_equal' => __( 'less or equal to', 'automatorwp' ), | |
| 1090 | + 'greater_or_equal' => __( 'greater or equal to', 'automatorwp' ), | |
| 1091 | + ); | |
| 1092 | +} | |
| 1093 | + | |
| 1094 | +/** | |
| 1084 | 1095 | * Utility function to get the string condition option parameter |
| 1085 | 1096 | * |
| 1086 | 1097 | * @since 1.4.5 |
| 1087 | 1098 | * |
| @@ -1919,6 +1930,82 @@ | ||
| 1919 | 1930 | |
| 1920 | 1931 | $user_fields = apply_filters( 'automatorwp_utilities_get_user_fields', $user_fields ); |
| 1921 | 1932 | |
| 1922 | 1933 | return $user_fields; |
| 1934 | + | |
| 1935 | +} | |
| 1936 | + | |
| 1937 | +/** | |
| 1938 | + * Get all public post types slugs | |
| 1939 | + * | |
| 1940 | + * @since 6.0.0 | |
| 1941 | + * | |
| 1942 | + * @return array | |
| 1943 | + */ | |
| 1944 | +function automatorwp_get_post_types_slugs() { | |
| 1945 | + $post_types = get_post_types( array( 'public' => true ), 'objects' ); | |
| 1946 | + | |
| 1947 | + return is_array( $post_types ) ? array_keys( $post_types ) : array(); | |
| 1948 | +} | |
| 1949 | + | |
| 1950 | +/** | |
| 1951 | + * Validates if value is in array ensuring to return a valid entry from the array | |
| 1952 | + * | |
| 1953 | + * @since 6.0.0 | |
| 1954 | + * | |
| 1955 | + * @param string $value | |
| 1956 | + * @param array $array | |
| 1957 | + * @param mixed $default | |
| 1958 | + * | |
| 1959 | + * @return mixed | |
| 1960 | + */ | |
| 1961 | +function automatorwp_validate_from_array( $value, $array, $default = '' ) { | |
| 1962 | + return is_array( $array ) && in_array( $value, $array ) ? $value : $default; | |
| 1963 | +} | |
| 1964 | + | |
| 1965 | +/** | |
| 1966 | + * Get a random array entry for associative arrays | |
| 1967 | + * For arrays without keys, use PHP array_rand() instead | |
| 1968 | + * | |
| 1969 | + * @since 6.0.0 | |
| 1970 | + * | |
| 1971 | + * @param array $array | |
| 1972 | + * @param mixed $default | |
| 1973 | + * | |
| 1974 | + * @return mixed | |
| 1975 | + */ | |
| 1976 | +function automatorwp_assoc_array_rand( $array, $default = false ) { | |
| 1977 | + | |
| 1978 | + if( is_array( $array ) && count( $array ) ) { | |
| 1979 | + $keys = array_keys( $array ); | |
| 1980 | + | |
| 1981 | + if( count( $keys ) === 1 ) { | |
| 1982 | + return $array[ $keys[0] ]; | |
| 1983 | + } else { | |
| 1984 | + return $array[ $keys[ array_rand( $keys ) ] ]; | |
| 1985 | + } | |
| 1986 | + } | |
| 1987 | + | |
| 1988 | + return $default; | |
| 1989 | +} | |
| 1990 | + | |
| 1991 | +/** | |
| 1992 | + * Join a string with a natural language conjunction at the end (one, two, three or four). | |
| 1993 | + * | |
| 1994 | + * @since 6.0.0 | |
| 1995 | + * | |
| 1996 | + * @param array $array Array of words | |
| 1997 | + * @param string $join and|or | |
| 1998 | + * | |
| 1999 | + * @return string | |
| 2000 | + */ | |
| 2001 | +function automatorwp_join_words( $array, $join = '' ) { | |
| 2002 | + | |
| 2003 | + if( $join === '' ) $join = __( 'and', 'automatorwp' ); | |
| 2004 | + | |
| 2005 | + $last = array_pop($array); | |
| 2006 | + | |
| 2007 | + if ($array) return implode(', ', $array) . ' ' . $join . ' ' . $last; | |
| 2008 | + | |
| 2009 | + return $last; | |
| 1923 | 2010 | |
| 1924 | 2011 | } |