Plugin Directory

Changeset 1460432


Ignore:
Timestamp:
07/25/2016 08:38:37 PM (10 years ago)
Author:
AmitDeyUS
Message:

Adding way to define site_url and home_url via code and not in wp-config.php.
Autoprefix of URL also added on URLs that uses the MPP address aside from site_url

Location:
marketpowerwp/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • marketpowerwp/trunk/leplugin.php

    r1456506 r1460432  
    44 * Plugin Name: Multisoft MarketPowerPRO Tools
    55 * Description: Integration of Market Power Pro to wordpress
    6  * Version: 2.1
     6 * Version: 2.1.1
    77 * Author: Rodine Mark Paul L. Villar, Dexter John Campos
    88 * Author URI: mailto:dean.villar@gmail.com?subject= Multisoft MPP Tools
    99 */
    10 define('MPP_DEBUG', true);
     10define('MPP_DEBUG', false || WP_DEBUG);
    1111
    1212if (!function_exists('is_plugin_active')) {
  • marketpowerwp/trunk/readme.txt

    r1456481 r1460432  
    1 === MarketPowerWP ===
     1=== MarketPowerWP ===
    22Contributors: dean.villar@gmail.com
    33Tags: marketpowerpro, multisoft
    44Requires at least: 3.4+
    5 Stable tag: 2.1
     5Stable tag: 2.1.1
    66License: Commercial
    77
     
    9090== Installation ==
    9191
    92 1. Extract the zip file and just drop the contents in the wp-content/plugins/ directory of your WordPress installation and then activate the Plugin from Plugins page.
     921. Add
    9393
    94 2. Login to your administrator page yourwordpressblog.com/wp-admin.
     942. Extract the zip file and just drop the contents in the wp-content/plugins/ directory of your WordPress installation and then activate the Plugin from Plugins page.
    9595
    96 3. Go to MarketPowerPRO.
     963. Login to your administrator page yourwordpressblog.com/wp-admin.
    9797
    98 4. Set your Base MPPE Web address, where your MarketPowerPRO system is located.
     984. Go to MarketPowerPRO.
    9999
    100 5. Contact a MarketPowerPRO technical assistant to know your applicationID and set it to the applicationID field.
     1005. Set your Base MPPE Web address, where your MarketPowerPRO system is located.
    101101
    102 6. Check "Enable auto-prefix replicationSiteName to URLs" option
     1026. Contact a MarketPowerPRO technical assistant to know your applicationID and set it to the applicationID field.
    103103
    104 7. Go to Settings > Permalinks then set your URL Structure to Post Name then click Save Changes
     1047. Check "Enable auto-prefix replicationSiteName to URLs" option
     105
     1068. Go to Settings > Permalinks then set your URL Structure to Post Name then click Save Changes
    105107
    106108==Readme Generator==
  • marketpowerwp/trunk/src/LePlugin/Core/Utils.php

    r1445352 r1460432  
    77/**
    88 * @author Dexter John R. Campos <dexterjohncampos@gmail.com>
    9   @copyright Les Coders
     9 * @copyright Les Coders
    1010 * Utility class that contains common methods that is unclassified as of the moment.
    1111 */
    12 class Utils {
     12class Utils
     13{
    1314
    1415    private static $encryption_key = 'kQ@DotLg!isQp#9&UHX7*&%AvHvG6&';
    1516
    16     public static function encrypt($value, $key = null) {
     17    public static function encrypt($value, $key = null)
     18    {
    1719        if ($key === null) {
    1820            $key = self::$encryption_key;
    1921        }
    2022        return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $value, MCRYPT_MODE_CBC,
    21                         md5(md5($key))));
    22     }
    23 
    24     public static function decrypt($enc_value, $key = null) {
     23            md5(md5($key))));
     24    }
     25
     26    public static function decrypt($enc_value, $key = null)
     27    {
    2528        if ($key === null) {
    2629            $key = self::$encryption_key;
    2730        }
    2831        return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($enc_value),
    29                         MCRYPT_MODE_CBC, md5(md5($key))), "\0");
    30     }
    31 
    32     public static function getMainDomain($host_only = false) {
     32            MCRYPT_MODE_CBC, md5(md5($key))), "\0");
     33    }
     34
     35    public static function getMainDomain($host_only = false)
     36    {
    3337        $url = parse_url(site_url());
    3438        $domain = explode('.', $url['host']);
     
    4246    }
    4347
    44     public static function getSubDomain($subdomain, $host_only = false) {
    45         $url = parse_url(site_url());
    46         $domain = explode('.', $url['host']);
    47         if (count($domain) < 2) {
    48             return false;
    49         }
    50         if ($host_only) {
    51             return $subdomain . '.' . $domain[count($domain) - 2] . '.' . $domain[count($domain) - 1];
    52         }
    53         return $url['scheme'] . '://' . $subdomain . '.' . $domain[count($domain) - 2] . '.' . $domain[count($domain) - 1];
    54     }
    55 
    56     public static function recurseCopy($src, $dst) {
     48    public static function get_domain($domain, $debug = false)
     49    {
     50        $original = $domain = strtolower($domain);
     51        if (filter_var($domain, FILTER_VALIDATE_IP)) {
     52            return $domain;
     53        }
     54        $arr = array_slice(array_filter(explode('.', $domain, 4), function ($value) {
     55            return $value !== 'www';
     56        }), 0); //rebuild array indexes
     57        if (count($arr) > 2) {
     58            $count = count($arr);
     59            $_sub = explode('.', $count === 4 ? $arr[3] : $arr[2]);
     60            if (count($_sub) === 2) // two level TLD
     61            {
     62                $removed = array_shift($arr);
     63                if ($count === 4) // got a subdomain acting as a domain
     64                {
     65                    $removed = array_shift($arr);
     66                }
     67            } elseif (count($_sub) === 1) // one level TLD
     68            {
     69                $removed = array_shift($arr); //remove the subdomain
     70                if (strlen($_sub[0]) === 2 && $count === 3) // TLD domain must be 2 letters
     71                {
     72                    array_unshift($arr, $removed);
     73                } else {
     74                    // non country TLD according to IANA
     75                    $tlds = array(
     76                        'aero',
     77                        'arpa',
     78                        'asia',
     79                        'biz',
     80                        'cat',
     81                        'com',
     82                        'coop',
     83                        'edu',
     84                        'gov',
     85                        'info',
     86                        'jobs',
     87                        'mil',
     88                        'mobi',
     89                        'museum',
     90                        'name',
     91                        'net',
     92                        'org',
     93                        'post',
     94                        'pro',
     95                        'tel',
     96                        'travel',
     97                        'xxx',
     98                    );
     99                    if (count($arr) > 2 && in_array($_sub[0], $tlds) !== false) //special TLD don't have a country
     100                    {
     101                        array_shift($arr);
     102                    }
     103                }
     104            } else // more than 3 levels, something is wrong
     105            {
     106                for ($i = count($_sub); $i > 1; $i--) {
     107                    $removed = array_shift($arr);
     108                }
     109            }
     110        } elseif (count($arr) === 2) {
     111            $arr0 = array_shift($arr);
     112            if (strpos(join('.', $arr), '.') === false
     113                && in_array($arr[0], array('localhost', 'test', 'invalid')) === false
     114            ) // not a reserved domain
     115            {
     116                // seems invalid domain, restore it
     117                array_unshift($arr, $arr0);
     118            }
     119        }
     120        return join('.', $arr);
     121    }
     122
     123    public static function str_replace_first($search, $replace, $subject)
     124    {
     125        $pos = strpos($subject, $search);
     126        if ($pos !== false) {
     127            return substr_replace($subject, $replace, $pos, strlen($search));
     128        }
     129        return $subject;
     130    }
     131
     132    public static function recurseCopy($src, $dst)
     133    {
    57134        $dir = opendir($src);
    58135        @mkdir($dst);
     
    69146    }
    70147
    71     public static function utc_date($format, $time = null) {
     148    public static function utc_date($format, $time = null)
     149    {
    72150        return self::tz_date('UTC', $format, $time);
    73151    }
    74152
    75     public static function tz_date($tz, $format, $time = null) {
     153    public static function tz_date($tz, $format, $time = null)
     154    {
    76155        $old_tz = date_default_timezone_get();
    77156        date_default_timezone_set($tz);
     
    86165    }
    87166
    88     public static function utc_strtotime($str, $time = null) {
     167    public static function utc_strtotime($str, $time = null)
     168    {
    89169        return self::tz_strtotime('UTC', $str, $time);
    90170    }
    91171
    92     public static function tz_strtotime($tz, $str, $time = null) {
     172    public static function tz_strtotime($tz, $str, $time = null)
     173    {
    93174        $old_tz = date_default_timezone_get();
    94175        date_default_timezone_set($tz);
     
    103184    }
    104185
    105     public static function parseCustomFields($custom) {
     186    public static function parseCustomFields($custom)
     187    {
    106188        $returnUrlParts = parse_url("?" . $custom);
    107189        $params = [];
     
    110192    }
    111193
    112     public static function buildNotice($message, $type = 'updated', $inline = true) {
     194    public static function buildNotice($message, $type = 'updated', $inline = true)
     195    {
    113196
    114197        $notice = '<div class="' . $type . ' notice is-dismissible ' . ($inline ? 'inline' : '') . '">'
    115                 . '<p><strong>' . $message . '</strong></p>'
    116                 . '</div>';
     198            . '<p><strong>' . $message . '</strong></p>'
     199            . '</div>';
    117200        return $notice;
    118201    }
    119202
    120     public static function buildTimezoneDropdown($name, $selected = '', array $otherAttrs = []) {
     203    public static function buildTimezoneDropdown($name, $selected = '', array $otherAttrs = [])
     204    {
    121205        $otherAttrsFlat = '';
    122206        if (count($otherAttrs) > 0) {
    123207            foreach ($otherAttrs as $key => $value) {
    124                 $otherAttrsFlat .=' ' . $key . '="' . $value . '" ';
     208                $otherAttrsFlat .= ' ' . $key . '="' . $value . '" ';
    125209            }
    126210        }
     
    136220            $select .= '<option ' . $selectedAttr . ' value="' . $zone . '">' . $zone . '</option>';
    137221        }
    138         $select .='</select>';
     222        $select .= '</select>';
    139223        return $select;
    140224    }
    141225
    142     public static function buildUtcTimezoneDropdown($name, $selected = '', array $otherAttrs = []) {
     226    public static function buildUtcTimezoneDropdown($name, $selected = '', array $otherAttrs = [])
     227    {
    143228        $otherAttrsFlat = '';
    144229        if (count($otherAttrs) > 0) {
    145230            foreach ($otherAttrs as $key => $value) {
    146                 $otherAttrsFlat .=' ' . $key . '="' . $value . '" ';
     231                $otherAttrsFlat .= ' ' . $key . '="' . $value . '" ';
    147232            }
    148233        }
     
    160245            }
    161246            $offset_name = 'UTC' . str_replace(['.25', '.5', '.75'], [':25', ':30', ':45'],
    162                             $offset_name);
     247                    $offset_name);
    163248
    164249            if ($selected !== '' && $selected !== null && $selected == $offset) {
     
    171256        }
    172257
    173         $select .='</select>';
     258        $select .= '</select>';
    174259        return $select;
    175260    }
    176261
    177     public static function buildCountryDropDown($name, $selected = '', array $otherAttrs = []) {
     262    public static function buildCountryDropDown($name, $selected = '', array $otherAttrs = [])
     263    {
    178264        $otherAttrsFlat = '';
    179265        if (count($otherAttrs) > 0) {
    180266            foreach ($otherAttrs as $key => $value) {
    181                 $otherAttrsFlat .=' ' . $key . '="' . $value . '" ';
     267                $otherAttrsFlat .= ' ' . $key . '="' . $value . '" ';
    182268            }
    183269        }
     
    190276                $selectedAttr = '';
    191277            }
    192             $select.='<option value="' . $code . '" ' . $selectedAttr . '>' . $country . '</option>';
    193         }
    194         $select .='</select>';
     278            $select .= '<option value="' . $code . '" ' . $selectedAttr . '>' . $country . '</option>';
     279        }
     280        $select .= '</select>';
    195281        return $select;
    196282    }
  • marketpowerwp/trunk/src/Multisoft/MPP/Replication/ReplicationController.php

    r1445352 r1460432  
    1212use Multisoft\MPP\Settings\SettingsGateway;
    1313use LePlugin\Core\View;
     14use LePlugin\Core\Utils;
    1415use Katzgrau\KLogger\Logger;
    1516use Psr\Log\LogLevel;
     
    3637        $this->add_action('admin_notices', 'mppe_replication_message');
    3738        $this->add_shortcode('mppe', 'mppe_replication_info');
     39        $this->add_filter('option_siteurl', 'replication_site_url');
     40        $this->add_filter('option_home', 'replication_home_url');
    3841
    3942        if (MPP_DEBUG) {
     
    5962        );
    6063
    61         /* @var $settingsGateway SettingsGateway */
    62         $settingsGateway = SettingsGateway::getInstance();
    63         $autoprefix = $settingsGateway->get(self::OPTION_AUTOPREFIX);
    64         if ($autoprefix && !is_admin()) {
    65             ob_start();
    66             add_action('shutdown', function () {
    67                 $final = '';
    68                 $levels = ob_get_level();
    69 
    70                 for ($i = 0; $i < $levels; $i++) {
    71                     $final .= ob_get_clean();
    72                 }
    73                 echo apply_filters('final_output', $final);
    74             }, 0);
    75             $this->add_filter('final_output', 'mppe_autoprefix_content');
    76         }
     64        ob_start();
     65        add_action('shutdown', function () {
     66            $final = '';
     67            $levels = ob_get_level();
     68
     69            for ($i = 0; $i < $levels; $i++) {
     70                $final .= ob_get_clean();
     71            }
     72            echo apply_filters('final_output', $final);
     73        }, 0);
     74        $this->add_filter('final_output', 'mppe_autoprefix_content');
     75
    7776    }
    7877
     
    173172    }
    174173
     174    public function replication_site_url($url)
     175    {
     176        if (is_admin()) {
     177            return $url;
     178        }
     179        return 'http://' . $_SERVER['HTTP_HOST'];
     180    }
     181
     182    public function replication_home_url($url)
     183    {
     184        if (is_admin()) {
     185            return $url;
     186        }
     187        return 'http://' . $_SERVER['HTTP_HOST'];
     188    }
     189
    175190    public function mppe_autoprefix_content($output)
    176191    {
    177192        require_once $this->_plugin_dir . '/src/Multisoft/MPP/Replication/simple_html_dom.php';
    178193        /* @var $replicationGateway ReplicationGateway */
     194        /* @var $settingsGateway SettingsGateway */
    179195        $replicationGateway = $this->replicationGateway;
     196        $settingsGateway = SettingsGateway::getInstance();
     197        $autoprefix = $settingsGateway->get(self::OPTION_AUTOPREFIX);
    180198
    181199        $xxx = $replicationGateway->get_replication_site_name();
    182         $host = $replicationGateway->parse_domain(site_url());
     200        $host = Utils::get_domain(site_url());
     201        $web_address_host = Utils::get_domain($settingsGateway->get(CoreController::OPTION_WEB_ADDRESS));
    183202        $html = str_get_html($output);
    184         foreach ($html->find('a') as $element) {
    185             $element_host = $replicationGateway->parse_domain($element->href);
    186             if ($element_host == $host) {
     203        if ($html) {
     204            foreach ($html->find('a') as $element) {
    187205                $href = parse_url($element->href);
    188                 if (substr($href['host'], 0, 1) !== $xxx) {
    189                     $new_href = $href['scheme'] . '://' . $xxx . '.' . str_replace($href['scheme'] . '://', '', $element->href);
    190                     $element->href = $new_href;
     206                $element_host = Utils::get_domain($href['host']);
     207
     208                if ($element_host == $host || $element_host == $web_address_host) {
     209                    $url_splice = explode('.', $href['host']);
     210                    if ($autoprefix && $url_splice[0] !== $xxx) {
     211                        $new_href = $href['scheme'] . '://' . $xxx . '.' .
     212                            Utils::str_replace_first($href['scheme'] . '://', '', $element->href);
     213                        $element->href = $new_href;
     214                    } else if (!$autoprefix && $url_splice[0] === $xxx) {
     215                        $new_href = Utils::str_replace_first($xxx . '.', '', $element->href);
     216                        $element->href = $new_href;
     217                    }
    191218                }
    192219            }
    193220        }
    194221        return $html;
    195 
    196222    }
    197223
  • marketpowerwp/trunk/src/Multisoft/MPP/Replication/ReplicationGateway.php

    r1456501 r1460432  
    99
    1010use LePlugin\Core\GatewayInterface;
     11use LePlugin\Core\Utils;
    1112use Multisoft\MPP\Core\CoreGateway;
    1213use Multisoft\MPP\Settings\SettingsGateway;
     
    3940    public function get_replication_site_name()
    4041    {
    41         $site_url = str_replace('https://', '', str_replace('http://', '', site_url()));
    42         $xxx = str_replace('.', '', str_replace($site_url, '', $_SERVER["SERVER_NAME"]));
     42        $domain = Utils::str_replace_first('https://', '', Utils::str_replace_first('http://', '', site_url()));
     43        $site_url = Utils::get_domain($domain);
     44        $xxx = str_replace('.', '', Utils::str_replace_first($site_url, '', $_SERVER["SERVER_NAME"]));
    4345        if (!strlen($xxx) || $xxx === 'localhost' || $xxx === 'www') {
    4446            $xxx = "admin";
    4547        }
    4648        return $xxx;
    47     }
    48 
    49     public function parse_domain($url)
    50     {
    51         $pieces = parse_url($url);
    52         $domain = isset($pieces['host']) ? $pieces['host'] : '';
    53         if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
    54             return $regs['domain'];
    55         }
    56         return false;
    57     }
    58 
    59     public function get_replication_info($force_new = false)
    60     {
    61         /* @var $settingsGateway SettingsGateway */
    62         /* @var $coreGateway CoreGateway */
    63         $settingsGateway = SettingsGateway::getInstance();
    64         $coreGateway = CoreGateway::getInstance();
    65 
    66         $mppe_domain = $settingsGateway->get(CoreController::OPTION_WEB_ADDRESS);
    67         $mppe_application_id = $settingsGateway->get(CoreController::OPTION_APPLICATION_ID);
    68         $mppe_web_path = $settingsGateway->get(CoreController::OPTION_WEB_PATH);
    69         $mppe_replication_path = $settingsGateway->get(ReplicationController::OPTION_REPLICATION_PATH);
    70         $mppe_replication_query_format = $settingsGateway->get(ReplicationController::OPTION_REPLICATION_PATH_QUERY_FORMAT);
    71         /* @var $logger \Katzgrau\KLogger\Logger */
    72         $logger = $this->logger;
    73         $logger->debug('Get Replication Info: ',
    74             array(
    75                 'mppe_domain' => $mppe_domain,
    76                 'mppe_application_id' => $mppe_application_id,
    77                 'mppe_web_path' => $mppe_web_path,
    78                 'mppe_replication_path' => $mppe_replication_path,
    79                 'mppe_replication_query_format' => $mppe_replication_query_format
    80             )
    81         );
    82 
    83         $replication_info = array();
    84 
    85         if ($mppe_domain && $mppe_application_id && $mppe_web_path && $mppe_replication_path) {
    86             $subdomain = $this->get_replication_site_name();
    87             $replication_pulled = $coreGateway->isset_transient(self::TRANS_REPLICATION_KEY, $subdomain);
    88             if (!$replication_pulled || $force_new) {
    89                 try {
    90                     $q_str = sprintf($mppe_replication_query_format, $mppe_application_id, $subdomain);
    91                     parse_str($q_str, $post_fields);
    92                     $url = $mppe_domain . $mppe_web_path . $mppe_replication_path;
    93                     $logger->info('Pulling replication info from: ' . $url);
    94                     $logger->debug('Post fields: ', $post_fields);
    95                     $data = wp_remote_post($url, array(
    96                         'method' => 'POST',
    97                         'timeout' => 45,
    98                         'redirection' => 5,
    99                         'httpversion' => '1.0',
    100                         'blocking' => true,
    101                         'headers' => array(),
    102                         'body' => $post_fields,
    103                         'cookies' => array()
    104                     ));
    105                     if (is_wp_error($data)) {
    106                         $error_message = $data->get_error_message();
    107                         $logger->critical($error_message);
    108                         $replication_info = $data;
    109                     } else {
    110                         if ($data['response']['code'] == '200') {
    111                             $logger->info("Successfully pulled replication info. Parsing replication data...");
    112                             $logger->debug("Received data: " . PHP_EOL . $data['body']);
    113                             $parser = xml_parser_create();
    114                             xml_parse_into_struct($parser, $data['body'], $values, $index);
    115                             xml_parser_free($parser);
    116                             $parsed_replication_info = array();
    117                             foreach ($values as $xml_child) {
    118                                 if ($xml_child["level"] == 2) {
    119                                     $parsed_replication_info[$xml_child["tag"]] =
    120                                         isset($xml_child["value"]) ? $xml_child["value"] : "";
    121                                 }
    122                             }
    123                             $logger->debug('Parsed replication info:', $parsed_replication_info);
    124                             if ($parsed_replication_info["DISTRIBUTORID"] != "00000000-0000-0000-0000-000000000000") {
    125                                 $replication_info = $parsed_replication_info;
    126                                 $coreGateway->set_transient(self::TRANS_REPLICATION_KEY, $replication_info, $subdomain);
    127                                 $logger->info('Successfully set replication info in mpp transient.');
    128                             } else {
    129                                 $logger->error('Retrieved DISTRIBUTORID is invalid . Replication info not set!');
    130                                 $replication_info = new \WP_Error('mpp_invalid_distributor_id',
    131                                     'Retrieved DISTRIBUTORID is invalid ');
    132                             }
    133                         } else {
    134                             $logger->error("HTTP Error: ", $data['response']);
    135                             $replication_info = new \WP_Error($data['response']['code'], $data['response']['message']);
    136                         }
    137                     }
    138                 } catch (\Exception $e) {
    139                     $logger->critical($e->getMessage());
    140                     $logger->debug($e->getTraceAsString());
    141                     $replication_info = new \WP_Error($e->getCode(), $e->getMessage());
    142                 }
    143             } else {
    144                 $replication_info = $coreGateway->get_transient(self::TRANS_REPLICATION_KEY, $subdomain);
    145                 $logger->debug("Replication info already in transient.", array($subdomain, $replication_info));
    146             }
    147         } else {
    148             $logger->warning("MPP Web Address or Application ID or replication settings not set!");
    149             $replication_info = new \WP_Error("NOT_SET", "MPP Web Address or Application ID or replication settings not set.");
    150         }
    151         return $replication_info;
    15249    }
    15350
Note: See TracChangeset for help on using the changeset viewer.