NMMailChimp_Admin

Working behind the seen.

Defined (1)

The class is defined in the following location(s).

/classes/admin.class.php  
  1. class NMMailChimp_Admin extends NMMailChimp{ 
  2.  
  3.  
  4. var $menu_pages, $plugin_scripts_admin, $plugin_settings; 
  5.  
  6.  
  7. function __construct() { 
  8.  
  9.  
  10. //setting plugin meta saved in config.php 
  11. $this -> plugin_meta = get_plugin_meta_nm_mailchimp(); 
  12.  
  13. //getting saved settings 
  14. $this -> plugin_settings = get_option($this->plugin_meta['shortname'].'_settings'); 
  15.  
  16.  
  17. /** 
  18. * [1] 
  19. * TODO: change this for plugin admin pages 
  20. */ 
  21. if(1) { 
  22.  
  23. $this -> menu_pages = array(array( 
  24. 'page_title' => $this->plugin_meta['name'],  
  25. 'menu_title' => $this->plugin_meta['name'],  
  26. 'cap' => 'manage_options',  
  27. 'slug' => $this->plugin_meta['shortname'],  
  28. 'callback' => 'nm_settings',  
  29. 'parent_slug' => '', ),  
  30.  
  31. array( 
  32. 'page_title' => 'Mailchimp Lists and Vars',  
  33. 'menu_title' => 'Lists, Vars & Grouping',  
  34. 'cap' => 'manage_options',  
  35. 'slug' => $this->plugin_meta['shortname'].'_lists',  
  36. 'callback' => 'mc_lists',  
  37. 'parent_slug' => $this->plugin_meta['shortname'], ),  
  38.  
  39. array( 
  40. 'page_title' => __('Subscription Forms', 'nm-mailchimp'),  
  41. 'menu_title' => __('Subscription Forms', 'nm-mailchimp'),  
  42. 'cap' => 'manage_options',  
  43. 'slug' => $this->plugin_meta['shortname'].'_forms',  
  44. 'callback' => 'mc_forms',  
  45. 'parent_slug' => $this->plugin_meta['shortname'], ),  
  46.  
  47. array( 
  48. 'page_title' => __('Form Designer', 'nm-mailchimp'),  
  49. 'menu_title' => __('Form Designer', 'nm-mailchimp'),  
  50. 'cap' => 'manage_options',  
  51. 'slug' => $this->plugin_meta['shortname'].'_form_designer',  
  52. 'callback' => 'mc_form_designer',  
  53. 'parent_slug' => $this->plugin_meta['shortname'], ),  
  54.  
  55. array( 
  56. 'page_title' => 'Campaign Manager',  
  57. 'menu_title' => 'Campaign Manager',  
  58. 'cap' => 'manage_options',  
  59. 'slug' => $this->plugin_meta['shortname'].'_campaigns',  
  60. 'callback' => 'mc_campaigns',  
  61. 'parent_slug' => $this->plugin_meta['shortname'], ),  
  62.  
  63. ); 
  64.  
  65. }else{ 
  66.  
  67. $this->menu_pages = array ( 
  68. array ( 
  69. 'page_title' => $this->plugin_meta ['name'],  
  70. 'menu_title' => $this->plugin_meta ['name'] . ' - validate plugin',  
  71. 'cap' => 'edit_plugins',  
  72. 'slug' => $this->plugin_meta ['shortname'],  
  73. 'callback' => 'activate_plugin',  
  74. 'parent_slug' => '' 
  75. ),  
  76. ); 
  77.  
  78.  
  79.  
  80.  
  81.  
  82. /** 
  83. * [2] 
  84. * TODO: Change this for admin related scripts 
  85. * JS scripts and styles to loaded 
  86. * ADMIN 
  87. */ 
  88. $this -> plugin_scripts_admin = array( 
  89.  
  90.  
  91.  
  92. array( 'script_name' => 'mc-angular',  
  93. 'script_source' => 'js/angular/angular.min.js',  
  94. 'localized' => false,  
  95. 'type' => 'js',  
  96. 'page_slug' => array($this->plugin_meta['shortname'].'_lists', $this->plugin_meta['shortname'].'_forms', $this->plugin_meta['shortname'].'_form_designer', $this->plugin_meta['shortname'].'_campaigns'),  
  97. 'depends' => array ('jquery', 'wp-color-picker') 
  98. ),  
  99.  
  100.  
  101.  
  102. array( 'script_name' => 'scripts-chosen',  
  103. 'script_source' => 'js/chosen/chosen.jquery.min.js',  
  104. 'localized' => false,  
  105. 'type' => 'js',  
  106. 'page_slug' => $this->plugin_meta['shortname'],  
  107. 'depends' => array ('jquery') 
  108. ),  
  109.  
  110. array ( 
  111. 'script_name' => 'chosen-style',  
  112. 'script_source' => 'js/chosen/chosen.min.css',  
  113. 'localized' => false,  
  114. 'type' => 'style',  
  115. 'page_slug' => $this->plugin_meta['shortname'],  
  116. ),  
  117.  
  118.  
  119. array( 'script_name' => 'scripts-admin',  
  120. 'script_source' => 'js/admin.js',  
  121. 'localized' => true,  
  122. 'type' => 'js',  
  123. 'page_slug' => $this->plugin_meta['shortname'],  
  124. 'depends' => array ( 
  125. 'jquery',  
  126. 'jquery-ui-tabs',  
  127. 'wp-color-picker',  
  128. //'media-upload',  
  129. //'thickbox' 
  130. ),  
  131. 'in_footer' => true,  
  132. ),  
  133.  
  134.  
  135. array ( 
  136. 'script_name' => 'ui-style',  
  137. 'script_source' => 'js/ui/css/smoothness/jquery-ui-1.10.3.custom.min.css',  
  138. 'localized' => false,  
  139. 'type' => 'style',  
  140. 'page_slug' => $this->plugin_meta['shortname'],  
  141. ),  
  142. array ( 
  143. 'script_name' => 'thickbox',  
  144. 'script_source' => 'shipped',  
  145. 'localized' => false,  
  146. 'type' => 'style',  
  147. 'page_slug' => $this->plugin_meta['shortname'],  
  148. ),  
  149.  
  150. array ( 
  151. 'script_name' => 'wp-color-picker',  
  152. 'script_source' => 'shipped',  
  153. 'localized' => false,  
  154. 'type' => 'style',  
  155. 'page_slug' => array ( 
  156. $this->plugin_meta ['shortname'], $this->plugin_meta['shortname'].'_form_designer',  
  157. ),  
  158.  
  159.  
  160. ); 
  161.  
  162. $this -> ajax_callbacks = array('save_settings' => true); //do not change this action, is for admin 
  163.  
  164.  
  165.  
  166. add_action('admin_menu', array($this, 'add_menu_pages')); 
  167.  
  168. /** 
  169. * laoding admin scripts only for plugin pages 
  170. * since 27 september, 2014 
  171. * Najeeb's  
  172. */ 
  173. add_action( 'admin_enqueue_scripts', array ( 
  174. $this,  
  175. 'load_scripts_admin' 
  176. )); 
  177.  
  178.  
  179. $this -> do_callbacks(); 
  180.  
  181.  
  182.  
  183.  
  184.  
  185. function load_scripts_admin($hook) { 
  186.  
  187. /** 
  188. * Note: we mostly hook independant page for our plugins 
  189. * so it's page hook will be: toplevel_page_PAGE_SLUG 
  190. */  
  191.  
  192. //var_dump($hook); 
  193.  
  194. // loading script for only plugin optios pages 
  195. // page_slug is key in $plugin_scripts_admin which determine the page 
  196.  
  197.  
  198.  
  199. foreach ( $this->plugin_scripts_admin as $script ) { 
  200.  
  201. $attach_script = false; 
  202. if (is_array ( $script ['page_slug'] )) { 
  203.  
  204.  
  205. foreach( $script ['page_slug'] as $page) { 
  206. /** 
  207. * its very important var, when menu page is loaded as submenu of current plugin 
  208. * then it has different hook_suffix 
  209. */ 
  210. $plugin_sublevel = "mailchimp-campaign_page_".$page; 
  211. $plugin_toplevel = "toplevel_page_".$page; 
  212.  
  213. if ( $hook == $plugin_toplevel || $hook == $plugin_sublevel) { 
  214. $attach_script = true; 
  215. }  
  216. } else { 
  217. /** 
  218. * its very important var, when menu page is loaded as submenu of current plugin 
  219. * then it has different hook_suffix 
  220. */ 
  221. $plugin_sublevel = "THE_PLUGIN_HOOK".$script ['page_slug']; 
  222. $plugin_toplevel = "toplevel_page_".$script ['page_slug']; 
  223. if ( $hook == $plugin_toplevel || $hook == $plugin_sublevel) { 
  224.  
  225. $attach_script = true; 
  226.  
  227. //echo 'script page '.$script_pages; 
  228. if( $attach_script ) { 
  229.  
  230. // adding media upload scripts (WP 3.5+) 
  231. wp_enqueue_media(); 
  232.  
  233. // localized vars in js 
  234. $arrLocalizedVars = array ( 
  235. 'plugin_url' => $this->plugin_meta ['url'],  
  236. 'doing' => $this->plugin_meta ['url'] . '/images/loading.gif',  
  237. 'plugin_admin_page' => admin_url ( 'options-general.php?page=nm_mailchimp' ) 
  238. ); 
  239.  
  240. // checking if it is style 
  241. if ($script ['type'] == 'js') { 
  242. $depends = (isset($script['depends']) ? $script['depends'] : NULL); 
  243. $in_footer = (isset($script['in_footer']) ? $script['in_footer'] : false); 
  244. wp_enqueue_script ( $this->plugin_meta ['shortname'] . '-' . $script ['script_name'], $this->plugin_meta ['url'] . $script ['script_source'], $depends, $this->plugin_meta['plugin_version'], $in_footer ); 
  245.  
  246. // if localized 
  247. if ($script ['localized']) 
  248. wp_localize_script ( $this->plugin_meta ['shortname'] . '-' . $script ['script_name'], $this -> plugin_meta['shortname'] . '_vars', $arrLocalizedVars ); 
  249. } else { 
  250.  
  251. if ($script ['script_source'] == 'shipped') 
  252. wp_enqueue_style ( $script ['script_name'] ); 
  253. else 
  254. wp_enqueue_style ( $this->plugin_meta ['shortname'] . '-' . $script ['script_name'], $this->plugin_meta ['url'] . $script ['script_source'] ); 
  255.  
  256.  
  257.  
  258.  
  259. /** 
  260. * creating menu page for this plugin 
  261. */ 
  262.  
  263. function add_menu_pages() { 
  264.  
  265. foreach ($this -> menu_pages as $page) { 
  266.  
  267. if ($page['parent_slug'] == '') { 
  268.  
  269. $menu = add_menu_page(__($page['page_title'], $this->plugin_meta['shortname']),  
  270. __($page['menu_title'], $this->plugin_meta['shortname']),  
  271. $page['cap'],  
  272. $page['slug'],  
  273. array($this, $page['callback']),  
  274. $this->plugin_meta['logo'],  
  275. $this->plugin_meta['menu_position']); 
  276. }else{ 
  277.  
  278. $menu = add_submenu_page($page['parent_slug'],  
  279. __($page['page_title'], $this->plugin_meta['shortname']),  
  280. __($page['menu_title'], $this->plugin_meta['shortname']),  
  281. $page['cap'],  
  282. $page['slug'],  
  283. array($this, $page['callback']) 
  284. ); 
  285.  
  286.  
  287.  
  288.  
  289. //====================== CALLBACKS ================================= 
  290.  
  291. /** 
  292. * saving admin setting in wp option data table 
  293. */ 
  294. function save_settings() { 
  295.  
  296. //print_r($_REQUEST); exit; 
  297.  
  298. update_option($this -> plugin_meta['shortname'].'_settings', $_REQUEST); 
  299. _e('All options are updated', 'nm-mailchimp'); 
  300. die(0); 
  301.  
  302. function mc_form_designer() { 
  303.  
  304. $this -> load_template('admin/form-designer.php'); 
  305.  
  306.  
  307. function nm_settings() { 
  308.  
  309. $this -> load_template('admin/settings.php'); 
  310.  
  311. function mc_lists() { 
  312.  
  313. $this -> load_template('admin/lists.php'); 
  314.  
  315. function mc_forms() { 
  316.  
  317. $this -> load_template('admin/forms.php'); 
  318.  
  319. function mc_campaigns() { 
  320.  
  321. $this -> load_template('admin/campaigns.php'); 
  322.  
  323.  
  324.  
  325. /** ============== some helper functions =============== */ 
  326. function render_settings_input($data) { 
  327.  
  328. $field_id = $data['id']; 
  329. $type = $data['type']; 
  330.  
  331. if( isset($this ->plugin_settings[ $data['id']]) && !is_array($this ->plugin_settings[ $data['id']])) 
  332. $value = stripslashes( $this ->plugin_settings[ $data['id']] ); 
  333. else  
  334. $value = (isset($this ->plugin_settings[ $data['id']]) ? $this ->plugin_settings[ $data['id']] : ''); 
  335.  
  336. $options = (isset($data['options']) ? $data['options'] : ''); 
  337.  
  338. switch($type) { 
  339.  
  340. case 'text' : 
  341. echo '<input type="text" name="' . $field_id . '" id="' . $field_id . '" value="' . $value . '" class="regular-text">'; 
  342. break; 
  343.  
  344. case 'textarea': 
  345. echo '<textarea cols="45" rows="6" name="' . $field_id . '" id="' . $field_id . '" >'.$value.'</textarea>'; 
  346. break; 
  347.  
  348. case 'checkbox': 
  349.  
  350. foreach($options as $k => $label) { 
  351.  
  352. echo '<label for="'.$field_id.'-'.$k.'">'; 
  353. echo '<input type="checkbox" name="' . $field_id . '" id="'.$field_id.'-'.$k.'" value="' . $k . '" '.checked( $value, $k, false).'>'; 
  354. printf(__("%s", 'nm-mailchimp'), $label); 
  355. echo '</label> '; 
  356.  
  357. break; 
  358.  
  359. case 'radio': 
  360.  
  361. foreach($options as $k => $label) { 
  362.  
  363. echo '<label for="'.$field_id.'-'.$k.'">'; 
  364. echo '<input type="radio" name="' . $field_id . '" id="'.$field_id.'-'.$k.'" value="' . $k . '" '.checked( $value, $k, false).'>'; 
  365. printf(__("%s", 'nm-mailchimp'), $label); 
  366. echo '</label> '; 
  367.  
  368. break; 
  369.  
  370. case 'select': 
  371.  
  372. $default = (isset($data['default']) ? $data['default'] : 'Select option'); 
  373.  
  374. echo '<select name="' . $field_id . '" id="' . $field_id . '" class="the_chosen">'; 
  375. echo '<option value="">'.$default.'</option>'; 
  376.  
  377. foreach($options as $k => $label) { 
  378.  
  379. echo '<option value="'.$k.'" '.selected( $value, $k, false).'>'.$label.'</option>'; 
  380.  
  381. echo '</select>'; 
  382. break; 
  383.  
  384. case 'color' : 
  385. echo '<input type="text" name="' . $field_id . '" id="' . $field_id . '" value="' . $value . '" class="wp-color-field">'; 
  386. break; 
  387.  
  388. // =========== some special settings ==================== 
  389.  
  390. case 'users': 
  391.  
  392. $default = (isset($data['default']) ? $data['default'] : 'Select option'); 
  393.  
  394.  
  395. $args = array( 'blog_id' => $GLOBALS['blog_id'],  
  396. 'orderby' => 'nicename',  
  397. 'order' => 'ASC', ); 
  398.  
  399. $wp_users = get_users($args); 
  400.  
  401. $multiple = ($data['multiselect'] == true ? 'multiple' : '');  
  402.  
  403. echo '<select name="' . $field_id . '[]" id="' . $field_id . '" class="the_chosen" '.$multiple.'>'; 
  404. echo '<option value="">'.$default.'</option>'; 
  405.  
  406. foreach($wp_users as $user) { 
  407.  
  408. if($value) { 
  409. if(in_array($user -> ID, $value)) 
  410. $selected = 'selected="selected"'; 
  411. else 
  412. $selected = ''; 
  413.  
  414. $label = $user -> display_name . ' ('.$user -> user_login.')'; 
  415. echo '<option value="'.$user -> ID.'" '.$selected.'>'.$label.'</option>'; 
  416.  
  417. echo '</select>'; 
  418. break; 
  419.  
  420. case 'categories': 
  421.  
  422. $default = (isset($data['default']) ? $data['default'] : 'Select option'); 
  423.  
  424.  
  425. $args = array(  
  426. 'type' => 'post',  
  427. 'child_of' => 0,  
  428. 'parent' => '',  
  429. 'orderby' => 'name',  
  430. 'order' => 'ASC',  
  431. 'hide_empty' => 0,  
  432. 'hierarchical' => 1, ); 
  433.  
  434. $wp_cats = get_categories($args); 
  435.  
  436. $multiple = ($data['multiselect'] == true ? 'multiple' : ''); 
  437.  
  438. echo '<select name="' . $field_id . '[]" id="' . $field_id . '" class="the_chosen" '.$multiple.'>'; 
  439. echo '<option value="">'.$default.'</option>'; 
  440.  
  441. foreach($wp_cats as $cat) { 
  442.  
  443. if($value) { 
  444. if(in_array($cat -> term_id, $value)) 
  445. $selected = 'selected="selected"'; 
  446. else 
  447. $selected = ''; 
  448.  
  449. $label = $cat -> name . ' ('.$cat -> category_nicename.')'; 
  450. echo '<option value="'.$cat -> term_id.'" '.$selected.'>'.$label.'</option>'; 
  451.  
  452. echo '</select>'; 
  453. break; 
  454.  
  455. case 'pages': 
  456.  
  457.  
  458. $default = (isset($data['default']) ? $data['default'] : 'Select option'); 
  459.  
  460.  
  461. $args = array( 
  462. 'sort_order' => 'ASC',  
  463. 'sort_column' => 'post_title',  
  464. 'post_type' => 'page',  
  465. 'post_status' => 'publish'); 
  466.  
  467. $wp_pages = get_pages($args); 
  468.  
  469. $multiple = ($data['multiselect'] == true ? 'multiple' : ''); 
  470.  
  471. echo '<select name="' . $field_id . '[]" id="' . $field_id . '" class="the_chosen" '.$multiple.'>'; 
  472. echo '<option value="">'.$default.'</option>'; 
  473.  
  474. foreach($wp_pages as $page) { 
  475.  
  476. if($value) { 
  477. if(in_array($page -> ID, $value)) 
  478. $selected = 'selected="selected"'; 
  479. else 
  480. $selected = ''; 
  481.  
  482. $label = $page -> post_title; 
  483. echo '<option value="'.$page -> ID.'" '.$selected.'>'.$label.'</option>'; 
  484.  
  485. echo '</select>'; 
  486. break; 
  487.  
  488. case 'media' : 
  489.  
  490. if(function_exists('wp_enqueue_media')) 
  491. wp_enqueue_media(); 
  492.  
  493. echo '<input type="text" name="' . $field_id . '" id="' . $field_id . '" value="' . $value . '" class="regular-text">'; 
  494. echo '<button class="button nm-media-upload">Select</button>'; 
  495. echo ' <a href="javascsript:;" class="remove-media"><i class="fa fa-pencil"></i>'.__('Remove', 'nm-mailchimp').'</a>'; 
  496.  
  497. //rendering image thumb 
  498. if($value) 
  499. echo '<br><span class="the-thumb"><img width="75" src="'.$value.'"></span>'; 
  500. else 
  501. echo '<br><span class="the-thumb"></span>'; 
  502. break; 
  503.  
  504. case 'file': 
  505. $file = $this->plugin_meta['path'] .'/templates/admin/'.$data['id']; 
  506. if(file_exists($file)) 
  507. include $file; 
  508. else 
  509. echo 'file not exists '.$file; 
  510. break; 
  511.  
  512.  
  513. function activate_plugin() { 
  514.  
  515. echo '<div class="wrap">'; 
  516. echo '<h2>' . __ ( 'Provide API key below:', 'nm_webcontact' ) . '</h2>'; 
  517. echo '<p>' . __ ( 'If you don\'t know your API key, please login into your: <a target="_blank" href="http://wordpresspoets.com/member-area">Member area</a>', 'nm_webcontact' ) . '</p>'; 
  518.  
  519. echo '<form onsubmit="return validate_api_'.$this->plugin_meta['shortname'].'(this)">'; 
  520. echo '<p><label id="plugin_api_key">'.__('Entery API key', 'nm-mailchimp').':</label><br /><input type="text" name="plugin_api_key" id="plugin_api_key" /></p>'; 
  521. wp_nonce_field(); 
  522. echo '<p><input type="submit" class="button-primary button" name="plugin_api_key" /></p>'; 
  523. echo '<p id="nm-sending-api"></p>'; 
  524. echo '</form>'; 
  525.  
  526. echo '</div>'; 
  527.  
  528.  
  529.