Advertisement
Guest User

Untitled

a guest
Jul 17th, 2012
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.33 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: CrankyAds
  4. Plugin URI: http://www.crankyads.com
  5. Description: CrankyAds allows you to sell advertising space on your blog quickly and easily.
  6. Version: 1.8.4
  7. Author: CrankyAds
  8. Author URI: http://www.crankyads.com
  9. */
  10.  
  11. // TODO (791): We will want to host our plugin on http://wordpress.org/extend/plugins/ as described in http://codex.wordpress.org/Writing_a_Plugin and http://wordpress.org/extend/plugins/about/readme.txt
  12.  
  13. /* ========================================================================================================== */
  14.  
  15. include_once dirname(__FILE__).'/includes/settings.php';
  16. include_once dirname(__FILE__).'/includes/Proxy.php';
  17. include_once dirname(__FILE__).'/includes/ContentController.php';
  18. include_once dirname(__FILE__).'/includes/ZoneWidget.php';
  19. include_once dirname(__FILE__).'/includes/DataContext.php';
  20.  
  21. if(CRANKY_ADS_FORCE_PHP_ERROR_REPORTING_ALL)
  22. {
  23. error_reporting(E_ALL);
  24. //error_reporting(E_ALL & ~E_NOTICE);
  25. }
  26.  
  27. /// <summary>Singleton instance of the CrankyAds plugin class</summary>
  28. global $CrankyAdsPlugin;
  29.  
  30. /* ========================================================================================================== */
  31.  
  32. if (!class_exists("CrankyAdsPlugin"))
  33. {
  34.  
  35. // ==========================================================================================================
  36. // Wordpress PHP Functions
  37. // (Use these to work with CrankyAds anywhere in your Wordpress PHP code)
  38. // ==========================================================================================================
  39.  
  40. /// <summary>
  41. /// (PHP Helper Function) Serves ads for the specified CrankyAds Zone
  42. /// </summary>
  43. /// <remarks>This is a PHP helper function that lets users manually add a CrankyAdsZone anywhere in their PHP code without having to use Widgets</remarks>
  44. function CrankyAdsZone( $intZoneId )
  45. {
  46. global $CrankyAdsPlugin;
  47. $CrankyAdsPlugin->ContentController->ServeAdZoneAds($intZoneId);
  48. }
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55. // ==========================================================================================================
  56. // CrankyAds Plugin Class
  57. // ==========================================================================================================
  58.  
  59. /// <summary>
  60. /// The main CrankyAds Plugin class
  61. /// </summary>
  62. /// <remarks>
  63. /// . This class plugs the CrankyAds plugin functionality INTO Wordpress
  64. /// </remarks>
  65. class CrankyAdsPlugin
  66. {
  67. var $WP_SHORT_CODE_ADVERTISE_HERE = "CrankyAdsAdvertiseHere";
  68.  
  69. /// <summary>Core plugin components</summary>
  70. var $Proxy;
  71. var $ContentController;
  72. var $WidgetFactory;
  73. var $DataContext;
  74. var $Cache;
  75.  
  76. /// <summary>Content to be output at the footer of the page (if any)</summary>
  77. var $FooterContent = false;
  78.  
  79. /// <summary>
  80. /// Constructor - initialize the Cranky Ads Plugin and hook it up to Wordpress
  81. /// </summary>
  82. function CrankyAdsPlugin()
  83. {
  84. // ** Setup the plugin
  85. $this->DataContext = new CrankyAdsDataContext();
  86. $this->Cache = new CrankyAdsCache($this->DataContext);
  87. $this->Proxy = new CrankyAdsProxy( $this->DataContext, $this->Cache );
  88. $this->ContentController = new CrankyAdsContentController( $this->DataContext, $this->Proxy, $this->Cache, $this );
  89. $this->WidgetFactory = new CrankyAdsZoneWidgetFactory();
  90. $this->DataContext->Init($this->Proxy);
  91.  
  92. if(function_exists('add_action')) // Wordpress is loaded
  93. {
  94. // Register Wordpress actions / hooks
  95. add_action('admin_menu', array(&$this, 'OnAddAdminMenuItem')); // Register our own Admin Menu
  96. add_filter('plugin_action_links', array(&$this, 'OnAddPluginLinks'), 10, 2); // Register our own Plugin Links
  97. add_action('widgets_init', array(&$this, 'OnRegisterWidgets') ); // Register our Widgets
  98. add_action('init', array(&$this, 'OnRegisterShortCodes') ); // Register short codes
  99.  
  100. register_activation_hook( __FILE__, array(&$this, 'OnPluginActivated') ); // Register a hook to let us know when this plugin is activated
  101. register_deactivation_hook( __FILE__, array(&$this, 'OnPluginDeactivated') ); // Register a hook to let us know when this plugin is deactivated
  102.  
  103. // Register AJAX callbacks where we have control over the entire HTTP Response (for both admin and non-admin calls)
  104. add_action('wp_ajax_crankyads_pushnotification', array(&$this, 'PushNotification') ); // Handle PUSH CrankyAds notification messages
  105. add_action('wp_ajax_nopriv_crankyads_pushnotification', array(&$this, 'PushNotification') );
  106.  
  107. add_action('wp_ajax_crankyads_servecontent', array(&$this, 'ServeRemoteContent') ); // Serve remote server content (used by HttpResponse.ReplaceContentUrlPlaceholders(..))
  108. add_action('wp_ajax_nopriv_crankyads_servecontent', array(&$this, 'ServeRemoteContent') );
  109.  
  110. add_action('wp_ajax_crankyads_serveadzoneasync', array(&$this, 'ServeAdZoneAsync') ); // Serve the Ad Zone HTML asynchronously
  111. add_action('wp_ajax_nopriv_crankyads_serveadzoneasync', array(&$this, 'ServeAdZoneAsync') ); // ** NOTE **: If changing these lines remember to also update ContentController.ServeAdZoneAds(..)
  112. add_action('wp_ajax_crankyads_serveadvertisehereasync', array(&$this, 'ServeAdvertiseHereAsync') ); // Serve the Advertise Here HTML asynchronously
  113. add_action('wp_ajax_nopriv_crankyads_serveadvertisehereasync', array(&$this, 'ServeAdvertiseHereAsync') ); // ** NOTE **: If changing these lines remember to also update ContentController.ServeAdvertiseHere(..)
  114. add_action('wp_ajax_crankyads_serveheadasync', array(&$this, 'ServeHeadAsync') ); // Serve the <head> css block asynchronously
  115. add_action('wp_ajax_nopriv_crankyads_serveheadasync', array(&$this, 'ServeHeadAsync') ); // ** NOTE **: If changing these lines remember to also update ContentController.ServeHead(..)
  116.  
  117. add_action('wp_ajax_crankyads_checkpluginupgrade', array(&$this, 'CheckPluginUpgrade') ); // Perform a call to the server to check whether a plugin upgrade is required (because a new version is available)
  118. add_action('wp_ajax_nopriv_crankyads_checkpluginupgrade', array(&$this, 'CheckPluginUpgrade') );
  119.  
  120. // Register a hook to output any footer content
  121. add_action('wp_footer', array(&$this, 'ServeFooter') );
  122.  
  123. // Initialize the remaining plugin settings (after Wordpress is loaded)
  124. add_action('init', array(&$this, 'OnCrankyAdsPluginInit') );
  125. }
  126. }
  127.  
  128. /// <summary>
  129. /// This is an extension of the plugin constructor, however, unlike the constructor this code is executed after the rest of Wordpress has fully loaded
  130. /// </summary>
  131. function OnCrankyAdsPluginInit()
  132. {
  133. // Note: At this point we can call init on any classes that need to perform initialization after Wordpress is fully loaded.
  134. // But make sure to do that after the Installation section otherwise they might not yet be configured.
  135.  
  136. // ** Installation
  137. // . This is the one time initialization we have to perform whenever the plugin is installed or upgraded
  138. // . The check below is a 'just in case' check since the installation should have occurred as part of the OnPluginActivated() call
  139. $dbVersion = $this->DataContext->GetInstalledPluginVersion();
  140.  
  141. if($dbVersion !== CRANKY_ADS_PLUGIN_VERSION )
  142. {
  143. $this->InstallOrUpgrade($dbVersion);
  144. }
  145.  
  146. // ** Init all required classes
  147. $this->Cache->Init();
  148.  
  149. // ** Check for new plugin version (asynchronously)
  150. $pluginUpgradeCheck = $this->ContentController->ServePluginUpgradeCheck(false, true, false);
  151. if($pluginUpgradeCheck !== false)
  152. $this->EnqueueFooterContent($pluginUpgradeCheck);
  153. }
  154.  
  155. /// <summary>
  156. /// Called when the plugin is activated
  157. /// </summary>
  158. function OnPluginActivated()
  159. {
  160. // ** Install / Upgrade the plugin
  161. $dbVersion = $this->DataContext->GetInstalledPluginVersion();
  162. if($dbVersion !== CRANKY_ADS_PLUGIN_VERSION )
  163. {
  164. $this->InstallOrUpgrade($dbVersion);
  165. }
  166.  
  167. // ** Publish the Advertise Here page
  168. // TODO: Should we check / leave this until the blog is registered?
  169. $advertiseHerePage = array(
  170. 'ID' => $this->DataContext->GetAdvertiseHerePageId(),
  171. 'post_status' => 'publish',
  172. );
  173.  
  174. if( $advertiseHerePage['ID'] !== false && strlen($advertiseHerePage['ID']) > 0 )
  175. {
  176. wp_update_post( $advertiseHerePage );
  177. }
  178. }
  179.  
  180. /// <summary>
  181. /// Called when the plugin is deactivated
  182. /// </summary>
  183. function OnPluginDeactivated()
  184. {
  185. // ** Revert the Advertise Here page to draft
  186. $advertiseHerePage = array(
  187. 'ID' => $this->DataContext->GetAdvertiseHerePageId(),
  188. 'post_status' => 'draft',
  189. );
  190.  
  191. if( $advertiseHerePage['ID'] !== false && strlen($advertiseHerePage['ID']) > 0 )
  192. {
  193. wp_update_post( $advertiseHerePage );
  194. }
  195. }
  196.  
  197. /// <summary>
  198. /// Called when the plugin is uninstalled (deleted)
  199. /// </summary>
  200. /// <remarks>This is called explicitly by Uninstall.php</remarks>
  201. function OnPluginUninstalled()
  202. {
  203. $this->Uninstall(); // Note: The Wordpress auto-update function does not call OnPluginUninstalled() so no settings will be lost there.
  204. // This is only called when a user explicitly hits Delete on the Wordpress Plugins Page, in which case we DO want to delete all the settings.
  205. //exit(); // <- DEBUG: This ensure we don't actually proceed with the delete, which is useful when we're developing the plugin
  206. }
  207.  
  208. /// <summary>
  209. /// Setup the CranyAds Admin Menu
  210. /// </summary>
  211. function OnAddAdminMenuItem()
  212. {
  213. $settingsPage = add_options_page('CrankyAds Options', 'CrankyAds', 'manage_options', __FILE__, array(&$this->ContentController, 'ServeSettingsPage'));
  214. //add_action( "admin_head-$settingsPage", array(&$this, 'OnAdminMenuHead') ); // The admin_head action doesn't allow us to enqueue scripts or styles
  215. add_action( "admin_print_styles-$settingsPage", array(&$this, 'OnAdminMenuHead') );
  216. add_action( "admin_footer-$settingsPage", array(&$this, 'ServeFooter') );
  217.  
  218. $settingsPageMenu = add_menu_page('CrankyAds Settings', 'CrankyAds', 'manage_options', __FILE__, array(&$this->ContentController, 'ServeSettingsPage'), plugins_url('crankyads/images/menuicon.png'));
  219. add_action( "admin_print_styles-$settingsPageMenu", array(&$this, 'OnAdminMenuHead') );
  220. add_action( "admin_footer-$settingsPageMenu", array(&$this, 'ServeFooter') );
  221. }
  222.  
  223. /// <summary>
  224. /// Enqueue the admin menu CSS
  225. /// </summary>
  226. function OnAdminMenuHead()
  227. {
  228. $bodyContent = false;
  229. $this->ContentController->ServeHead(true,false,true,true,false,$bodyContent);
  230.  
  231. if( $bodyContent !== false )
  232. $this->EnqueueFooterContent($bodyContent);
  233. }
  234.  
  235. /// <summary>
  236. /// Setup the Wordpress > Plugin Links for the CranyAds Plugin
  237. /// </summary>
  238. function OnAddPluginLinks($links, $file)
  239. {
  240. // Add a link for CrankyAds only
  241. if ( stripos( $file, basename(__FILE__) ) !== false )
  242. {
  243. $settings_link = '<a href="'.CRANKY_ADS_SETTINGS_PAGE_URL_RELATIVE.'"';
  244. if( $this->DataContext->GetBlogGuid() === false )
  245. $settings_link .= ' style="color:orange;font-weight:bold">' . __('Start Here');
  246. else
  247. $settings_link .= '>' . __('Settings');
  248. $settings_link .= '</a>';
  249. array_unshift($links, $settings_link);
  250. }
  251. return $links;
  252. }
  253.  
  254. /// <summary>
  255. /// Setup the CrankyAds Widgets
  256. /// </summary>
  257. function OnRegisterWidgets()
  258. {
  259. if( !$this->DataContext->IsSetup() )
  260. return;
  261.  
  262. $allZones = $this->DataContext->GetAllZones();
  263.  
  264. foreach ($allZones as $zone)
  265. {
  266. $this->WidgetFactory->RegisterWidgetFor($zone->server_zone_id,$zone->name);
  267. }
  268. }
  269.  
  270. /// <summary>
  271. /// Setup the CrankyAds Short Codes
  272. /// </summary>
  273. function OnRegisterShortCodes()
  274. {
  275. // Advertise Here
  276. add_shortcode($this->WP_SHORT_CODE_ADVERTISE_HERE, array(&$this, 'ShortCodeAdvertiseHere') );
  277.  
  278. // Enqueue css and script only if shortcodes are present
  279. add_action('wp_enqueue_scripts', array(&$this, 'OnEnqueueShortcodeHead') );
  280. }
  281.  
  282. /// <summary>
  283. /// Enqueue the css and script header blocks if any shortcode is present
  284. /// </summary>
  285. function OnEnqueueShortcodeHead()
  286. {
  287. global $post;
  288. $bodyContent = false;
  289.  
  290. if( isset($post) && isset($post->post_content) &&
  291. strpos($post->post_content, "[".$this->WP_SHORT_CODE_ADVERTISE_HERE."]") !== false )
  292. {
  293. $this->ContentController->ServeHead(true,true,true,true,false,&$bodyContent);
  294.  
  295. if( $bodyContent !== false )
  296. $this->EnqueueFooterContent($bodyContent);
  297. }
  298. }
  299.  
  300. /// <summary>
  301. /// Handle push notifications from the server.
  302. /// </summary>
  303. /// <remarks>These notifications are used primarily to update plugin settings in line with changes to the server content.</remarks>
  304. function PushNotification()
  305. {
  306. // Don't let anyone mess with our output
  307. $errorReportingSave = error_reporting(0);
  308. if( CRANKY_ADS_DISABLE_ERROR_SUPPRESSION )
  309. error_reporting($errorReportingSave);
  310.  
  311. // ** Setup
  312. $allowedPushNotifications = array( "get-info", "clear-cache", "resync-zones" );
  313.  
  314. // ** Get the parameters
  315. $isAuthenticated = isset($_GET['auth']) && strlen($_GET['auth']) > 0 && $_GET['auth'] === $this->DataContext->GetBlogGuid() && $this->DataContext->GetBlogGuid() !== false && strlen($this->DataContext->GetBlogGuid()) > 0;
  316. $notification = $_GET['notification'];
  317. $values = $_GET['values'];
  318.  
  319. // * Further security checks
  320. // We don't want all notifications open for push
  321. // TODO: We should uncomment the code below at a later stage when we don't need easy debug access to the settings.
  322. // We are resonably safe in commenting out this code because it is quite secure (password protected) and 100% limited to the plugin and not the blog.
  323. //if( !CRANKY_ADS_DEBUG )
  324. //{
  325. //if( isset($notification) )
  326. //{
  327. //$checkNotification = strtolower($notification);
  328. //$isAuthenticated = $isAuthenticated & in_array( $checkNotification, $allowedPushNotifications );
  329. //}
  330. //}
  331.  
  332. // ** Handle the notification
  333. if( $isAuthenticated && isset( $notification ) && strlen($notification) > 0 )
  334. {
  335. // * Split $values
  336. if( isset( $values ) && strlen($values) > 0 )
  337. $values = explode(";",$values);
  338. else
  339. $values = array();
  340.  
  341. // * Make the call
  342. $this->ContentController->HandleCrankyAdsNotification( $notification, $values, true );
  343.  
  344. }
  345. else
  346. {
  347. echo("0");
  348. }
  349.  
  350. error_reporting($errorReportingSave);
  351.  
  352.  
  353. die(); // We've completed successfully so Wordpress shouldn't output anything else
  354. }
  355.  
  356. /// <summary>
  357. /// This function is used as the target for all proxy server content (via the Wordpress AJAX script).
  358. /// It serves it's content using ContentController
  359. /// </summary>
  360. function ServeRemoteContent()
  361. {
  362. // ** Serve the content
  363. $serverUrl = $_GET["serverurl"];
  364. if( isset($serverUrl) )
  365. {
  366. // Don't let anyone mess with our output
  367. $errorReportingSave = error_reporting(0);
  368. if( CRANKY_ADS_DISABLE_ERROR_SUPPRESSION )
  369. error_reporting($errorReportingSave);
  370.  
  371. // Note:
  372. // . For security reasons this page will serve content from the server /Content directory or /Plugin directory ONLY where content has been marked as public.
  373. // |-> We don't want users accessing plugin settings without being admins
  374. // |-> We don't want malicious users using this page as a proxy to hit just any page on the server (and slowing down this blog)
  375. // |-> We can't limit this call to admins only because this page needs to serve images and other content to users of the blog.
  376. if( !$this->ContentController->ServeRemoteContent($serverUrl, CrankyAdsHelper::GetFullRequestUri("serverurl"),0) )
  377. {
  378. echo "Error contacting the server"; // <- Serving at least something tends to keep colorbox happy
  379. }
  380.  
  381. error_reporting($errorReportingSave);
  382. }
  383. else
  384. {
  385. echo "Invalid URL";
  386. }
  387.  
  388. die(); // We've completed successfully so Wordpress shouldn't output anything else
  389. }
  390.  
  391. /// <summary>
  392. /// Call the server to check whether a plugin upgrade is required (because a new version is available).
  393. /// TODO (791): Review whether we want to continue this once CrankyAds is part of the Wordpress plugin list. We likely do since CrankyAds does a plugin version check whenever someone accesses the Settings page.
  394. /// </summary>
  395. function CheckPluginUpgrade()
  396. {
  397. // Don't let anyone mess with our output
  398. $errorReportingSave = error_reporting(0);
  399. if( CRANKY_ADS_DISABLE_ERROR_SUPPRESSION )
  400. error_reporting($errorReportingSave);
  401.  
  402. $this->ContentController->ServePluginUpgradeCheck(true,false,false);
  403.  
  404. error_reporting($errorReportingSave);
  405.  
  406. die(); // We've completed successfully so Wordpress shouldn't output anything else
  407. }
  408.  
  409. /// <summary>
  410. /// This function is used to server the contents of an Ad Zone asynchronously (via Wordpress Ajax)
  411. /// </summary>
  412. /// <remarks>
  413. /// . This method serves the Ad Zone through ContentController->ServeAdZoneAds(..)
  414. /// </remarks>
  415. function ServeAdZoneAsync()
  416. {
  417. // ** Get the parameters
  418.  
  419. // Zone Id
  420. $zoneId = $_GET["zoneId"];
  421. if( isset($zoneId) )
  422. $zoneId = intval($zoneId);
  423. else
  424. $zoneId = 0;
  425.  
  426. // Permutation
  427. $permutation = $_GET["permutation"];
  428. if( isset($permutation) )
  429. {
  430. $permutation = intval($permutation);
  431. if( $permutation === 0 ) // Failure
  432. $permutation = false;
  433. }
  434.  
  435. // Suppress Output
  436. $suppressOutput = isset($_GET["suppressoutput"]) && $_GET["suppressoutput"]=="1";
  437.  
  438. // ** Serve the zone
  439. if( $zoneId > 0 )
  440. {
  441. // Don't let anyone mess with our output
  442. $errorReportingSave = error_reporting(0);
  443. if( CRANKY_ADS_DISABLE_ERROR_SUPPRESSION )
  444. error_reporting($errorReportingSave);
  445.  
  446. $isUserSensitive = false;
  447. if( CRANKY_ADS_DEBUG && isset($_GET['debug']) )
  448. {
  449. $isUserSensitive = true;
  450. }
  451.  
  452.  
  453. // Note:
  454. // . There are no security issues here since this function will ONLY server AdZones which are inherently public content.
  455. if( !$this->ContentController->ServeAdZoneAds($zoneId,$isUserSensitive,$permutation,$suppressOutput) )
  456. {
  457. echo "<!--Error contacting the Cranky Ads Server-->";
  458. }
  459.  
  460. error_reporting($errorReportingSave);
  461. }
  462. else
  463. {
  464. echo "Invalid Zone Id";
  465. }
  466.  
  467. die(); // We've completed successfully so Wordpress shouldn't output anything else
  468. }
  469.  
  470. /// <summary>
  471. /// This function is used to serve the contents of the Advertise Here page asynchronously (via Wordpress Ajax)
  472. /// </summary>
  473. /// <remarks>
  474. /// . This method serves the Advertise Here page through ContentController->ServeAdvertiseHere(..)
  475. /// </remarks>
  476. function ServeAdvertiseHereAsync()
  477. {
  478. // Don't let anyone mess with our output
  479. $errorReportingSave = error_reporting(0);
  480. if( CRANKY_ADS_DISABLE_ERROR_SUPPRESSION )
  481. error_reporting($errorReportingSave);
  482.  
  483. // ** Setup
  484. $isUserSensitive = false;
  485. $instructions = isset($_POST['instructions']) ? $_POST['instructions'] : (isset($_GET['instructions']) ? $_GET['instructions'] : false);
  486. $suppressOutput = isset($_GET["suppressoutput"]) && $_GET["suppressoutput"]=="1";
  487.  
  488. // Special case - check for PayPal response and add "thankyou" instruction
  489. if(isset($_POST['txn_type']) && isset($_POST['auth']) || isset($_GET['txn_type']) && isset($_GET['auth']) )
  490. {
  491. if( $instructions === false )
  492. $instructions = "";
  493. else
  494. $instructions .= ";";
  495.  
  496. $instructions .= "thankyou";
  497. }
  498.  
  499. // ** Serve the Advertise Here content
  500. if( !$this->ContentController->ServeAdvertiseHere($isUserSensitive, $instructions, $suppressOutput) )
  501. {
  502. echo "<!--Error contacting the Cranky Ads Server-->";
  503. }
  504.  
  505. error_reporting($errorReportingSave);
  506.  
  507.  
  508. die(); // We've completed successfully so Wordpress shouldn't output anything else
  509. }
  510.  
  511. /// <summary>
  512. /// This function is used to serve the <head> block asynchronously (via Wordpress Ajax)
  513. /// </summary>
  514. /// <remarks>
  515. /// . This method serves the <head> block through ContentController->ServeHead(..)
  516. /// </remarks>
  517. function ServeHeadAsync()
  518. {
  519. // Don't let anyone mess with our output
  520. $errorReportingSave = error_reporting(0);
  521. if( CRANKY_ADS_DISABLE_ERROR_SUPPRESSION )
  522. error_reporting($errorReportingSave);
  523.  
  524. // Suppress Output
  525. $suppressOutput = $_GET["suppressoutput"]=="1";
  526.  
  527. if( !$this->ContentController->ServeHead(false,false,false,false,$suppressOutput) )
  528. {
  529. echo "<!--Error contacting the Cranky Ads Server-->";
  530. }
  531.  
  532. error_reporting($errorReportingSave);
  533.  
  534.  
  535. die(); // We've completed successfully so Wordpress shouldn't output anything else
  536. }
  537.  
  538. /// <summary>
  539. /// Serve any footer content
  540. /// </summary>
  541. /// <remarks>
  542. /// . To serve additional footer content use $this->EnqueueFooterContent(..)
  543. /// </remarks>
  544. function ServeFooter()
  545. {
  546. if( $this->FooterContent !== false )
  547. echo $this->FooterContent;
  548. }
  549.  
  550. /// <summary>
  551. /// Returns the Cranky Ads Advertise Here HTML as part of a ShortCode request
  552. /// </summary>
  553. function ShortCodeAdvertiseHere( $dicAttrs, $content )
  554. {
  555. // TODO: We can add attributes later to determine how the Advertise Here page should be displayed
  556.  
  557. // ** Setup
  558. $instructions = isset($_POST['instructions']) ? $_POST['instructions'] : (isset($_GET['instructions']) ? $_GET['instructions'] : false);
  559.  
  560. // Special case - check for PayPal response and add "thankyou" instruction
  561. if(isset($_POST['txn_type']) && isset($_POST['auth']) || isset($_GET['txn_type']) && isset($_GET['auth']) )
  562. {
  563. if( $instructions === false )
  564. $instructions = "";
  565. else
  566. $instructions .= ";";
  567.  
  568. $instructions .= "thankyou";
  569. }
  570.  
  571. // ** Serve the Advertise Here content
  572. return $this->ContentController->ServeAdvertiseHere(true,$instructions,true);
  573. }
  574.  
  575. /// <summary>
  576. /// Performs a one time wordpress setup to run the plugin
  577. /// </summary>
  578. /// <remarks>
  579. /// . Install should be run once when the plugin is first activated and once every time a new version is installed.
  580. /// . Install handles cases where a previous installation may only have been partially complete or not performed at all
  581. /// </remarks>
  582. function InstallOrUpgrade( $strPreviousVersion = false )
  583. {
  584. $vMajor = 0;
  585. $vMinor = 0;
  586. $vRevision = 0;
  587.  
  588. if( $strPreviousVersion !== false )
  589. {
  590. $version = explode(".",$strPreviousVersion);
  591.  
  592. $vMajor = intval($version[0]);
  593. $vMinor = intval($version[1]);
  594. $vRevision = intval($version[2]);
  595. }
  596.  
  597. // ** Perform install on all relevant subcomponents
  598. $this->DataContext->InstallOrUpgrade($vMajor, $vMinor, $vRevision);
  599. $this->Cache->InstallOrUpgrade($vMajor, $vMinor, $vRevision);
  600.  
  601. // ** Perform Plugin Installation
  602.  
  603. // * 1.1.0
  604. if( $vMajor < 1 || $vMajor == 1 && $vMinor < 1 )
  605. {
  606. // Create the Advertise Here page
  607. // Note: for reference on parameters see http://codex.wordpress.org/Function_Reference/wp_insert_post
  608. $advertiseInsertError = false;
  609. $advertiseHerePage = array(
  610. 'comment_status' => 'closed',
  611. 'ping_status' => 'closed',
  612. 'post_content' => '['.$this->WP_SHORT_CODE_ADVERTISE_HERE.']',
  613. 'post_name' => 'Advertise Here',
  614. 'post_status' => 'publish',
  615. 'post_title' => 'Advertise Here',
  616. 'post_type' => 'page'
  617. );
  618.  
  619. $advertisePageId = wp_insert_post( $advertiseHerePage, $advertiseInsertError );
  620. if($advertiseInsertError === false)
  621. $this->DataContext->SetAdvertiseHerePageId($advertisePageId);
  622. else
  623. $this->DataContext->SetAdvertiseHerePageId(false);
  624. }
  625.  
  626. // ** Save the plugin version (for future upgrades)
  627. $this->DataContext->SetInstalledPluginVersion(CRANKY_ADS_PLUGIN_VERSION);
  628.  
  629. }
  630.  
  631. /// <summary>
  632. /// Clears all the one time Wordpress setup performed by the plugin during InstallOrUpgrade()
  633. /// </summary>
  634. /// <remarks>
  635. /// . Uninstalling is performed in the reverse order to the installation
  636. /// . Uninstalling handles cases where the installation may only be partially complete or not performed at all
  637. /// </remarks>
  638. function Uninstall()
  639. {
  640. // ** Perform Plugin Un-Installation
  641.  
  642. // * 1.1.0
  643. {
  644. // Disable the Advertise Here page
  645. // Note: for reference on parameters see http://codex.wordpress.org/Function_Reference/wp_insert_post
  646. $advertiseHerePage = array(
  647. 'ID' => $this->DataContext->GetAdvertiseHerePageId(),
  648. 'post_status' => 'draft',
  649. );
  650.  
  651. if( $advertiseHerePage['ID'] !== false && strlen($advertiseHerePage['ID']) > 0 )
  652. {
  653. //wp_update_post( $advertiseHerePage );
  654. wp_delete_post( $advertiseHerePage['ID'] ); // This will move the page into trash allowing users to recover it if desired
  655. }
  656.  
  657. // Clear the advertise here id
  658. $this->DataContext->SetAdvertiseHerePageId(false);
  659.  
  660. }
  661.  
  662. // ** Perform Un-Installation on all relevant subcomponents (in reverse installation order)
  663. $this->Cache->Uninstall();
  664. $this->DataContext->Uninstall();
  665.  
  666. }
  667.  
  668. /// <summary>
  669. /// Enqueue additional content that will be output as part of the wp_footer action
  670. /// </summary>
  671. function EnqueueFooterContent( $strContent )
  672. {
  673. if( $this->FooterContent === false )
  674. $this->FooterContent = "";
  675.  
  676. $this->FooterContent .= $strContent;
  677. }
  678.  
  679. } // end class
  680.  
  681.  
  682.  
  683.  
  684. // ** Create a new Cranky Ads Plugin instance
  685. $CrankyAdsPlugin = new CrankyAdsPlugin();
  686.  
  687.  
  688. } // end if
  689.  
  690.  
  691.  
  692. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement