PluginProbe
ONTRApages / 1.2.11
ONTRApages v1.2.11
trunk 1.1.7 1.1.8 1.2 1.2.10 1.2.11 1.2.12
ontrapages / OPCoreFunctions.php

OPCoreFunctions.php in ONTRApages 1.2.11, at OPCoreFunctions.php

155 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Manages the API requests and any other core functionality that gets used accross multiple classes.
3 class OPCoreFunctions
4 {
5 // Performs an API request to api.ontrapages.com or api.ontraport.com/1/. Returns the API response.
6 public static function apiRequest( $requestUrl, $appid, $key, $httpCodeOnly=false )
7 {
8 $response = self::getResponse( $requestUrl, array("Api-Appid" => $appid, "Api-Key" => $key) );
9 if ( $httpCodeOnly !== false )
10 {
11 return wp_remote_retrieve_response_code( $response );
12 }
13 return wp_remote_retrieve_body( $response );
14 }
15
16
17 // Get's the contents of the URL. Returns the HTML.
18 public static function getURLContent( $url )
19 {
20 $response = self::getResponse( $url, array("X-Ontrapages-Plugin: $url"));
21 $content = wp_remote_retrieve_body( $response );
22 if (!$content)
23 {
24 // WP errored out trying to receive the HTML, lets grab a copy from the last known successful time
25 $content = get_transient("ontrapages_".$url);
26
27 if (!$content)
28 {
29 // no cached content either. Try one more time
30 $response = self::getResponse( $url, array("X-Ontrapages-Plugin: no_cache_attempt_2") );
31 $content = wp_remote_retrieve_body( $response );
32 }
33 }
34
35 if ($content)
36 {
37 // got some content lets cache it for a day so we can use in the case something goes wrong fetching it from the server
38 set_transient("ontrapages_".$url, $content, 86400);
39 }
40
41 return $content;
42 }
43
44 public static function getResponse( $url , $headers = array("X-Ontrapages-Plugin: 1") )
45 {
46 return wp_remote_get( $url, array(
47 'blocking' => true,
48 'headers' => $headers,
49 'sslverify' => false
50 )
51 );
52 }
53
54
55 /**
56 * Check db options to see if there are valid api creds.
57 *
58 * @param $returnType You can pass 'echo' or 'code' as the return type. If echo then it will echo the response message. If code it will just return the response code. If nothing then it will return the entire response.
59 * @param $emptyObject If there is an empty object check it to see if it's an auth error. If so let the user know.
60 *
61 * @return Echos a warning message if not and returns an error code.
62 *
63 * Error Code 0 - All good in the hood
64 * Error Code 1 - API Creds are wrong
65 * Error Code 2 - Missing API Creds
66 * Error Code 3 - No ONTRApages available
67 */
68 public static function checkAPICreds( $returnType=false, $emptyObject=false )
69 {
70 $appid = get_option( 'opAppID' );
71 $validCreds = get_option( 'opValidCreds' );
72
73 if ( $appid === false )
74 {
75 $code = 2;
76 }
77 else if ( $validCreds !== false && $validCreds == '0' )
78 {
79 $code = 1;
80 }
81 else if ( $validCreds !== false && $validCreds == '1' && $emptyObject !== false )
82 {
83 if ( $emptyObject == 'auth-error' )
84 {
85 $code = 1;
86 }
87 else
88 {
89 $code = 3;
90 }
91 }
92 else
93 {
94 $code = 0;
95 }
96
97 $response = self::buildResponse( $code );
98
99 if ( $returnType === 'echo' || $returnType === '' )
100 {
101 echo $response['message'];
102 }
103 else if ( $returnType === 'code' )
104 {
105 return $response['code'];
106 }
107 else
108 {
109 return $response;
110 }
111 }
112
113
114 /**
115 * Takes the code that it gets passed and builds a response with the code and message
116 * @param integer $code A response code
117 *
118 * @return array A complete response with a code and message
119 */
120 public static function buildResponse( $code )
121 {
122 switch( $code )
123 {
124 case 0:
125 $message = 'Success';
126 break;
127
128 case 1:
129 $message = '<div class="op-error-message">It looks like your API credentials are incorrect. Your ONTRApages will not display properly until you fix this. Please <a href="edit.php?post_type=ontrapage&page=opsettings">click here</a> to remedy that & then try again.</div>';
130 break;
131
132 case 2:
133 $message = '<div id="message" class="error is-dismissible">
134 <p>ONTRApages Warning - It looks like you don\'t have any API credentials setup just yet. <a href="edit.php?post_type=ontrapage&page=opsettings">Click here</a> to remedy that & then try again.</p>
135 </div>';
136 break;
137
138 case 3:
139 $message = '<div class="op-error-message">Unfortunately it appears that you don\'t have any ONTRApages available. Create one by logging into your <a href="https://ontrapages.com" target="_blank">ONTRApages account</a> and then return when it has been saved!</div>';
140 break;
141
142 default:
143 $message = '';
144 break;
145 }
146
147 $response = array(
148 'code' => $code,
149 'message' => $message
150 );
151
152 return $response;
153 }
154 }
155