Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.04 KB | None | 0 0
  1. <?php
  2. /*
  3.  
  4. This is example how to add simple Google Maps field type.
  5. Most of functions are called automatically from Types plugin.
  6. Functions naming conventions are:
  7.  
  8. - For basic type data (required) callback
  9. wpcf_fields_$myfieldname()
  10.  
  11. Optional
  12.  
  13. - Group form data callback
  14. wpcf_fields_$myfieldname_insert_form()
  15.  
  16. - Post edit page form callback
  17. wpcf_fields_$myfieldname_meta_box_form()
  18.  
  19. - Editor popup callback
  20. wpcf_fields_$myfieldname_editor_callback()
  21.  
  22. - View function callback
  23. wpcf_fields_$myfieldname_view()
  24.  
  25. */
  26.  
  27. // Add registration hook
  28. add_filter( 'types_register_fields', 'my_types' );
  29.  
  30. /**
  31. * Register custom post type on 'types_register_fields' hook.
  32. *
  33. * @param array $fields
  34. * @return type
  35. */
  36. function my_types( $fields ) {
  37. $fields['google_map'] = __FILE__;
  38. return $fields;
  39. }
  40.  
  41. /**
  42. * Define field.
  43. *
  44. * @return type
  45. */
  46. function wpcf_fields_google_map() {
  47. return array(
  48. 'path' => __FILE__, // This is deprecated but should be tested for safe removal
  49. 'id' => 'google_map',
  50. 'title' => __( 'Google Map', 'wpcf' ),
  51. 'description' => __( 'This is additional field', 'wpcf' ),
  52. /*
  53. * Validation
  54. *
  55. * TODO Elaborate on this
  56. * Add examples for various usage (review needed)
  57. */
  58. 'validate' => array('required'),
  59. /*
  60. *
  61. *
  62. *
  63. *
  64. *
  65. *
  66. * Possible (optional) parameters
  67. */
  68. // Additional JS on post edit page
  69. 'meta_box_js' => array(// Add JS when field is active on post edit page
  70. 'wpcf-jquery-fields-my-field' => array(
  71. 'inline' => 'wpcf_fields_google_map_meta_box_js_inline', // This calls function that renders JS
  72. 'deps' => array('jquery'), // (optional) Same as WP's enqueue_script() param
  73. 'in_footer' => true, // (optional) Same as WP's enqueue_script() param
  74. ),
  75. 'wpcf-jquery-fields-my-field' => array(
  76. 'src' => get_stylesheet_directory_uri() . '/js/my-field.js', // This will load JS file
  77. ),
  78. ),
  79. // Additional CSS on post edit page
  80. 'meta_box_css' => array(
  81. 'wpcf-jquery-fields-my-field' => array(
  82. 'src' => get_stylesheet_directory_uri() . '/css/my-field.css', // or inline function 'inline' => $funcname
  83. 'deps' => array('somecss'), // (optional) Same as WP's enqueue_style() param
  84. ),
  85. ),
  86. // Additional JS on group edit page
  87. 'group_form_js' => array(// Add JS when field is active on post edit page
  88. 'wpcf-jquery-fields-my-field' => array(
  89. 'inline' => 'wpcf_fields_google_map_group_form_js_inline', // This calls function that renders JS
  90. 'deps' => array('jquery'), // (optional) Same as WP's enqueue_script() param
  91. 'in_footer' => true, // (optional) Same as WP's enqueue_script() param
  92. ),
  93. 'wpcf-jquery-fields-my-field' => array(
  94. 'src' => get_stylesheet_directory_uri() . '/js/my-field.js', // This will load JS file
  95. ),
  96. ),
  97. // Additional CSS on post edit page
  98. 'group_form_css' => array(
  99. 'wpcf-jquery-fields-my-field' => array(
  100. 'src' => get_stylesheet_directory_uri() . '/css/my-field.css', // or inline function 'inline' => $funcname
  101. 'deps' => array('somecss'), // (optional) Same as WP's enqueue_style() param
  102. ),
  103. ),
  104. // override editor popup link (you must then load JS function that will process it)
  105. // 'editor_callback' => 'wpcfFieldsMyFieldEditorCallback(\'%s\')', // %s will inject field ID
  106. // meta key type
  107. 'meta_key_type' => 'INT',
  108. // Required WP version check
  109. 'wp_version' => '3.3',
  110. );
  111. }
  112.  
  113. /**
  114. * Types Group edit screen form.
  115. *
  116. * Here you can specify all additional group form data if nedded,
  117. * it will be auto saved to field 'data' property.
  118. *
  119. * @return string
  120. */
  121. function wpcf_fields_google_map_insert_form() {
  122. $form['additional'] = array(
  123. '#type' => 'textfield',
  124. '#description' => 'Add some comment',
  125. '#name' => 'comment',
  126. );
  127. return $form;
  128. }
  129.  
  130. /**
  131. * Overrides form output in meta box on post edit screen.
  132. */
  133. function wpcf_fields_google_map_meta_box_form( $data ) {
  134. $form['name'] = array(
  135. '#name' => 'wpcf[' . $data['slug'] . ']', // Set this to override default output
  136. '#type' => 'textfield',
  137. '#title' => __( 'Add Google Map coordinates', 'wpcf' ),
  138. '#description' => __( 'Your input should look something like "41.934146,12.455821"',
  139. 'wpcf' )
  140. );
  141. return $form;
  142. }
  143.  
  144. /**
  145. * Adds editor popup callnack.
  146. *
  147. * This form will be showed in editor popup
  148. */
  149. function wpcf_fields_google_map_editor_callback( $field, $settings ) {
  150. ob_start();
  151.  
  152. ?>
  153. <label><input type="text" name="width" value="<?php echo isset( $settings['width'] ) ? $settings['width'] : '425'; ?>" />&nbsp;<?php _e( 'Width',
  154. 'wpcf' ); ?></label>
  155. <br />
  156. <label><input type="text" name="height" value="<?php echo isset( $settings['height'] ) ? $settings['height'] : '350'; ?>" />&nbsp;<?php _e( 'Height',
  157. 'wpcf' ); ?></label>
  158. <?php
  159. $form = ob_get_contents();
  160. ob_get_clean();
  161. return array(
  162. 'tabs' => array(
  163. 'display' => array(
  164. 'menu_title' => __( 'Display', 'wpcf' ),
  165. 'title' => __( 'Display', 'wpcf' ),
  166. 'content' => $form,
  167. )
  168. )
  169. );
  170. }
  171.  
  172. /**
  173. * Processes editor popup submit
  174. */
  175. function wpcf_fields_google_map_editor_submit( $data, $field ) {
  176. $add = '';
  177.  
  178. // Add parameters
  179. if ( !empty( $data['width'] ) ) {
  180. $add .= ' width="' . strval( $data['width'] ) . '"';
  181. }
  182. if ( !empty( $data['height'] ) ) {
  183. $add .= ' height="' . strval( $data['height'] ) . '"';
  184. }
  185.  
  186. // Generate and return shortcode
  187. return wpcf_fields_get_shortcode( $field, $add );
  188. }
  189.  
  190. /**
  191. * Renders view
  192. *
  193. * Useful $data:
  194. * $data['field_value'] - Value of custom field
  195. *
  196. * @param array $data
  197. */
  198. function wpcf_fields_google_map_view( $data ) {
  199. $data['width'] = !empty( $data['width'] ) ? $data['width'] : 425;
  200. $data['height'] = !empty( $data['height'] ) ? $data['height'] : 350;
  201. return '<iframe width="' . $data['width'] . '" height="' . $data['height']
  202. . '" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?q='
  203. . $data['field_value']
  204. . '&amp;num=1&amp;vpsrc=0&amp;hl=en&amp;ie=UTF8&amp;t=m&amp;z=14&amp;ll='
  205. . $data['field_value']
  206. . '&amp;output=embed"></iframe><br /><small><a href="http://maps.google.com/maps?q='
  207. . $data['field_value']
  208. . '&amp;num=1&amp;vpsrc=0&amp;hl=en&amp;ie=UTF8&amp;t=m&amp;z=14&amp;ll='
  209. . $data['field_value']
  210. . '&amp;source=embed" style="color:#0000FF;text-align:left">'
  211. . __( 'View Larger Map', 'wpcf' )
  212. . '</a></small><br />';
  213. }
  214.  
  215. function WPToolset_Field_Google_Map_loader()
  216. {
  217.  
  218. if ( class_exists('WPToolset_Field_Google_Map' ) ) {
  219. return;
  220. }
  221.  
  222. class WPToolset_Field_Google_Map extends FieldFactory
  223. {
  224. public function metaform()
  225. {
  226. $attributes = $this->getAttr();
  227.  
  228. $metaform = array();
  229. $metaform[] = array(
  230. '#type' => 'textfield',
  231. '#title' => $this->getTitle(),
  232. '#description' => $this->getDescription(),
  233. '#name' => $this->getName(),
  234. '#value' => $this->getValue(),
  235. '#validate' => $this->getValidationData(),
  236. '#repetitive' => $this->isRepetitive(),
  237. '#attributes' => $attributes,
  238. );
  239. return $metaform;
  240. }
  241.  
  242. }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement