Yoast_GA_Admin_Form

This class is for the backend.

Defined (1)

The class is defined in the following location(s).

/admin/class-admin-form.php  
  1. class Yoast_GA_Admin_Form { 
  2.  
  3. /** 
  4. * @var string $form_namespace 
  5. */ 
  6. private static $form_namespace; 
  7.  
  8. /** 
  9. * Create a form element to init a form 
  10. * @param string $namespace 
  11. * @return string 
  12. */ 
  13. public static function create_form( $namespace ) { 
  14. self::$form_namespace = $namespace; 
  15.  
  16. $action = admin_url( 'admin.php' ); 
  17. if ( isset( $_GET['page'] ) ) { 
  18. $action .= '?page=' . $_GET['page']; 
  19.  
  20. return '<form action="' . $action . '" method="post" id="yoast-ga-form-' . self::$form_namespace . '" class="yoast_ga_form">' . wp_nonce_field( 'save_settings', 'yoast_ga_nonce', null, false ); 
  21.  
  22.  
  23. /** 
  24. * Return the form end tag and the submit button 
  25. * @param string $button_label 
  26. * @param string $name 
  27. * @param string $onclick 
  28. * @return null|string 
  29. */ 
  30. public static function end_form( $button_label = null, $name = 'submit', $onclick = null ) { 
  31. if ( $button_label === null ) { 
  32. $button_label = __( 'Save changes', 'google-analytics-for-wordpress' ); 
  33.  
  34. $output = null; 
  35. $output .= '<div class="ga-form ga-form-input">'; 
  36. $output .= '<input type="submit" name="ga-form-' . $name . '" value="' . $button_label . '" class="button button-primary ga-form-submit" id="yoast-ga-form-submit-' . self::$form_namespace . '"'; 
  37. if ( ! is_null( $onclick ) ) { 
  38. $output .= ' onclick="' . $onclick . '"'; 
  39. $output .= ' />'; 
  40. $output .= '</div></form>'; 
  41.  
  42. return $output; 
  43.  
  44.  
  45. /** 
  46. * Create a input form element with our labels and wrap them 
  47. * @param string $type 
  48. * @param null|string $title 
  49. * @param null|string $name 
  50. * @param null|string $text_label 
  51. * @param null|string $description 
  52. * @return null|string 
  53. */ 
  54. public static function input( $type = 'text', $title = null, $name = null, $text_label = null, $description = null ) { 
  55. $input = null; 
  56. $id = str_replace( '[', '-', $name ); 
  57. $id = str_replace( ']', '', $id ); 
  58.  
  59. $input_value = self::get_formfield_from_options( $name ); 
  60.  
  61. $input .= '<div class="ga-form ga-form-input">'; 
  62. if ( ! is_null( $title ) ) { 
  63. $input .= self::label( $id, $title, $type ); 
  64.  
  65. $attributes = array( 
  66. 'type' => $type,  
  67. 'id' => 'yoast-ga-form-' . $type . '-' . self::$form_namespace . '-' . $id . '',  
  68. 'name' => $name,  
  69. 'class' => 'ga-form ga-form-' . $type . ' ',  
  70. ); 
  71.  
  72. if ( $type == 'checkbox' ) { 
  73. $attributes['value'] = '1'; 
  74.  
  75. if ( $input_value == 1 ) { 
  76. $attributes['checked'] = 'checked'; 
  77. else { 
  78. $attributes['value'] = esc_attr( stripslashes( $input_value ) ); 
  79.  
  80. $input .= '<input ' . self::parse_attributes( $attributes ) . ' />'; 
  81.  
  82. // If we get a description, append it to this select field in a new row 
  83. if ( ! is_null( $description ) ) { 
  84. $input .= self::show_help( $id, $description ); 
  85.  
  86. if ( ! is_null( $text_label ) ) { 
  87. $input .= '<label class="ga-form ga-form-' . $type . '-label" id="yoast-ga-form-label-' . $type . '-textlabel-' . self::$form_namespace . '-' . $id . '" for="yoast-ga-form-' . $type . '-' . self::$form_namespace . '-' . $id . '">' . $text_label . '</label>'; 
  88.  
  89. $input .= '</div>'; 
  90.  
  91. return $input; 
  92.  
  93. /** 
  94. * Generate a select box 
  95. * @param string $title 
  96. * @param string $name 
  97. * @param array $values 
  98. * @param null|string $description 
  99. * @param bool $multiple 
  100. * @param string $empty_text 
  101. * @return null|string 
  102. */ 
  103. public static function select( $title, $name, $values, $description = null, $multiple = false, $empty_text = null ) { 
  104. $select = null; 
  105. $id = str_replace( '[', '-', $name ); 
  106. $id = str_replace( ']', '', $id ); 
  107.  
  108. $select .= '<div class="ga-form ga-form-input">'; 
  109. if ( ! is_null( $title ) ) { 
  110. $select .= self::label( $id, $title, 'select' ); // '<label class="ga-form ga-form-select-label ga-form-label-left" id="yoast-ga-form-label-select-' . self::$form_namespace . '-' . $id . '">' . $title . ':</label>'; 
  111.  
  112. if ( $multiple ) { 
  113. $select .= '<select multiple name="' . $name . '[]" id="yoast-ga-form-select-' . self::$form_namespace . '-' . $id . '" class="ga-multiple">'; 
  114. else { 
  115. $select .= '<select data-placeholder="' . $empty_text . '" name="' . $name . '" id="yoast-ga-form-select-' . self::$form_namespace . '-' . $id . '">'; 
  116. if ( ! is_null( $empty_text ) ) { 
  117. $select .= '<option></option>'; 
  118. if ( count( $values ) >= 1 ) { 
  119. $select_value = self::get_formfield_from_options( $name ); 
  120.  
  121. foreach ( $values as $optgroup => $value ) { 
  122. if ( ! empty( $value['items'] ) ) { 
  123. $select .= self::create_optgroup( $optgroup, $value, $select_value ); 
  124. else { 
  125. $select .= self::option( $select_value, $value ); 
  126. $select .= '</select>'; 
  127.  
  128. if ( ! is_null( $description ) ) { 
  129. $select .= self::show_help( $id, $description ); 
  130.  
  131. $select .= '</div>'; 
  132.  
  133. return $select; 
  134.  
  135.  
  136. /** 
  137. * Generate a textarea field 
  138. * @param string $title 
  139. * @param string $name 
  140. * @param null|string $description 
  141. * @return null|string 
  142. */ 
  143. public static function textarea( $title, $name, $description = null ) { 
  144. $text = null; 
  145. $id = Yoast_GA_Options::instance()->option_prefix . '_' . $name; 
  146.  
  147. $textarea_value = self::get_formfield_from_options( $name ); 
  148.  
  149. $text .= '<div class="ga-form ga-form-input">'; 
  150.  
  151. if ( ! is_null( $title ) ) { 
  152. $text .= '<label class="ga-form ga-form-select-label ga-form-label-left" id="yoast-ga-form-label-select-' . self::$form_namespace . '-' . $id . '">' . __( $title, 'google-analytics-for-wordpress' ) . ':</label>'; 
  153.  
  154. $text .= '<textarea rows="5" cols="60" name="' . $name . '" id="yoast-ga-form-textarea-' . self::$form_namespace . '-' . $id . '">' . stripslashes( $textarea_value ) . '</textarea>'; 
  155.  
  156. if ( ! is_null( $description ) ) { 
  157. $text .= self::show_help( $id, $description ); 
  158.  
  159. $text .= '</div>'; 
  160.  
  161. return $text; 
  162.  
  163. /** 
  164. * Parsing a option string for select 
  165. * @param string $select_value 
  166. * @param string $value 
  167. * @return string 
  168. */ 
  169. private static function option( $select_value, $value ) { 
  170.  
  171. if ( is_array( $select_value ) ) { 
  172. if ( in_array( esc_attr( $value['id'] ), $select_value ) ) { 
  173. return '<option value="' . esc_attr( $value['id'] ) . '" selected="selected">' . esc_attr( stripslashes( $value['name'] ) ) . '</option>'; 
  174. else { 
  175. return '<option value="' . esc_attr( $value['id'] ) . '">' . esc_attr( stripslashes( $value['name'] ) ) . '</option>'; 
  176. else { 
  177. return '<option value="' . esc_attr( $value['id'] ) . '" ' . selected( $select_value, $value['id'], false ) . '>' . esc_attr( stripslashes( $value['name'] ) ) . '</option>'; 
  178.  
  179.  
  180. /** 
  181. * Show a question mark with help 
  182. * @param string $id 
  183. * @param string $description 
  184. * @return string 
  185. */ 
  186. public static function show_help( $id, $description ) { 
  187. $help = '<img src="' . plugins_url( 'assets/img/question-mark.png', GAWP_FILE ) . '" class="alignleft yoast_help" id="' . esc_attr( $id . 'help' ) . '" alt="' . esc_attr( $description ) . '" />'; 
  188.  
  189. return $help; 
  190.  
  191.  
  192. /** 
  193. * Will parse the optgroups. 
  194. * @param array $values 
  195. * @return array 
  196. */ 
  197. public static function parse_optgroups( $values ) { 
  198. $optgroups = array(); 
  199. foreach ( $values as $key => $value ) { 
  200. foreach ( $value['items'] as $subitem ) { 
  201. $optgroups[ $subitem['name'] ]['items'] = $subitem['items']; 
  202.  
  203. return $optgroups; 
  204.  
  205. /** 
  206. * Creates a label 
  207. * @param string $id 
  208. * @param string $title 
  209. * @param string $type 
  210. * @return string 
  211. */ 
  212. private static function label( $id, $title, $type ) { 
  213. return '<label for="' . 'yoast-ga-form-' . $type . '-' . self::$form_namespace . '-' . $id . '" class="ga-form ga-form-' . $type . '-label ga-form-label-left" id="yoast-ga-form-label-' . $type . '-' . self::$form_namespace . '-' . $id . '">' . $title . ':</label>'; 
  214.  
  215. /** 
  216. * Creates a optgroup with the items. If items contain items it will create a nested optgroup 
  217. * @param string $optgroup 
  218. * @param array $value 
  219. * @param array $select_value 
  220. * @return string 
  221. */ 
  222. private static function create_optgroup( $optgroup, $value, $select_value ) { 
  223. $optgroup = '<optgroup label="' . esc_attr( $optgroup ) . '">'; 
  224.  
  225. foreach ( $value['items'] as $option ) { 
  226. if ( ! empty( $option['items'] ) ) { 
  227.  
  228. $optgroup .= self::create_optgroup( esc_attr( $option['name'] ), $option, $select_value ); 
  229. else { 
  230. $optgroup .= self::option( $select_value, $option ); 
  231.  
  232. $optgroup .= '</optgroup>'; 
  233.  
  234. return $optgroup; 
  235.  
  236.  
  237. /** 
  238. * Getting the value from the option, if it doesn't exist return empty string 
  239. * @param string $name 
  240. * @return string 
  241. */ 
  242. private static function get_formfield_from_options( $name ) { 
  243. static $options; 
  244.  
  245. if ( $options === null ) { 
  246. $options = Yoast_GA_Options::instance()->get_options(); 
  247.  
  248. // Catch a notice if the option doesn't exist, yet 
  249. return ( isset( $options[ $name ] ) ) ? $options[ $name ] : ''; 
  250.  
  251. /** 
  252. * Parsing given array with attributes as an attribute string 
  253. * @param array $attributes_to_parse 
  254. * @return string 
  255. */ 
  256. private static function parse_attributes( $attributes_to_parse ) { 
  257. $parsed_attributes = ''; 
  258. foreach ( $attributes_to_parse as $attribute_name => $attribute_value ) { 
  259. $parsed_attributes .= $attribute_name . '="' . $attribute_value . '" '; 
  260.  
  261. return trim( $parsed_attributes ); 
  262.