randomadds.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. <?php
  2. // Add link to options page in Settings menu
  3. add_action('admin_menu', 'dfrads_menu');
  4. function dfrads_menu() {
  5. add_management_page('Datafeedr Random Ad Options', 'Datafeedr Random Ads', 'manage_options', 'datafeedr-ads', 'dfrads_options');
  6. }
  7. // Add "Settings" link on plugin page
  8. add_action('plugin_action_links_' . basename(dirname(__FILE__)) . '/' . basename(__FILE__), 'plugin_settings', 10, 4);
  9. function plugin_settings($links = array()) {
  10. $settings_link = '<a href="tools.php?page=datafeedr-ads">' . __('Settings', 'dfrads') . '</a>';
  11. array_unshift($links, $settings_link);
  12. return $links;
  13. }
  14. // Parse $_GET values
  15. function dfrads_return_get($field) {
  16. if (isset($_GET[$field]) && trim($_GET[$field]) != '') {
  17. return trim($_GET[$field]);
  18. }
  19. return false;
  20. }
  21. // Parse $_POST values
  22. function dfrads_return_post($field) {
  23. if (isset($_POST[$field]) && trim($_POST[$field]) != '') {
  24. return trim($_POST[$field]);
  25. }
  26. return false;
  27. }
  28. // Determine what to do
  29. function dfrads_options() {
  30. if (isset($_POST['submit-add-group'])) {
  31. check_admin_referer('dfrads_add_group');
  32. $ads = '';
  33. foreach ($_POST as $k => $v) {
  34. if (preg_match("/\bad_/", $k) && trim($v) != '') {
  35. $ads .= trim($v) . '[DFRADS]';
  36. }
  37. }
  38. if ($ads != '') {
  39. $ads = substr($ads, 0, -8);
  40. }
  41. $dfrads = get_option('dfrads');
  42. $new_group_id = dfrads_new_group_id($dfrads);
  43. $dfrads[$new_group_id]['name'] = (trim($_POST['group_name']) != '') ? stripslashes(trim($_POST['group_name'])) : $new_group_id;
  44. $dfrads[$new_group_id]['before_ad'] = stripslashes(trim($_POST['before_ad']));
  45. $dfrads[$new_group_id]['after_ad'] = stripslashes(trim($_POST['after_ad']));
  46. $dfrads[$new_group_id]['ads'] = stripslashes($ads);
  47. update_option('dfrads', $dfrads);
  48. }
  49. if (isset($_POST['submit-edit-group'])) {
  50. check_admin_referer('dfrads_edit_group');
  51. $ads = '';
  52. foreach ($_POST as $k => $v) {
  53. if (preg_match("/\bad_/", $k) && trim($v) != '') {
  54. $ads .= trim($v) . '[DFRADS]';
  55. }
  56. }
  57. if ($ads != '') {
  58. $ads = substr($ads, 0, -8);
  59. }
  60. $dfrads = get_option('dfrads');
  61. $group_id = $_POST['group_id'];
  62. $dfrads[$group_id]['name'] = (trim($_POST['group_name']) != '') ? stripslashes(trim($_POST['group_name'])) : $group_id;
  63. $dfrads[$group_id]['before_ad'] = stripslashes(trim($_POST['before_ad']));
  64. $dfrads[$group_id]['after_ad'] = stripslashes(trim($_POST['after_ad']));
  65. $dfrads[$group_id]['ads'] = stripslashes($ads);
  66. update_option('dfrads', $dfrads);
  67. }
  68. if (dfrads_return_get('action') == 'edit' && dfrads_return_get('group_id')) {
  69. dfrads_edit_group(dfrads_return_get('group_id'));
  70. } elseif (dfrads_return_get('action') == 'add') {
  71. dfrads_add_group();
  72. } elseif (dfrads_return_get('action') == 'duplicate' && dfrads_return_get('group_id')) {
  73. dfrads_duplicate_group(dfrads_return_get('group_id'));
  74. } elseif (dfrads_return_get('action') == 'delete' && dfrads_return_get('group_id')) {
  75. dfrads_delete_group(dfrads_return_get('group_id'));
  76. } else {
  77. dfrads_show_groups();
  78. }
  79. }
  80. // Add CSS, JS and initial HTML
  81. function dfrads_header($page = '') {?>
  82. <style type="text/css">
  83. .dfrads_ads
  84. {
  85. margin-bottom: 20px;
  86. background-color: #fff;
  87. padding: 10px;
  88. border: 1px #CBCBCB solid;
  89. }
  90. .dfrads_ad_title { display: block; }
  91. .dfrads_ad_preview { }
  92. .dfrads_textarea
  93. {
  94. float: left;
  95. margin-right: 10px;
  96. width: 500px;
  97. height: 220px;
  98. }
  99. .dfrads_longtext { width: 100%; }
  100. .clear
  101. {
  102. clear: both;
  103. display: block;
  104. overflow: hidden;
  105. visibility: hidden;
  106. width: 0;
  107. height: 0;
  108. }
  109. </style>
  110. <script type="text/javascript">
  111. // http://www.dustindiaz.com/add-and-remove-html-elements-dynamically-with-javascript/
  112. function addEvent() {
  113. var ni = document.getElementById('myDiv');
  114. var numi = document.getElementById('theValue');
  115. var num = (document.getElementById("theValue").value -1)+ 2;
  116. numi.value = num;
  117. var divIdName = "my"+num+"Div";
  118. var newdiv = document.createElement('div');
  119. newdiv.setAttribute("id",divIdName);
  120. newdiv.innerHTML = "<div class=\"dfrads_ads\">Add new ad here (<a href=\"javascript:;\" onclick=\"removeElement(\'"+divIdName+"\')\">Remove this ad box</a>)<br /><textarea name='ad_" + num + "' class='dfrads_textarea'><\/textarea><div class='clear'> <\/div></div>";
  121. ni.appendChild(newdiv);
  122. }
  123. function removeElement(divNum) {
  124. var d = document.getElementById('myDiv');
  125. var olddiv = document.getElementById(divNum);
  126. d.removeChild(olddiv);
  127. }
  128. </script>
  129. <div class="wrap" id="dfrads">
  130. <h2>Datafeedr Random Ads V2</h2>
  131. <ul class="subsubsub">
  132. <li><a href="tools.php?page=datafeedr-ads"<?php if ($page == ''): ?> class="current"<?php endif;?>>All Ad Groups</a> | </li>
  133. <li><a href="tools.php?page=datafeedr-ads&amp;action=add"<?php if ($page == 'add'): ?> class="current"<?php endif;?>>Add New Group</a></li>
  134. </ul>
  135. <div class="clear"> </div>
  136. <?php }
  137. // Close <div>
  138. function dfrads_footer() {
  139. echo '</div>';
  140. }
  141. // Show All Groups
  142. function dfrads_show_groups() {
  143. dfrads_header();
  144. ?>
  145. <table class="widefat" cellspacing="0">
  146. <thead>
  147. <tr>
  148. <th scope="col">Ad ID</th>
  149. <th scope="col">Ad Name</th>
  150. <th scope="col">Template Code</th>
  151. <th scope="col" style="text-align: center;">Actions</th>
  152. </tr>
  153. </thead>
  154. <tfoot>
  155. <tr>
  156. <th scope="col">Ad ID</th>
  157. <th scope="col">Ad Name</th>
  158. <th scope="col">Template Code</th>
  159. <th scope="col" style="text-align: center;">Actions</th>
  160. </tr>
  161. </tfoot>
  162. <tbody>
  163. <?php
  164. $dfrads = get_option('dfrads');
  165. $current_ads = '';
  166. $i = 0;
  167. if (!empty($dfrads)) {
  168. foreach ($dfrads as $k => $v) {
  169. $i++;
  170. if ($i % 2 == 0) {
  171. $class = "alternate";
  172. } else {
  173. $class = "";
  174. }
  175. $name = ($v['name'] == '') ? $k : $v['name'];
  176. ?>
  177. <tr class="<?php echo $class; ?>" valign="top">
  178. <td><?php echo $k; ?></td>
  179. <td><a href="tools.php?page=datafeedr-ads&amp;action=edit&amp;group_id=<?php echo $k; ?>"><b><?php echo $name; ?></b></a></td>
  180. <td style="white-space: nowrap;"><code>&lt;?php if (function_exists('dfrads')) { echo dfrads('<?php echo $k; ?>'); } ?&gt;</code></td>
  181. <td align="center">
  182. <a href="tools.php?page=datafeedr-ads&amp;action=edit&amp;group_id=<?php echo $k; ?>">edit</a> |
  183. <a href="<?php echo wp_nonce_url("tools.php?page=datafeedr-ads&amp;action=duplicate&amp;group_id=" . $k, 'dfrads_duplicate_group'); ?>"'><?php _e('duplicate', 'dfrads');?></a> |
  184. <a href="<?php echo wp_nonce_url("tools.php?page=datafeedr-ads&amp;action=delete&amp;group_id=" . $k, 'dfrads_delete_group'); ?>"' onclick="return confirm('<?php _e('You are about to delete this ad group.', 'datafeedr');?> \n\n <?php _e("Click \\'Cancel\\' to stop, \\'OK\\' to delete.", 'dfrads')?>')" class="delete" ><?php _e('delete', 'dfrads');?></a>
  185. </td>
  186. </tr>
  187. <?php
  188. }
  189. } else {
  190. echo '<tr><td colspan="4">There are no ad groups. <a href="tools.php?page=datafeedr-ads&amp;action=add">Create a new group</a>.</td></tr>';
  191. }
  192. ?>
  193. </tbody>
  194. </table>
  195. <?php dfrads_footer();
  196. }
  197. // Get new, unique group ID
  198. function dfrads_new_group_id($dfrads) {
  199. $new_group_id = mt_rand(1111111, 9999999);
  200. if (empty($dfrads)) {
  201. $dfrads = array();
  202. }
  203. if (array_key_exists($new_group_id, $dfrads)) {
  204. return dfrads_new_group_id($dfrads);
  205. } else {
  206. return $new_group_id;
  207. }
  208. }
  209. // Duplicate group
  210. function dfrads_duplicate_group($group_id = false) {
  211. check_admin_referer('dfrads_duplicate_group');
  212. if (!$group_id) {
  213. return dfrads_show_groups();
  214. }
  215. $dfrads = get_option('dfrads');
  216. $new_group_id = dfrads_new_group_id($dfrads);
  217. $dfrads[$new_group_id] = $dfrads[$group_id];
  218. $dfrads[$new_group_id]['name'] = $dfrads[$new_group_id]['name'] . ' copy';
  219. update_option('dfrads', $dfrads);
  220. dfrads_show_groups();
  221. }
  222. // Delete group
  223. function dfrads_delete_group($group_id = false) {
  224. check_admin_referer('dfrads_delete_group');
  225. if (!$group_id) {
  226. return dfrads_show_groups();
  227. }
  228. $dfrads = get_option('dfrads');
  229. unset($dfrads[$group_id]);
  230. update_option('dfrads', $dfrads);
  231. dfrads_show_groups();
  232. }
  233. // Show edit form for group
  234. function dfrads_edit_group($group_id = false) {
  235. if (!$group_id) {
  236. return dfrads_show_groups();
  237. }
  238. $dfrads = get_option('dfrads');
  239. $group = $dfrads[$group_id];
  240. $ads = explode("[DFRADS]", $group['ads']);
  241. $i = 0;
  242. $ad_textareas = '';
  243. foreach ($ads as $ad) {
  244. $i++;
  245. $ad_textareas .= "
  246. <div class=\"dfrads_ads\" id=\"my{$i}Div\">
  247. <span class=\"dfrads_ad_title\">Ad #{$i} (<a href=\"javascript:;\" onclick=\"removeElement('my{$i}Div')\">Remove this ad</a>)</span>
  248. <textarea name='ad_{$i}' class=\"dfrads_textarea\">{$ad}</textarea></span><span class=\"dfrads_ad_preview\">{$ad}</span>
  249. <div class=\"clear\"> </div>
  250. </div>
  251. ";
  252. }
  253. dfrads_header('edit');
  254. ?>
  255. <form action="tools.php?page=datafeedr-ads" method="post">
  256. <?php wp_nonce_field('dfrads_edit_group');?>
  257. <input name="group_id" type="hidden" value="<?php echo $group_id; ?>">
  258. <input type="hidden" value="<?php echo ($i++); ?>" id="theValue" />
  259. <h3>Optional Fields</h3>
  260. <p>The following fields are optional. You can insert text and/or HTML code before or after the entire ad group and each individual ad.</p>
  261. <table class="form-table">
  262. <tr>
  263. <th>Ad Group Name:</th>
  264. <td><input name="group_name" type="text" value="<?php echo $group['name']; ?>" /> (No HTML. This field will not appear on your site.)</td>
  265. </tr>
  266. <tr>
  267. <th>Before Ad:</th>
  268. <td><input name="before_ad" type="text" value="<?php echo $group['before_ad']; ?>" class="dfrads_longtext" /></td>
  269. </tr>
  270. <tr>
  271. <th>After Ad:</th>
  272. <td><input name="after_ad" type="text" value="<?php echo $group['after_ad']; ?>" class="dfrads_longtext" /></td>
  273. </tr>
  274. </table>
  275. <h3>Ad Boxes</h3>
  276. <p>Enter one ad into each box. To add additional boxes, click the "Add Box" link at the bottom of this page.</p>
  277. <div id="myDiv"> <?php echo $ad_textareas; ?> </div>
  278. <div>
  279. <a href="javascript:;" onclick="addEvent();" class="button-secondary">Add Box</a>
  280. <input name="submit-edit-group" type="submit" value="Save Changes" class="button-secondary">
  281. <div class="clear"> </div>
  282. </div>
  283. </form>
  284. </div>
  285. <?php dfrads_footer();
  286. }
  287. // Show "Add Group" form
  288. function dfrads_add_group() {
  289. dfrads_header('add');
  290. ?>
  291. <h3>Optional Fields</h3>
  292. <p>The following fields are optional. You can insert text and/or HTML code before or after the entire ad group and each individual ad.</p>
  293. <form action="tools.php?page=datafeedr-ads" method="post">
  294. <?php wp_nonce_field('dfrads_add_group');?>
  295. <input type="hidden" value="1" id="theValue" />
  296. <table class="form-table">
  297. <tr>
  298. <th>Ad Group Name</th>
  299. <td><input name="group_name" type="text" value="" /> (No HTML. This field will not appear on your site.)</td>
  300. </tr>
  301. <tr>
  302. <th>Before Ad:</th>
  303. <td><input name="before_ad" type="text" value="" class="dfrads_longtext" /></td>
  304. </tr>
  305. <tr>
  306. <th>After Ad:</th>
  307. <td><input name="after_ad" type="text" value="" class="dfrads_longtext" /></td>
  308. </tr>
  309. </table>
  310. <h3>Ad Boxes</h3>
  311. <p>Enter one ad into each box. To add additional boxes, click the "Add Box" button at the bottom of this page.</p>
  312. <div id="myDiv">
  313. <div class="dfrads_ads" id="my1Div">
  314. <span class="dfrads_ad_title">Ad #1 (<a href="javascript:;" onclick="removeElement('my1Div')">Remove this ad</a>)</span>
  315. <textarea name='ad_1' class='dfrads_textarea'></textarea>
  316. <div class="clear"> </div>
  317. </div>
  318. </div>
  319. <div>
  320. <a href="javascript:;" onclick="addEvent();" class="button-secondary">Add Box</a>
  321. <input name="submit-add-group" type="submit" value="Save Ad Group" class="button-secondary">
  322. <div class="clear"> </div>
  323. </div>
  324. </form>
  325. <?php dfrads_footer();
  326. }
  327. // Show error message if user is Admin
  328. function dfrads_display_admin_error($msg = '') {
  329. if (current_user_can('manage_options')) {
  330. return '<div style="color:red;padding:10px;border:red 1px solid;background:#FFEFF1;"><b>Datafeedr Random Ads Message:</b><br />' . $msg . '</div>';
  331. }
  332. return '';
  333. }
  334. // Display the ads from a template function
  335. function dfrads($group_id = false) {
  336. if (!$group_id) {
  337. return dfrads_display_admin_error('A <i>group ID</i> is required.');
  338. }
  339. $dfrads = get_option('dfrads');
  340. if (!is_array($dfrads[$group_id])) {
  341. return dfrads_display_admin_error('The ad group "<i>' . $group_id . '</i>" does not exist.');
  342. }
  343. $ads = explode('[DFRADS]', $dfrads[$group_id]['ads']);
  344. $num_ads = count($ads);
  345. $ad_id = mt_rand(1, $num_ads);
  346. $ad = $ads[($ad_id - 1)];
  347. return $dfrads[$group_id]['before_ad'] . $ad . $dfrads[$group_id]['after_ad'];
  348. }
  349. /**
  350. * Add function to widgets_init that'll load our widget.
  351. * @since 0.1
  352. */
  353. add_action('widgets_init', 'example_load_widgets');
  354. /**
  355. * Register our widget.
  356. * 'DfrAds_Widget' is the widget class used below.
  357. *
  358. * @since 0.1
  359. */
  360. function example_load_widgets() {
  361. register_widget('DfrAds_Widget');
  362. }
  363. /**
  364. * Example Widget class.
  365. * This class handles everything that needs to be handled with the widget:
  366. * the settings, form, display, and update. Nice!
  367. *
  368. * @since 0.1
  369. */
  370. class DfrAds_Widget extends WP_Widget {
  371. /**
  372. * Widget setup.
  373. */
  374. function __construct() {
  375. /* Widget settings. */
  376. $widget_ops = array('classname' => 'dfrads', 'description' => __('Display your rotating ads in the sidebar.', 'dfrads'));
  377. /* Widget control settings. */
  378. $control_ops = array('id_base' => 'dfrads-widget');
  379. /* Create the widget. */
  380. parent::__construct('dfrads-widget', __('Datafeedr Random Ads', 'dfrads'), $widget_ops, $control_ops);
  381. }
  382. /**
  383. * How to display the widget on the screen.
  384. */
  385. function widget($args, $instance) {
  386. extract($args);
  387. /* Our variables from the widget settings. */
  388. $title = apply_filters('widget_title', $instance['title']);
  389. $group_id = $instance['group_id'];
  390. /* Before widget (defined by themes). */
  391. echo $before_widget;
  392. /* Display the widget title if one was input (before and after defined by themes). */
  393. if ($title) {
  394. echo $before_title . $title . $after_title;
  395. }
  396. /* Display name from widget settings if one was input. */
  397. echo dfrads($group_id);
  398. /* After widget (defined by themes). */
  399. echo $after_widget;
  400. }
  401. /**
  402. * Update the widget settings.
  403. */
  404. function update($new_instance, $old_instance) {
  405. $instance = $old_instance;
  406. /* Strip tags for title and name to remove HTML (important for text inputs). */
  407. $instance['title'] = strip_tags($new_instance['title']);
  408. /* No need to strip tags for sex and show_sex. */
  409. $instance['group_id'] = $new_instance['group_id'];
  410. return $instance;
  411. }
  412. /**
  413. * Displays the widget settings controls on the widget panel.
  414. * Make use of the get_field_id() and get_field_name() function
  415. * when creating your form elements. This handles the confusing stuff.
  416. */
  417. function form($instance) {
  418. /* Set up some default widget settings. */
  419. $defaults = array('title' => __('Our Sponsors', 'dfrads'));
  420. $instance = wp_parse_args((array) $instance, $defaults);
  421. $dfrads = get_option('dfrads');
  422. ?>
  423. <!-- Widget Title: Text Input -->
  424. <p>
  425. <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'dfrads');?></label>
  426. <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" class="widefat" type="text" />
  427. </p>
  428. <!-- group_id: Select Box -->
  429. <p>
  430. <label for="<?php echo $this->get_field_id('group_id'); ?>"><?php _e('Select Ad Group:', 'example');?></label>
  431. <select id="<?php echo $this->get_field_id('group_id'); ?>" name="<?php echo $this->get_field_name('group_id'); ?>" class="widefat">
  432. <?php
  433. if (count($dfrads) > 0) {
  434. foreach ($dfrads as $k => $v) {
  435. $name = ($v['name'] == '') ? $k : $v['name'];
  436. ?>
  437. <option <?php if ($k == $instance['group_id']) {
  438. echo 'selected="selected"';
  439. }
  440. ?> value="<?php echo $k; ?>"><?php echo $name; ?></option>
  441. <?php
  442. }
  443. } else {
  444. ?>
  445. <option>You have not created any ad groups.</option>
  446. <?php
  447. }
  448. ?>
  449. </select>
  450. </p>
  451. <?php
  452. }
  453. }