|
| 1 | +<?php |
| 2 | +if ( ! class_exists( 'WPLF_Polylang' ) ) { |
| 3 | + class WPLF_Dynamic_Values { |
| 4 | + |
| 5 | + public static $instance; |
| 6 | + protected $regular_expression = "/%[^%%\n]+%/"; |
| 7 | + |
| 8 | + public static function init() { |
| 9 | + if ( is_null( self::$instance ) ) { |
| 10 | + self::$instance = new WPLF_Dynamic_Values(); |
| 11 | + } |
| 12 | + return self::$instance; |
| 13 | + } |
| 14 | + |
| 15 | + /** |
| 16 | + * Hook our actions, filters and such |
| 17 | + */ |
| 18 | + public function __construct() { |
| 19 | + add_filter( 'wplf_form', array( $this, 'render_form' ), 10, 4 ); |
| 20 | + } |
| 21 | + |
| 22 | + public function render_form( $content, $id, $xclass, $attributes ) { |
| 23 | + // Get all strings inside % placeholders |
| 24 | + preg_match_all( $this->regular_expression, $content, $matches ); |
| 25 | + foreach ( $matches[0] as $match ) { |
| 26 | + // match contains the % chars, get rid of them. |
| 27 | + $string = trim( str_replace( array( '%' ), array( '' ), $match ) ); |
| 28 | + $content = str_replace( |
| 29 | + $match, |
| 30 | + $this->populate_value( |
| 31 | + $string, |
| 32 | + compact( 'content', 'id', 'xclass', 'attributes' ) |
| 33 | + ), |
| 34 | + $content |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + return $content; |
| 39 | + } |
| 40 | + |
| 41 | + public static function get_available() { |
| 42 | + $register = function( $value, $callback, $labels = array() ) use ( &$available ) { |
| 43 | + if ( ! is_callable( $callback ) ) { |
| 44 | + throw new Exception( '$callback is not callable' ); |
| 45 | + } |
| 46 | + |
| 47 | + $available[ $value ] = [ |
| 48 | + 'callback' => $callback, |
| 49 | + 'labels' => array_merge([ |
| 50 | + 'name' => $value, |
| 51 | + 'description' => esc_html__( 'No description provided', 'wp-libre-form' ), |
| 52 | + ], $labels), |
| 53 | + ]; |
| 54 | + |
| 55 | + return $available; |
| 56 | + }; |
| 57 | + |
| 58 | + return apply_filters( 'wplf_dynamic_values', array_merge( |
| 59 | + $register('USER_ID', 'get_current_user_id', [ |
| 60 | + 'name' => esc_html__( 'User ID', 'wp-libre-form' ), |
| 61 | + 'description' => esc_html__( 'Get current user ID. Prints 0 if user isn\'t logged in.', 'wp-libre-form' ), |
| 62 | + ]), |
| 63 | + $register('USER_EMAIL', function () { |
| 64 | + $user = wp_get_current_user(); |
| 65 | + |
| 66 | + if ( $user->ID === 0 ) { |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + return $user->user_email; |
| 71 | + }, [ |
| 72 | + 'name' => esc_html__( 'User email', 'wp-libre-form' ), |
| 73 | + 'description' => esc_html__( 'Get user email, if it exists.', 'wp-libre-form' ), |
| 74 | + ]), |
| 75 | + $register('USER_NAME', function () { |
| 76 | + $user = wp_get_current_user(); |
| 77 | + |
| 78 | + if ( $user->ID === 0 ) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + return "{$user->first_name} {$user->last_name}"; |
| 83 | + }, [ |
| 84 | + 'name' => esc_html__( 'User name', 'wp-libre-form' ), |
| 85 | + 'description' => esc_html__( 'Get user name, if it exists.', 'wp-libre-form' ), |
| 86 | + ]), |
| 87 | + $register('TIMESTAMP', function () { |
| 88 | + return date( 'U' ); |
| 89 | + }, [ |
| 90 | + 'name' => esc_html__( 'Timestamp', 'wp-libre-form' ), |
| 91 | + 'description' => esc_html__( |
| 92 | + 'Get UNIX epoch at the time of form render. Can be used to determine how long did it take for the user to fill the form.' |
| 93 | + ), |
| 94 | + ]) |
| 95 | + ) ); |
| 96 | + } |
| 97 | + |
| 98 | + public function populate_value( $string, $data = [] ) { |
| 99 | + $available = self::get_available(); |
| 100 | + |
| 101 | + if ( ! empty( $available[ $string ] ) && is_callable( $available[ $string ]['callback'] ) ) { |
| 102 | + return $available[ $string ]['callback']($data); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments