PluginProbe
ONTRApages / trunk
ONTRApages vtrunk
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 trunk, at OPCoreFunctions.php

156 lines 4.4 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 , $extra_get_vars = "")
19 {
20 $response = self::getResponse( $url . $extra_get_vars, 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 . $extra_get_vars, array("X-Ontrapages-Plugin: no_cache_attempt_2") );
31 $content = wp_remote_retrieve_body( $response );
32 }
33 }
34
35 if ($content && !$extra_get_vars)
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 'timeout' => 60
51 )
52 );
53 }
54
55
56 /**
57 * Check db options to see if there are valid api creds.
58 *
59 * @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.
60 * @param $emptyObject If there is an empty object check it to see if it's an auth error. If so let the user know.
61 *
62 * @return Echos a warning message if not and returns an error code.
63 *
64 * Error Code 0 - All good in the hood
65 * Error Code 1 - API Creds are wrong
66 * Error Code 2 - Missing API Creds
67 * Error Code 3 - No ONTRApages available
68 */
69 public static function checkAPICreds( $returnType=false, $emptyObject=false )
70 {
71 $appid = get_option( 'opAppID' );
72 $validCreds = get_option( 'opValidCreds' );
73
74 if ( $appid === false )
75 {
76 $code = 2;
77 }
78 else if ( $validCreds !== false && $validCreds == '0' )
79 {
80 $code = 1;
81 }
82 else if ( $validCreds !== false && $validCreds == '1' && $emptyObject !== false )
83 {
84 if ( $emptyObject == 'auth-error' )
85 {
86 $code = 1;
87 }
88 else
89 {
90 $code = 3;
91 }
92 }
93 else
94 {
95 $code = 0;
96 }
97
98 $response = self::buildResponse( $code );
99
100 if ( $returnType === 'echo' || $returnType === '' )
101 {
102 echo $response['message'];
103 }
104 else if ( $returnType === 'code' )
105 {
106 return $response['code'];
107 }
108 else
109 {
110 return $response;
111 }
112 }
113
114
115 /**
116 * Takes the code that it gets passed and builds a response with the code and message
117 * @param integer $code A response code
118 *
119 * @return array A complete response with a code and message
120 */
121 public static function buildResponse( $code )
122 {
123 switch( $code )
124 {
125 case 0:
126 $message = 'Success';
127 break;
128
129 case 1:
130 $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>';
131 break;
132
133 case 2:
134 $message = '<div id="message" class="error is-dismissible">
135 <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>
136 </div>';
137 break;
138
139 case 3:
140 $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>';
141 break;
142
143 default:
144 $message = '';
145 break;
146 }
147
148 $response = array(
149 'code' => $code,
150 'message' => $message
151 );
152
153 return $response;
154 }
155 }
156