/admin/class-admin-form.php

https://gitlab.com/Blueprint-Marketing/google-analytics-for-wordpress · PHP · 338 lines · 166 code · 58 blank · 114 comment · 28 complexity · fdbdf6eeec49ef098a482bafd57e6faf MD5 · raw file

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