sys1.{{plugin}}.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. {"label":"Plugin","syntax":"php"}
  2. <?php
  3. /**
  4. * Plugin Name: {{plugin}}
  5. * Plugin URI: http://no-url.com
  6. * Description: {{description}}
  7. * Version: 1.0.0
  8. * Author: {{user.fullname}}
  9. * Author URI: https://sys1.fr
  10. * License: Copyright
  11. */
  12. function sys1_{{plugin}}_init()
  13. //Permet de créer un type de posts personnalisé, avec un menu dédié
  14. $labels = array(
  15. 'name' => _x( '{{Entity}}s', 'Post Type General Name'),
  16. 'singular_name' => _x( '{{Entity}}', 'Post Type Singular Name'),
  17. 'menu_name' => __( '{{Entity}}s'),
  18. 'all_items' => __( 'Toutes les {{entity}}s'),
  19. 'view_item' => __( 'Voir l\'{{entity}}'),
  20. 'add_new_item' => __( 'Ajouter une nouvelle {{entity}}'),
  21. 'add_new' => __( 'Ajouter'),
  22. 'edit_item' => __( 'Editer l\'{{entity}}'),
  23. 'update_item' => __( 'Modifier l\'{{entity}}'),
  24. 'search_items' => __( 'Rechercher une {{entity}}'),
  25. 'not_found' => __( 'Non trouvée'),
  26. 'not_found_in_trash' => __( 'Non trouvée dans la corbeille'),
  27. );
  28. $args = array(
  29. 'label' => __( '{{Entity}}s'),
  30. 'description' => __( '{{description}}'),
  31. 'labels' => $labels,
  32. 'supports' => array( 'title', 'thumbnail'),
  33. 'menu_icon' => 'dashicons-admin-multisite',
  34. 'hierarchical' => false,
  35. 'public' => true,
  36. 'has_archive' => false,
  37. 'rewrite' => array( 'slug' => '{{entity}}s'),
  38. );
  39. register_post_type( '{{entity}}s', $args );
  40. //Inclus le fichier action.php
  41. require_once(__DIR__.DIRECTORY_SEPARATOR.'functions.php');
  42. add_rewrite_endpoint( 'action.php', EP_ROOT );
  43. }
  44. //Permet de gérer l'affichage du conteneur des champs personnalisés dans l'interface de création d'une nouvelle {{entity}}
  45. function show_{{entity}}_meta_box() {
  46. global $post;
  47. $meta = get_post_meta( $post->ID, '{{entity}}_fields', true );
  48. require_once(__DIR__.DIRECTORY_SEPARATOR.'{{Entity}}.class.php');
  49. ${{entity}} = new {{Entity}}();
  50. ${{entity}}->fromArray($meta);
  51. ?>
  52. <input type="hidden" name="{{entity}}_meta_box_nonce" value="<?php echo wp_create_nonce( basename(__FILE__) ); ?>">
  53. <table id="fieldsSettings">
  54. {{:fields}}<tr>
  55. <td><label for="{{value.key}}">{{value.label}}</label></td>
  56. <td>
  57. <!-- TODO !! : remplacer name="{{value.key}}" par name="{{entity}}_fields[{{value.key}}]" -->
  58. {{value.input}}
  59. </td>
  60. </tr>{{/:fields}}
  61. </table>
  62. <?php
  63. }
  64. //Permet de sauvegarder les différents champs personnalisés créés avec le custom post type
  65. //
  66. function save_{{entity}}_fields_meta($post_id) {
  67. if (wp_is_post_revision($post_id))
  68. return;
  69. // Check du nonce
  70. if (!wp_verify_nonce($_POST['{{entity}}_meta_box_nonce'], basename(__FILE__))) return $post_id;
  71. // Check de l'autosave
  72. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
  73. // Check des permissions
  74. if ('page' === $_POST['post_type']) {
  75. if ( !current_user_can('edit_page', $post_id) || !current_user_can('edit_post', $post_id))
  76. return $post_id;
  77. }
  78. $old = get_post_meta($post_id, '{{entity}}_fields', true);
  79. $new = $_POST['{{entity}}_fields'];
  80. if ($new && $new !== $old) {
  81. update_post_meta($post_id, '{{entity}}_fields', $new);
  82. } elseif ('' === $new && $old) {
  83. delete_post_meta($post_id, '{{entity}}_fields', $old);
  84. }
  85. }
  86. //Permet d'afficher l'intégralité du contenu du formulaire de soumission d'une {{entity}}
  87. function sys1_{{plugin}}_shortcode($attributes) {
  88. ob_start();
  89. ?>
  90. <div id="{{plugin}}-submit-form">
  91. <input type="hidden" name="{{plugin}}_meta_box_nonce" id="{{plugin}}_meta_box_nonce" value="<?php echo wp_create_nonce( '{{plugin}}' ); ?>">
  92. {{:fields}}<div>
  93. <label for="post_title"><span>{{value.label}} *</span></label>
  94. {{value.input}}
  95. </div>{{/:fields}}
  96. <div class="g-recaptcha" id="captcha" data-sitekey=" <?php echo get_option( 'recaptcha_public_{{plugin}}' ) ?>"></div>
  97. <div class="generalButton" id="submitButton" onclick="sys1_{{plugin}}_save();"><div class="hoverButton">Soumettre</div></div>
  98. </div>
  99. <?php
  100. $output = ob_get_clean();
  101. return $output;
  102. }
  103. // Ajout du sous-menu "Paramètres" dans l'onglet {{Plugin}}, afin de gérer les différents élémentsutiles au plugin (captcha keys, email, shortcode)
  104. function sys1_{{plugin}}_settings() {
  105. add_submenu_page(
  106. 'edit.php?post_type={{entity}}s',
  107. __('Paramètres - Plugin SYS1 {{Plugin}}s','menu-test'),
  108. __('Paramètres','menu-test'),
  109. 'manage_options',
  110. 'sys1_{{plugin}}_settings',
  111. 'show_{{plugin}}_settings_page'
  112. );
  113. }
  114. //Ajout des champs de configuration du plugin SYS1 {{Entity}} sur l'onglet de menu {{plugin}}cié
  115. function sys1_{{plugin}}_admin(){
  116. add_meta_box(
  117. // id
  118. '{{entity}}_meta_box',
  119. // titre
  120. 'Informations : {{entity}}',
  121. //Callback
  122. 'show_{{entity}}_meta_box',
  123. //Screen
  124. '{{entity}}s',
  125. //Contexte
  126. 'normal',
  127. //Priorité
  128. 'high'
  129. );
  130. $customsSettings = array(
  131. "recaptcha_url_{{plugin}}" => array(
  132. "title" => "Recaptcha url",
  133. "placeholder" => "https://www.google.com/recaptcha/api/siteverify"
  134. ),
  135. "recaptcha_public_{{plugin}}" => array(
  136. "title" => "Recaptcha clé publique",
  137. "placeholder" => "ex: 6LeuNQITAAAAAPGRU7dkrCPIrrR64WPvzMc7pn6Z"
  138. ),
  139. "recaptcha_secret_{{plugin}}" => array(
  140. "title" => "Recaptcha clé privée",
  141. "placeholder" => "ex: 6LeuNQITAAAAAHwUcbXbyFCUudJKRAjcgNRwlaoE"
  142. ),
  143. "api_key_gmaps_geocode" => array(
  144. "title" => "Geocode clé privée",
  145. "placeholder" => ""
  146. ),
  147. "notification_email_{{plugin}}" => array(
  148. "title" => "Email notification d'inscription d'{{entity}}",
  149. "placeholder" => "votre.email@example.fr"
  150. )
  151. );
  152. add_settings_section(
  153. 'sys1_{{plugin}}_settings_section',
  154. 'Option plugin {{Entity}}s',
  155. function(){
  156. echo '<p>Paramétrage du plugin {{plugin}}</p>';
  157. },
  158. 'sys1_{{plugin}}_settings'
  159. );
  160. foreach($customsSettings as $slug => $values){
  161. add_settings_field(
  162. $slug,
  163. $values['title'],
  164. function($args){
  165. $option = get_option($args[0]);
  166. echo '<input type="text" id="'. $args[0] .'" name="'. $args[0] .'" value="' . $option . '" class="regular-text ltr" placeholder="'.$values['placeholder'].'" />';
  167. },
  168. 'sys1_{{plugin}}_settings',
  169. 'sys1_{{plugin}}_settings_section',
  170. array($slug)
  171. );
  172. register_setting('sys1_{{plugin}}_settings_group',$slug, 'esc_attr');
  173. }
  174. }
  175. //Gestion de l'affichage de la page des paramètres du plugin des {{Entity}}s.
  176. function show_{{plugin}}_settings_page() {
  177. ?>
  178. <div class='wrap'>
  179. <h2>Paramètres</h2>
  180. <hr>
  181. <div id="shortcodeContainer">
  182. <ul>
  183. <li>Afin d'afficher le plugin {{plugin}} sur une page, utilisez le shortcode suivant : <strong>[sys1_{{plugin}}]</strong></li>
  184. </ul>
  185. </div>
  186. <hr>
  187. <form method='post' action='options.php'>
  188. <?php
  189. settings_fields( 'sys1_{{plugin}}_settings_group' );
  190. do_settings_sections( 'sys1_{{plugin}}_settings' );
  191. ?>
  192. <p class='submit'>
  193. <input name='submit' type='submit' id='submit' class='button-primary' value='<?php _e("Save Changes") ?>' />
  194. </p>
  195. </form>
  196. </div>
  197. <?php
  198. }
  199. // Ajout de la variable action.php pour que Wordpress le trouve et ne l'ignore pas
  200. function {{plugin}}_add_query_vars($vars){
  201. $vars[] = "action.php";
  202. return $vars;
  203. }
  204. function action_{{plugin}}_parsing(){
  205. require_once(__DIR__.DIRECTORY_SEPARATOR.'action.php');
  206. }
  207. //Création des différents hooks
  208. wp_enqueue_script('sys1_{{plugin}}_js', plugin_dir_url(__FILE__) . '/js/main.js?v=1', array('jquery'));
  209. wp_enqueue_style('sys1_{{plugin}}_css', plugin_dir_url(__FILE__) . '/css/main.css?v=1');
  210. add_action('admin_init', 'sys1_{{plugin}}_admin', 200);
  211. add_action('admin_menu', 'sys1_{{plugin}}_settings');
  212. add_action('save_post', 'save_{{entity}}_fields_meta', 10);
  213. add_action('init', 'sys1_{{plugin}}_init');
  214. add_action('parse_request','action_{{plugin}}_parsing');
  215. add_shortcode('sys1_{{plugin}}', 'sys1_{{plugin}}_shortcode', 10);
  216. add_filter( 'query_vars', '{{plugin}}_add_query_vars');