| 1 |
<?php |
| 2 |
// Manages the objects that get used in the BE settings of each ONTRApages or ONTRAform. |
| 3 |
class OPObjects |
| 4 |
{ |
| 5 |
|
| 6 |
// Basic function that starts the process of getting and formatting the ONTRApage or ONTRAform objects. Returns the formatted objects ready to use. |
| 7 |
public static function getOPObjects( $type ) |
| 8 |
{ |
| 9 |
if ( get_option('opValidCreds') != 1 ) |
| 10 |
{ |
| 11 |
return 'auth-error'; |
| 12 |
} |
| 13 |
|
| 14 |
$opObjects = OPObjects::createObject( $type ); |
| 15 |
|
| 16 |
return $opObjects; |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
// Gets a single object. Used for ONTRAforms. Returns the object. |
| 21 |
public static function getOPObject( $type, $extraVars, $id ) |
| 22 |
{ |
| 23 |
$opObject = OPObjects::requestOPObjects( $type, $extraVars, $id ); |
| 24 |
|
| 25 |
return $opObject; |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
// Works together with the requestObjects function to compile a complete object. It makes one call to requestObjects and if there are 50 object items then it makes more calls till it collects all the items. Returns a complete and formatted object. |
| 30 |
private static function createObject( $type ) |
| 31 |
{ |
| 32 |
$number = 0; |
| 33 |
$newOpObjects = array(); |
| 34 |
$opObjects = array(); |
| 35 |
|
| 36 |
$objectSet = OPObjects::requestOPObjects( $type ); |
| 37 |
|
| 38 |
if ($objectSet == "timeout-error") |
| 39 |
{ |
| 40 |
return "timeout-error"; |
| 41 |
} |
| 42 |
|
| 43 |
if (!is_string($objectSet)) |
| 44 |
{ |
| 45 |
return "error in response"; |
| 46 |
} |
| 47 |
|
| 48 |
$objectSet = json_decode( $objectSet, true); |
| 49 |
$objectSet = $objectSet["data"]; |
| 50 |
|
| 51 |
//If the number of records is 50, let's see if there are more |
| 52 |
if ( count( $objectSet ) === 50 ) |
| 53 |
{ |
| 54 |
while ( $number > -1 ) |
| 55 |
{ |
| 56 |
if ( $number !== 0 ) |
| 57 |
{ |
| 58 |
$extraVars = '&start=' . $number; |
| 59 |
$objectSet = json_decode( OPObjects::requestOPObjects( $type, $extraVars ), true ); |
| 60 |
$objectSet = $objectSet["data"]; |
| 61 |
} |
| 62 |
|
| 63 |
foreach ($objectSet as $setData) |
| 64 |
{ |
| 65 |
array_push($newOpObjects, $setData); |
| 66 |
} |
| 67 |
|
| 68 |
if ( count($objectSet) === 50 ) |
| 69 |
{ |
| 70 |
$number = $number + 50; |
| 71 |
} |
| 72 |
else |
| 73 |
{ |
| 74 |
$number = -1; |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
else |
| 79 |
{ |
| 80 |
$newOpObjects = $objectSet; |
| 81 |
} |
| 82 |
|
| 83 |
return $newOpObjects; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
// Calls home to get all the ONTRApage objects using either the ontrapages or ontraport api. Then it works with modifyOntrapagesObjects to format the object correctly depending upon the API used. Returns a formatted object. |
| 88 |
private static function requestOPObjects( $type, $extraVars=false, $formId=false ) |
| 89 |
{ |
| 90 |
$appid = get_option( 'opAppID' ); |
| 91 |
$key = get_option( 'opAPIKey' ); |
| 92 |
|
| 93 |
switch ( $type ) |
| 94 |
{ |
| 95 |
case 'form': |
| 96 |
$request = OPAPI . 'object?objectID=122&id=' . $formId; |
| 97 |
break; |
| 98 |
|
| 99 |
case 'forms': |
| 100 |
$request = OPAPI . 'objects?objectID=122&performAll=true&sort=formname&sortDir=asc&condition=%5B%7B%22field%22%3A%7B%22field%22%3A%22type%22%7D%2C%22op%22%3A%22%3D%22%2C%22value%22%3A%7B%22value%22%3A11%7D%7D%5D&searchNotes=true&listFields=form_id%2Cformname%2Cfillouts' . $extraVars; |
| 101 |
break; |
| 102 |
|
| 103 |
case 'pages': |
| 104 |
|
| 105 |
// "`design_type` = '4' AND (`domain` != '' OR `resource` != '')" |
| 106 |
// Available wordpress pages are pages that have been published in app before, which means `resource` will be set even after unhosting blanks out `domain` |
| 107 |
// Edge case: If page is flagged as abusive on first publish, `resource` will be empty, but `domain` will be set |
| 108 |
// `domain` != '' OR covers this case |
| 109 |
$condition = array( |
| 110 |
array("field" => |
| 111 |
array("field" => "design_type"), |
| 112 |
"op" => "IN", |
| 113 |
"value" => array("list" => array(array("value" => 3,), array("value" => 4))) |
| 114 |
), |
| 115 |
"AND", |
| 116 |
array( |
| 117 |
array("field"=> |
| 118 |
array( "field"=>"domain"), |
| 119 |
"op"=> "!=", |
| 120 |
"value"=> |
| 121 |
array( "value"=>"") |
| 122 |
), |
| 123 |
"OR", |
| 124 |
array("field"=> |
| 125 |
array("field"=>"resource"), |
| 126 |
"op"=> "!=", |
| 127 |
"value"=> |
| 128 |
array("value"=> "") |
| 129 |
) |
| 130 |
) |
| 131 |
); |
| 132 |
|
| 133 |
$condition = json_encode($condition); |
| 134 |
$condition = urlencode($condition); |
| 135 |
$request = OPAPI . 'objects?objectID=20&performAll=true&sort=name&sortDir=asc&condition=' . $condition . '&searchNotes=true&listFields=id%2Cname%2Cdomain%2Cvisits_0%2Cvisits_1%2Cvisits_2%2Cvisits_3%2Ca_convert%2Cb_convert%2Cc_convert%2Cd_convert' . $extraVars; |
| 136 |
$opObjects = OPCoreFunctions::apiRequest( $request, $appid, $key ); |
| 137 |
break; |
| 138 |
|
| 139 |
default: |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
$opObjects = OPCoreFunctions::apiRequest( $request, $appid, $key ); |
| 144 |
|
| 145 |
if (!$opObjects) |
| 146 |
{ |
| 147 |
return "timeout-error"; |
| 148 |
} |
| 149 |
|
| 150 |
$opObjects = self::modifyOntrapagesObjects( $opObjects ); |
| 151 |
|
| 152 |
if ( isset($opObjects) && |
| 153 |
( $opObjects === 'Your App ID and API Key do not authenticate.' || $opObjects === 'Not Authorized' ) ) |
| 154 |
{ |
| 155 |
return 'auth-error'; |
| 156 |
} |
| 157 |
else |
| 158 |
{ |
| 159 |
return $opObjects; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
// Modify objects from the ONTRApages API to make the data from either API match each other and additionally does the math for the conversion stat. Returns a properly formatted object. |
| 165 |
protected static function modifyOntrapagesObjects( $opObjects ) |
| 166 |
{ |
| 167 |
$opObjects = json_decode( $opObjects ); |
| 168 |
$ONTRApageObjects = $opObjects->data; |
| 169 |
|
| 170 |
if ( is_array($ONTRApageObjects) ) |
| 171 |
{ |
| 172 |
$modifiedObject = array(); |
| 173 |
|
| 174 |
foreach ( $ONTRApageObjects as $opObject => $obj ) |
| 175 |
{ |
| 176 |
if ( get_option('opApiSource') === 'ontrapages' ) |
| 177 |
{ |
| 178 |
if ( $obj->a_sent === null ) |
| 179 |
$obj->a_sent = 0; |
| 180 |
if ( $obj->b_sent === null ) |
| 181 |
$obj->b_sent = 0; |
| 182 |
if ( $obj->c_sent === null ) |
| 183 |
$obj->c_sent = 0; |
| 184 |
if ( $obj->d_sent === null ) |
| 185 |
$obj->d_sent = 0; |
| 186 |
|
| 187 |
$obj->visits_0 = $obj->a_sent; |
| 188 |
$obj->visits_1 = $obj->b_sent; |
| 189 |
$obj->visits_2 = $obj->c_sent; |
| 190 |
$obj->visits_3 = $obj->d_sent; |
| 191 |
|
| 192 |
unset($obj->a_sent); |
| 193 |
unset($obj->b_sent); |
| 194 |
unset($obj->c_sent); |
| 195 |
unset($obj->d_sent); |
| 196 |
unset($obj->resource); |
| 197 |
|
| 198 |
if ( !isset($obj->a_convert) ) |
| 199 |
$obj->a_convert = '0'; |
| 200 |
if ( !isset($obj->b_convert) ) |
| 201 |
$obj->b_convert = '0'; |
| 202 |
if ( !isset($obj->c_convert) ) |
| 203 |
$obj->c_convert = '0'; |
| 204 |
if ( !isset($obj->d_convert) ) |
| 205 |
$obj->d_convert = '0'; |
| 206 |
} |
| 207 |
|
| 208 |
if ( $obj->a_convert != 0 && $obj->visits_0 != 0 ) |
| 209 |
$obj->a_convert = round( ( $obj->a_convert / $obj->visits_0 ) * 100, 2); |
| 210 |
if ( $obj->b_convert != 0 && $obj->visits_1 != 0 ) |
| 211 |
$obj->b_convert = round( ( $obj->b_convert / $obj->visits_1 ) * 100, 2); |
| 212 |
if ( $obj->c_convert != 0 && $obj->visits_2 != 0 ) |
| 213 |
$obj->c_convert = round( ( $obj->c_convert / $obj->visits_2 ) * 100, 2); |
| 214 |
if ( $obj->d_convert != 0 && $obj->visits_3 != 0 ) |
| 215 |
$obj->d_convert = round( ( $obj->d_convert / $obj->visits_3 ) * 100, 2); |
| 216 |
|
| 217 |
array_push( $modifiedObject, $obj ); |
| 218 |
} |
| 219 |
|
| 220 |
$opObjects = array( |
| 221 |
'code' => 0, |
| 222 |
'data' => $modifiedObject ); |
| 223 |
} |
| 224 |
|
| 225 |
$opObjects = json_encode( $opObjects ); |
| 226 |
|
| 227 |
return $opObjects; |
| 228 |
} |
| 229 |
|
| 230 |
} |
| 231 |
|