| 1 |
<?php |
| 2 |
/** |
| 3 |
* Utility functions for use when rendering form |
| 4 |
* |
| 5 |
* @package Caldera_Forms Modified by QuantumCloud |
| 6 |
* @author Josh Pollock <Josh@CalderaWP.com> |
| 7 |
* @license GPL-2.0+ |
| 8 |
* @link |
| 9 |
* @copyright 2016 CalderaWP LLC |
| 10 |
*/ |
| 11 |
class Qcformbuilder_Forms_Render_Util { |
| 12 |
|
| 13 |
/** |
| 14 |
* Array of Qcformbuilder_Forms_Render_Footer objects |
| 15 |
* |
| 16 |
* @since 1.5.0 |
| 17 |
* |
| 18 |
* @var array |
| 19 |
*/ |
| 20 |
public static $footer_objects; |
| 21 |
|
| 22 |
/** |
| 23 |
* Get ID of form notice HTML element |
| 24 |
* |
| 25 |
* @since 1.5.0 |
| 26 |
* |
| 27 |
* @param array $form Form config |
| 28 |
* @param int $count Form instance count |
| 29 |
* |
| 30 |
* @return string |
| 31 |
*/ |
| 32 |
public static function notice_element_id( array $form, $count ){ |
| 33 |
$element = 'qcformbuilder_notices_' . $count; |
| 34 |
|
| 35 |
/** |
| 36 |
* Filter ID of form notice HTML element |
| 37 |
* |
| 38 |
* @since 1.5.0 |
| 39 |
* |
| 40 |
* @param string $element notice element ID |
| 41 |
* @param array $form Form config |
| 42 |
* @param int $count Form instance count |
| 43 |
*/ |
| 44 |
return apply_filters( 'qcformbuilder_forms_render_notice_element_id', $element, $form, $count ); |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Get the current forms number |
| 50 |
* |
| 51 |
* If 1 form on page, will be 1, if 2 and is scodn form rendered will be 2... |
| 52 |
* This is a wrapper for global $current_form_count in hopes that one day, that global will be removed |
| 53 |
* |
| 54 |
* @since 1.5.0 |
| 55 |
* |
| 56 |
* @return int |
| 57 |
*/ |
| 58 |
public static function get_current_form_count(){ |
| 59 |
global $current_form_count; |
| 60 |
if( null === $current_form_count ){ |
| 61 |
$current_form_count = 0; |
| 62 |
} |
| 63 |
|
| 64 |
return absint( $current_form_count ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Get ID attribute for a form |
| 69 |
* |
| 70 |
* @since 1.5.8 |
| 71 |
* |
| 72 |
* @param int $current_form_count Current form count on page |
| 73 |
* |
| 74 |
* @return string |
| 75 |
*/ |
| 76 |
public static function form_id_attr( $current_form_count ){ |
| 77 |
//JOSH - Don't put a filter here SO MANY things assume this is the way it is |
| 78 |
$form_wrap_id = "qcformbuilder_form_" . $current_form_count; |
| 79 |
return $form_wrap_id; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Get ID attribute for a form |
| 84 |
* |
| 85 |
* @since 1.5.0 |
| 86 |
* @deprecated 1.5.0.8 |
| 87 |
* |
| 88 |
* @param int $current_form_count Current form count on page |
| 89 |
* |
| 90 |
* @return string |
| 91 |
*/ |
| 92 |
public static function field_id_attribute( $current_form_count ){ |
| 93 |
//Deprecated beacuse naming was wrong |
| 94 |
//See: https://github.com/QcformbuilderWP/Qcformbuilder-Forms/issues/1489 |
| 95 |
_deprecated_function( 'Qcformbuilder_Forms_Render_Util::field_id_attribute', 'Qcformbuilder_Forms_Render_Util::form_id_attr', '1.5.0.8'); |
| 96 |
$form_wrap_id = "qcformbuilder_form_" . $current_form_count; |
| 97 |
return $form_wrap_id; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Add data to be printed in footer |
| 102 |
* |
| 103 |
* Container/factory for Qcformbuilder_Forms_Render_Footer objects |
| 104 |
* |
| 105 |
* @uses 1.5.0 |
| 106 |
* |
| 107 |
* @param string $data Data to add |
| 108 |
* @param array $form Form config |
| 109 |
* |
| 110 |
* @return bool True if added, false if invalid or could not be added (not string or added too late) |
| 111 |
*/ |
| 112 |
public static function add_inline_data( $data, array $form ){ |
| 113 |
if( ! empty( $form[ 'ID' ] ) ){ |
| 114 |
$form_id = $form[ 'ID' ]; |
| 115 |
}else{ |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
if( empty( self::$footer_objects[ $form[ 'ID' ] ] ) ){ |
| 120 |
if ( is_array( $form ) ) { |
| 121 |
self::$footer_objects[ $form_id ] = new Qcformbuilder_Forms_Render_Footer( $form ); |
| 122 |
} |
| 123 |
} |
| 124 |
/** @var Qcformbuilder_Forms_Render_Footer */ |
| 125 |
return self::$footer_objects[ $form_id ]->add_data( $data ); |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Add an inline script to footer scripts |
| 131 |
* |
| 132 |
* @since 1.5.0.8 |
| 133 |
* |
| 134 |
* @param string $script JavaScript without <script> tags |
| 135 |
* @param array $form Form config |
| 136 |
* |
| 137 |
* @return bool |
| 138 |
*/ |
| 139 |
public static function add_inline_script( $script, array $form ){ |
| 140 |
$script = self::create_inline_script( $script ); |
| 141 |
|
| 142 |
return self::add_inline_data( $script, $form ); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Add CData markup to footer scripts |
| 147 |
* |
| 148 |
* @since 1.5.0.8 |
| 149 |
* |
| 150 |
* @param $script |
| 151 |
* @param array $form |
| 152 |
* |
| 153 |
* @return bool |
| 154 |
*/ |
| 155 |
public static function add_cdata( $script, array $form ){ |
| 156 |
$output = self::create_cdata( $script ); |
| 157 |
return self::add_inline_data( $output, $form ); |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Create inline script markup |
| 163 |
* |
| 164 |
* @since 1.5.0.8 |
| 165 |
* |
| 166 |
* @param string $script JavaScript with not <script> tags |
| 167 |
* |
| 168 |
* @return string |
| 169 |
*/ |
| 170 |
protected static function create_inline_script( $script ){ |
| 171 |
$script = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $script ); |
| 172 |
|
| 173 |
return $script; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Create CData markup |
| 178 |
* |
| 179 |
* @since 1.5.0.8 |
| 180 |
* |
| 181 |
* @param string $script JavaScript with not <script> tags |
| 182 |
* |
| 183 |
* @return string |
| 184 |
*/ |
| 185 |
public static function create_cdata( $script ){ |
| 186 |
$output = "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5 |
| 187 |
$output .= "/* <![CDATA[ */\n"; |
| 188 |
$output .= "$script\n"; |
| 189 |
$output .= "/* ]]> */\n"; |
| 190 |
$output .= "</script>\n"; |
| 191 |
|
| 192 |
return $output; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Get footer object, by form ID |
| 197 |
* |
| 198 |
* @since 1.5.6 |
| 199 |
* |
| 200 |
* @param string $form_id Forms ID |
| 201 |
* |
| 202 |
* @return bool|Qcformbuilder_Forms_Render_Footer |
| 203 |
*/ |
| 204 |
public static function get_footer_object( $form_id ){ |
| 205 |
if( isset( self::$footer_objects[ $form_id ] ) && is_object( self::$footer_objects[ $form_id ] ) ){ |
| 206 |
return self::$footer_objects[ $form_id ]; |
| 207 |
} |
| 208 |
|
| 209 |
return false; |
| 210 |
} |
| 211 |
|
| 212 |
} |
| 213 |
|