PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/campayn_settings.php

https://github.com/wp-plugins/campayn-email-newsletter-sign-up
PHP | 169 lines | 136 code | 20 blank | 13 comment | 4 complexity | 9beedeb062e9c29d56bfe8dd18674d7b MD5 | raw file
  1. <?php
  2. //
  3. // SETTINGS CONFIGURATION CLASS
  4. //
  5. // By Olly Benson / v 1.3 / 20 November 2011 / http://code.olib.co.uk
  6. //
  7. // HOW TO USE
  8. // * add a include() to this file in your plugin.
  9. // * amend the config class below to add your own settings requirements.
  10. // * to avoid potential conflicts change the namespace to something unique.
  11. // * Full details of how to use Settings see here: http://codex.wordpress.org/Settings_API
  12. class ob_campayn_settings_config {
  13. var $group = 'ob_campayn'; // defines setting groups (should be bespoke to your settings)
  14. var $menu = array(
  15. 'page_name' => 'ob_campayn', // defines which pages settings will appear on. Either bespoke or media/discussion/reading etc
  16. 'title' => "Campayn", // page title that is displayed
  17. 'intro_text' => "", // This allows you to configure the Bar search plugin exactly the way you want it", // text below title
  18. 'nav_title' => "Campayn" // how page is listed on left-hand Settings panel
  19. );
  20. // settings screen WITHOUT API key
  21. var $sections_withoutkey = array(
  22. 'campayn' => array(
  23. 'title' => "Campayn Settings",
  24. 'description' => "",
  25. 'fields' => array (
  26. 'apikey' => array (
  27. 'label' => "Campayn API Key",
  28. 'description' => "Enter your Campayn API Key before using the plugin. You can get one <a href=\"http://campayn.com/login?redirect=/users/api\">here</a>.",
  29. 'length' => "65",
  30. 'suffix' => "",
  31. 'default_value' => ""
  32. ),
  33. 'signup' => array(
  34. 'label' => "Sign up free",
  35. 'description' => "You can edit this text by editin the function... ",
  36. 'function' => 'campayn_signup_callback',
  37. )
  38. )));
  39. //settings screen WITH API key
  40. var $sections_withkey = array(
  41. 'campayn' => array(
  42. 'title' => "Campayn Settings",
  43. 'description' => "",
  44. 'fields' => array (
  45. 'apikey' => array (
  46. 'label' => "Campayn API Key",
  47. 'description' => "Enter your Campayn API Key before using the plugin. You can get one <a href=\"http://campayn.com/login?redirect=/users/api\">here</a>.",
  48. 'length' => "65",
  49. 'suffix' => "",
  50. 'default_value' => ""
  51. ),
  52. 'forms' => array (
  53. 'label' => "Forms",
  54. 'function' => "ob_api_key_callback",
  55. 'length' => "40",
  56. 'suffix' => "",
  57. 'default_value' => "",
  58. 'description' => "Copy and paste the form shortcode on your site"
  59. ),
  60. ),
  61. ),
  62. 'commentators' => array (
  63. 'title' => "Comment subscritions",
  64. 'description' => "",
  65. 'fields' => array(
  66. 'enabled' => array (
  67. 'label' => "Enable comment subscriptions", // you can edit this text in campayn.php in the function below
  68. 'function' => "campayn_setting_checkbox",
  69. ),
  70. 'list' => array(
  71. 'label' => 'Select contact list',
  72. 'function' => 'campayn_setting_dropdown',
  73. ),
  74. 'text' => array(
  75. 'label' => 'Text',
  76. 'description' => '',
  77. 'length' => 255,
  78. 'default_value' => 'Signup to out newsletter!',
  79. )
  80. )
  81. )
  82. );
  83. };
  84. class ob_campayn_settings {
  85. var $settingsConfig = NULL;
  86. function __CONSTRUCT() {
  87. $this->settingsConfig = get_class_vars(sprintf('%s_settings_config','ob_campayn'));
  88. if (function_exists('add_action')) :
  89. add_action('admin_init', array( &$this, 'admin_init'));
  90. add_action('admin_menu', array( &$this, 'admin_add_page'));
  91. endif;
  92. }
  93. function admin_add_page() {
  94. extract($this->settingsConfig['menu']);
  95. add_options_page($title,$nav_title, 'manage_options', $page_name, array( &$this,'options_page'));
  96. }
  97. function options_page() {
  98. printf('</pre><div><h2>%s</h2>%s<form action="options.php" method="post">',$this->settingsConfig['menu']['title'],$this->settingsConfig['menu']['intro_text']);
  99. settings_fields($this->settingsConfig['group']);
  100. do_settings_sections($this->settingsConfig['menu']['page_name']);
  101. printf('<input type="submit" name="Submit" value="%s" /></form></div><pre>',__('Save Changes'));
  102. }
  103. function admin_init(){
  104. $apikey = get_option('ob_campayn_apikey');
  105. $apikey = $apikey['text_string'];
  106. if (!empty($apikey)) {
  107. $this->settingsConfig['sections'] = $this->settingsConfig['sections_withkey'];
  108. } else {
  109. $this->settingsConfig['sections'] = $this->settingsConfig['sections_withoutkey'];
  110. }
  111. foreach ($this->settingsConfig["sections"] AS $section_key=>$section_value) :
  112. add_settings_section($section_key, $section_value['title'], array( &$this, 'section_text'), $this->settingsConfig['menu']['page_name'], $section_value);
  113. foreach ($section_value['fields'] AS $field_key=>$field_value) :
  114. $function = (!empty($field_value['dropdown'])) ? array( &$this, 'setting_dropdown' ) : array( &$this, 'setting_string' );
  115. $function = (!empty($field_value['function'])) ? $field_value['function'] : $function;
  116. $callback = (!empty($field_value['callback'])) ? $field_value['callback'] : NULL;
  117. add_settings_field($this->settingsConfig['group'].'_'.$field_key, $field_value['label'], $function, $this->settingsConfig['menu']['page_name'],
  118. $section_key,array_merge($field_value,array('name' => $this->settingsConfig['group'].'_'.$field_key)));
  119. register_setting($this->settingsConfig['group'], $this->settingsConfig['group'].'_'.$field_key,$callback);
  120. endforeach;
  121. endforeach;
  122. }
  123. function section_text($value = NULL) {
  124. printf("%s",$this->settingsConfig['sections'][$value['id']]['description']);
  125. }
  126. function setting_string($value = NULL) {
  127. $options = get_option($value['name']);
  128. $default_value = (!empty ($value['default_value'])) ? $value['default_value'] : NULL;
  129. printf('<input id="%s" type="text" name="%1$s[text_string]" value="%2$s" size="40" /> %3$s%4$s',
  130. $value['name'],
  131. (!empty ($options['text_string'])) ? $options['text_string'] : $default_value,
  132. (!empty ($value['suffix'])) ? $value['suffix'] : NULL,
  133. (!empty ($value['description'])) ? sprintf("<br /><em>%s</em>",$value['description']) : NULL);
  134. }
  135. function setting_dropdown($value = NULL) {
  136. $options = get_option($value['name']);
  137. $default_value = (!empty ($value['default_value'])) ? $value['default_value'] : NULL;
  138. $current_value = ($options['text_string']) ? $options['text_string'] : $default_value;
  139. $chooseFrom = array();
  140. $choices = $this->settingsConfig['dropdown_options'][$value['dropdown']];
  141. foreach($choices AS $key=>$option) $chooseFrom[]= sprintf('<option value="%s" %s>%s</option>',$key,($current_value == $key ) ? ' selected="selected"' : NULL,$option);
  142. printf('<select id="%s" name="%1$s[text_string]">%2$s</select>%3$s',$value['name'],implode("",$chooseFrom),(!empty ($value['description'])) ? sprintf("<br /><em>%s</em>",$value['description']) : NULL);
  143. }
  144. //end class
  145. }
  146. $a = (sprintf('%s_settings','ob_campayn'));
  147. $b = (sprintf("%s_init",'ob_campayn'));
  148. $$b = new $a;
  149. ?>