/v5/acf-generous-slider.php

  1. <?php 
  2.  
  3. class acf_field_generous_slider extends acf_field { 
  4.  
  5. /** 
  6. * The id of the field. 
  7. * 
  8. * @since 5.0.0 
  9. * 
  10. * @var array $name Single word, no spaces. 
  11. */ 
  12. var $name; 
  13.  
  14. /** 
  15. * The label for the field. 
  16. * 
  17. * @since 5.0.0 
  18. * 
  19. * @var string $label Visible when selecting a field type. 
  20. */ 
  21. var $label; 
  22.  
  23. /** 
  24. * The type of ACF field. 
  25. * 
  26. * @since 5.0.0 
  27. * 
  28. * @var string $category basic|content|choice|relational|layout|custom 
  29. */ 
  30. var $category; 
  31.  
  32. /** 
  33. * Default settings which are merged into the field object. 
  34. * 
  35. * @since 5.0.0 
  36. * 
  37. * @var array $defaults The defaults for the field. 
  38. */ 
  39. var $defaults; 
  40.  
  41. /** 
  42. * Array of strings that are used in JavaScript. This allows JS strings 
  43. * to be translated in PHP and loaded via: 
  44. * 
  45. * var message = acf._e('generous_slider', 'error'); 
  46. * 
  47. * @since 5.0.0 
  48. * 
  49. * @var array $l10n The defaults for the field. 
  50. */ 
  51. var $l10n; 
  52.  
  53. /** 
  54. * Initialize the class. 
  55. * 
  56. * Setup the field type data. 
  57. * 
  58. * @since 5.0.0 
  59. */ 
  60. function __construct() { 
  61. $this->name = 'generous_slider'; 
  62. $this->label = __( 'Generous Slider', 'acf-generous_slider' ); 
  63. $this->category = 'basic'; 
  64. $this->defaults = array(); 
  65. $this->l10n = array(); 
  66.  
  67. parent::__construct(); 
  68.  
  69. /** 
  70. * Creates the HTML interface for the field 
  71. * 
  72. * @since 3.6 
  73. * 
  74. * @param array $field The field array holding all the field options. 
  75. */ 
  76. function render_field( $field ) { 
  77.  
  78. global $wp_plugin_generous; 
  79.  
  80. $options = $wp_plugin_generous->get_options(); 
  81.  
  82. if ( isset( $options['username'] ) && $options['username'] !== '') { 
  83.  
  84. $field_id = ''; 
  85. $field_title = ''; 
  86.  
  87. if ( isset( $field['value'], $field['value']['id'] ) ) { 
  88. $field_id = $field['value']['id']; 
  89.  
  90. if ( isset( $field['value']['title'] ) ) { 
  91. $field_title = $field['value']['title']; 
  92.  
  93. $prefix = 'acf-generous-slider--search'; 
  94.  
  95. echo "<input type=\"text\" name=\"{$field['name']}[title]\" class=\"{$prefix}-input-title\" value=\"" . htmlspecialchars( $field_title ) . "\" />"; 
  96. echo "<input type=\"hidden\" name=\"{$field['name']}[id]\" class=\"{$prefix}-input-id\" value=\"{$field_id}\" />"; 
  97. echo "<input type=\"hidden\" class=\"{$prefix}-account\" value=\"{$options['username']}\" />"; 
  98.  
  99. echo "<div class=\"{$prefix}-results\"></div>"; 
  100.  
  101. } else { 
  102.  
  103. echo "<span>Generous username not found. Check plugin settings.</span>"; 
  104.  
  105.  
  106.  
  107. /** 
  108. * This action is called in the admin_enqueue_scripts action on the edit 
  109. * screen where your field is created. Use this action to add CSS + JavaScript 
  110. * to assist your create_field() action. 
  111. * 
  112. * Info: http://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts 
  113. * 
  114. * @since 3.6 
  115. */ 
  116. function input_admin_enqueue_scripts() { 
  117.  
  118. $dir = plugin_dir_url( dirname( __FILE__ ) ); 
  119.  
  120. wp_register_script( 'acf-input-generous_slider', "{$dir}assets/js/input.js" ); 
  121. wp_register_style( 'acf-input-generous_slider', "{$dir}assets/css/input.css" ); 
  122.  
  123. wp_enqueue_script('acf-input-generous_slider'); 
  124. wp_enqueue_style('acf-input-generous_slider'); 
  125.  
  126.  
  127.  
  128. /** 
  129. * Updates the value for the database. 
  130. * 
  131. * This filter is applied to the $value before it is updated in the db. 
  132. * 
  133. * @since 3.6 
  134. * 
  135. * @param array $value The value which will be saved in the database. 
  136. * @param int $post_id The $post_id of which the value will be saved. 
  137. * @param array $field The field array holding all the field options. 
  138. * 
  139. * @return array $value The modified value. 
  140. */ 
  141. function update_value( $value, $post_id, $field ) { 
  142.  
  143. if ( '' === $value['id'] ) { 
  144. $value = false; 
  145.  
  146. return $value; 
  147.  
  148. /** 
  149. * Validates the value prior to saving to database. 
  150. * 
  151. * This filter is used to perform validation on the value prior to saving. 
  152. * All values are validated regardless of the field's required setting. This 
  153. * allows you to validate and return messages to the user if the value is not correct. 
  154. * 
  155. * @since 5.0 
  156. * 
  157. * @param bool $valid Validation status based on the value and the fields required setting. 
  158. * @param mixed $value The $_POST value. 
  159. * @param array $field The field array holding all the field options. 
  160. * @param string $input The corresponding input name for $_POST value 
  161. * 
  162. * @return bool $value If values are valid. 
  163. */ 
  164. function validate_value( $valid, $value, $field, $input ) { 
  165. if( '' === $value['id'] ) { 
  166. $valid = false; 
  167.  
  168. return $valid; 
  169.  
  170. /** 
  171. * Formats the value for the frontend. 
  172. * 
  173. * This filter is applied to the $value after it is loaded from the db and 
  174. * before it is passed back to the API functions such as the_field. 
  175. * 
  176. * @since 3.6 
  177. * 
  178. * @param array $value The value which will be saved in the database. 
  179. * @param int $post_id The $post_id of which the value will be saved. 
  180. * @param array $field The field array holding all the field options. 
  181. * 
  182. * @return array $value The modified value. 
  183. */ 
  184. function format_value( $value, $post_id, $field ) { 
  185. if ( isset( $value['id'] ) ) { 
  186. $api = new WP_Generous_Api(); 
  187. return $api->get_slider( $value['id'] ); 
  188. } else { 
  189. return false; 
  190.  
  191.  
  192. new acf_field_generous_slider(); 
.