Synchronize the site's context license by fetching the license form the API and updating the local data with it.

@author Vova Feldman (@svovaf)
@since  2.0.0

@return \FS_Plugin_License|mixed
327253
private function sync_site_license() {
            $api = $this->get_api_user_scope();

            $result = $api->get( "/licenses/{$this->_license->id}.json?license_key=" . urlencode( $this->_license->secret_key ), true );

            if ( ! $this->is_api_result_entity( $result ) ) {
                return $result;
            }

            $license = $this->_update_site_license( new FS_Plugin_License( $result ) );
            $this->_store_licenses();

            return $license;
        }
1034248
function doc(): Result {
    return $this->collectionBuilder()
      ->addTask($this->taskFilesystemStack()
        ->copy('CHANGELOG.md', 'doc/about/changelog.md')
        ->copy('LICENSE.md', 'doc/about/license.md'))
      ->addTask($this->taskExec('mkdocs build --config-file=etc/mkdocs.yaml'))
      ->addTask($this->taskFilesystemStack()
        ->remove(['doc/about/changelog.md', 'doc/about/license.md']))
      ->run();
  }
280728
protected function proxy() {
        if (!$this->context) {
            $this->context = new DefaultsContext($this->version, $this->solution['assistantSid']);
        }

        return $this->context;
    }
328882
public function getContextualAcl()
    {
        $acl = $this->getRules();
        if (!$acl->hasRole($this->getContextId())) {
            $contextRoles = $this->getContext()->getRoles();
            if ($contextRoles instanceof RoleBucket) {
                $contextRoles = $contextRoles->toArray();
            }
            $acl->addRole(new Role($this->getContextId()), $contextRoles);
        }
        return $acl;
    }
585272
private function getRemoteLicense() {
		$call = new Api\Call( Configs::get( 'api' ) . '/api/plugin/getLicense?v=' .
			$this->apiVersion );

		if ( ! $response = $call->getError() ) {
			$response = $call->getResponse()->result->data;
		}

		return $response;
	}
550684
public static function getLicenseFile()

    {
        if (!isset(self::$file)) {
            $path = Grav::instance()['locator']->findResource('user-data://') . '/licenses.yaml';
            if (!file_exists($path)) {
                touch($path);
            }
            self::$file = CompiledYamlFile::instance($path);
        }

        return self::$file;
    }
1130385
function _update_site_license( $new_license ) {
            $this->_logger->entrance();

            $this->_license = $new_license;

            if ( ! is_object( $new_license ) ) {
                $this->_site->license_id = null;
                $this->_sync_site_subscription( null );

                return $this->_license;
            }

            $this->_site->license_id = $this->_license->id;

            if ( ! is_array( $this->_licenses ) ) {
                $this->_licenses = array();
            }

            $is_license_found = false;
            for ( $i = 0, $len = count( $this->_licenses ); $i < $len; $i ++ ) {
                if ( $new_license->id == $this->_licenses[ $i ]->id ) {
                    $this->_licenses[ $i ] = $new_license;

                    $is_license_found = true;
                    break;
                }
            }

            // If new license just append.
            if ( ! $is_license_found ) {
                $this->_licenses[] = $new_license;
            }

            $this->_sync_site_subscription( $new_license );

            return $this->_license;
        }
1157716
public function getLicense() : ?int 
    {
        if ( ! $this->hasLicense()) {
            $this->setLicense($this->getDefaultLicense());
        }
        return $this->license;
    }
534441
public function definition() {
        global $CFG;
        // type of plugin, string
        $this->plugin = $this->_customdata['plugin'];
        $this->instance = (isset($this->_customdata['instance'])
                && is_a($this->_customdata['instance'], 'repository_type'))
            ? $this->_customdata['instance'] : null;

        $this->action = $this->_customdata['action'];
        $this->pluginname = $this->_customdata['pluginname'];
        $mform =& $this->_form;
        $strrequired = get_string('required');

        $mform->addElement('hidden', 'action', $this->action);
        $mform->setType('action', PARAM_TEXT);
        $mform->addElement('hidden', 'repos', $this->plugin);
        $mform->setType('repos', PARAM_PLUGIN);

        // let the plugin add its specific fields
        $classname = 'repository_' . $this->plugin;
        require_once($CFG->dirroot . '/repository/' . $this->plugin . '/lib.php');
        //add "enable course/user instances" checkboxes if multiple instances are allowed
        $instanceoptionnames = repository::static_function($this->plugin, 'get_instance_option_names');

        $result = call_user_func(array($classname, 'type_config_form'), $mform, $classname);

        if (!empty($instanceoptionnames)) {
            $sm = get_string_manager();
            $component = 'repository';
            if ($sm->string_exists('enablecourseinstances', 'repository_' . $this->plugin)) {
                $component .= ('_' . $this->plugin);
            }
            $mform->addElement('checkbox', 'enablecourseinstances', get_string('enablecourseinstances', $component));
            $mform->setType('enablecourseinstances', PARAM_BOOL);

            $component = 'repository';
            if ($sm->string_exists('enableuserinstances', 'repository_' . $this->plugin)) {
                $component .= ('_' . $this->plugin);
            }
            $mform->addElement('checkbox', 'enableuserinstances', get_string('enableuserinstances', $component));
            $mform->setType('enableuserinstances', PARAM_BOOL);
        }

        // set the data if we have some.
        if ($this->instance) {
            $data = array();
            $option_names = call_user_func(array($classname,'get_type_option_names'));
            if (!empty($instanceoptionnames)){
                $option_names[] = 'enablecourseinstances';
                $option_names[] = 'enableuserinstances';
            }

            $instanceoptions = $this->instance->get_options();
            foreach ($option_names as $config) {
                if (!empty($instanceoptions[$config])) {
                    $data[$config] = $instanceoptions[$config];
                } else {
                    $data[$config] = '';
                }
            }
            // XXX: set plugin name for plugins which doesn't have muliti instances
            if (empty($instanceoptionnames)){
                $data['pluginname'] = $this->pluginname;
            }
            $this->set_data($data);
        }

        $this->add_action_buttons(true, get_string('save','repository'));
    }
588374
private function get_license_ids_associated_with_installs() {
            if ( ! $this->_is_network_active ) {
                if ( ! is_object( $this->_site ) ||
                     ! FS_Plugin_License::is_valid_id( $this->_site->license_id )
                ) {
                    return array();
                }

                return array( $this->_site->license_id );
            }

            $license_ids = array();
            $sites       = self::get_sites();
            foreach ( $sites as $site ) {
                $blog_id = self::get_site_blog_id( $site );
                $install = $this->get_install_by_blog_id( $blog_id );

                if ( ! is_object( $install ) ||
                     ! FS_Plugin_License::is_valid_id( $install->license_id )
                ) {
                    continue;
                }

                $license_ids[ $install->license_id ] = true;
            }

            return array_keys( $license_ids );
        }
1082637
protected function proxy() {
        if (!$this->context) {
            $this->context = new SampleContext(
                $this->version,
                $this->solution['assistantSid'],
                $this->solution['taskSid'],
                $this->solution['sid']
            );
        }

        return $this->context;
    }
354089
public function synchronize( $prefix = false, $plugin = false, array $permission_ids = [] )
    {
        $classname = '';
        if( !$plugin ) {
            $path = App::path('Controller/'.$prefix)[0];
        } else if( $plugin && is_string($prefix) ) {
            $path = App::path('Controller/' . $prefix, $plugin)[0];
            $classname = $plugin . '.';
        }

        $type_prefix = ( $prefix === '/' || is_bool($prefix) ) ? '' : '/'.$prefix;
        $Permission = TableRegistry::get('Permission');
        $files = scandir($path);
        $this->all_files = array_merge($this->all_files, $files);
        foreach($files as $file) {

            if(in_array($file, $this->_sync_ignore_list['*']) ||
                ( isset($this->_sync_ignore_list[$prefix]) && in_array($file, $this->_sync_ignore_list[$prefix]) )
            ) continue;

            if( is_dir($path.$file) ) {
                if( $prefix || !$plugin )
                    $permission_ids = $this->synchronize($file, $plugin, $permission_ids);
                else if( $plugin )
                    $permission_ids = $this->synchronize('/', $file, $permission_ids);
                continue;
            }

            $controller_name = str_replace('Controller', '', explode('.', $file)[0]);
            $class_name = App::classname($classname.$controller_name, 'Controller'.$type_prefix, 'Controller');
            if( empty($class_name) ) continue;
            $class = new ReflectionClass($class_name);
            $all_actions = $class->getMethods(ReflectionMethod::IS_PUBLIC);

            foreach($all_actions as $action) {
                $permission_prefix = '';
                if( $plugin )
                    $permission_prefix .= $plugin.'.';
                if( $prefix )
                    $permission_prefix .= $prefix;

                $class = new ReflectionClass($action->class);
                $file_name = explode('/',$class->getFileName());
                $file_name = end($file_name);
                if( in_array($file_name, $this->_sync_ignore_list['*']) || ($action->class != $class_name && !in_array($file_name, $this->all_files)) ||
                    in_array($action->name, $this->_sync_ignore_list['*']['*']) ||
                    ( isset($this->_sync_ignore_list['*'][$controller_name]) && in_array($action->name, $this->_sync_ignore_list['*'][$controller_name]) ) ||
                    ( isset($this->_sync_ignore_list[$permission_prefix]['*']) && in_array($action->name, $this->_sync_ignore_list[$permission_prefix]['*']) ) ||
                    ( isset($this->_sync_ignore_list[$permission_prefix][$controller_name]) && in_array($action->name, $this->_sync_ignore_list[$permission_prefix][$controller_name]) )
                ) continue;

                $unique_string = $this->getUniqueString($controller_name, $action->name, $prefix, $plugin);
                $permission_id = $Permission->find()
                    ->select(['id'])
                    ->where(['unique_string' => $unique_string])
                    ->first();

                if (is_null($permission_id)) {
                    $new_permission = $Permission->newEntity();
                    $new_permission->action = $action->name;
                    $new_permission->controller = $controller_name;
                    $new_permission->prefix = $permission_prefix;
                    $new_permission->unique_string = $unique_string;

                    if ($Permission->save($new_permission))
                        array_push($permission_ids, $new_permission->id);

                } else {
                    array_push($permission_ids, $permission_id->id);
                }
            }
        }

        if( !$plugin && !$prefix ) {
            foreach( $this->_plugins as $plugin )
                $permission_ids = $this->synchronize('/', $plugin, $permission_ids);

            $Permission->deleteAll(['id NOT IN'=>$permission_ids]);
        }

        return $permission_ids;
    }
738013
private function _sync_addon_license( $addon_id, $background ) {
            $this->_logger->entrance();

            if ( $this->is_addon_activated( $addon_id ) ) {
                // If already installed, use add-on sync.
                $fs_addon = self::get_instance_by_id( $addon_id );
                $fs_addon->_sync_license( $background );

                return;
            }

            // Validate add-on exists.
            $addon = $this->get_addon( $addon_id );

            if ( ! is_object( $addon ) ) {
                return;
            }

            // Add add-on into account add-ons.
            $account_addons = $this->get_account_addons();
            if ( ! is_array( $account_addons ) ) {
                $account_addons = array();
            }
            $account_addons[] = $addon->id;
            $account_addons   = array_unique( $account_addons );
            $this->_store_account_addons( $account_addons );

            // Load add-on licenses.
            $licenses = $this->_fetch_licenses( $addon->id );

            // Sync add-on licenses.
            if ( $this->is_array_instanceof( $licenses, 'FS_Plugin_License' ) ) {
                $this->_update_licenses( $licenses, $addon->id );

                if ( ! $this->is_addon_installed( $addon->id ) && FS_License_Manager::has_premium_license( $licenses ) ) {
                    $plans_result = $this->get_api_site_or_plugin_scope()->get( $this->add_show_pending( "/addons/{$addon_id}/plans.json" ) );

                    if ( ! isset( $plans_result->error ) ) {
                        $plans = array();
                        foreach ( $plans_result->plans as $plan ) {
                            $plans[] = new FS_Plugin_Plan( $plan );
                        }

                        $this->_admin_notices->add_sticky(
                            sprintf(
                                ( FS_Plan_Manager::instance()->has_free_plan( $plans ) ?
                                    $this->get_text_inline( 'Your %s Add-on plan was successfully upgraded.', 'addon-successfully-upgraded-message' ) :
                                    /* translators: %s:product name, e.g. Facebook add-on was successfully... */
                                    $this->get_text_inline( '%s Add-on was successfully purchased.', 'addon-successfully-purchased-message' ) ),
                                $addon->title
                            ) . ' ' . $this->get_latest_download_link(
                                $this->get_text_inline( 'Download the latest version', 'download-latest-version' ),
                                $addon_id
                            ),
                            'addon_plan_upgraded_' . $addon->slug,
                            $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
                        );
                    }
                }
            }
        }
1093146
protected function _load()
    {
        if (empty($this->_plugins)) {
            $this->_plugins = AnService::get('repos:settings.plugin')
                              ->getQuery()
                              ->where('enabled', '=', 1)
                              ->toEntityset();
        }

        return $this->_plugins;
    }
105602
public function syncCatalog()
    {
        try {
            //remove product with product id set and no product
            $this->catalogResourceFactory->create()
                ->removeOrphanProducts();

            return $this->catalogSyncFactory->create()->sync();

        } catch (\Exception $e) {
            $this->helper->debug((string)$e, []);
        }
    }
458122
function has_any_active_valid_license() {
            if ( ! fs_is_network_admin() ) {
                return $this->has_active_valid_license();
            }

            $installs            = $this->get_blog_install_map();
            $all_plugin_licenses = self::get_all_licenses( $this->_module_id );

            foreach ( $installs as $blog_id => $install ) {
                if ( ! FS_Plugin_License::is_valid_id( $install->license_id ) ) {
                    continue;
                }

                foreach ( $all_plugin_licenses as $license ) {
                    if ( $license->id == $install->license_id ) {
                        if ( self::is_active_valid_license( $license ) ) {
                            return true;
                        }
                    }
                }
            }

            return false;
        }
139431
private function _store_licenses( $store = true, $module_id = false, $licenses = array() ) {
            $this->_logger->entrance();

            $all_licenses = self::get_all_licenses();

            if ( ! FS_Plugin::is_valid_id( $module_id ) ) {
                $module_id = $this->_module_id;

                $user_licenses = is_array( $this->_licenses ) ?
                    $this->_licenses :
                    array();

                if ( empty( $user_licenses ) ) {
                    // If the context user doesn't have any license, don't update the licenses collection.
                    return;
                }

                $new_user_licenses_map = array();
                foreach ( $user_licenses as $user_license ) {
                    $new_user_licenses_map[ $user_license->id ] = $user_license;
                }

                self::store_user_id_license_ids_map( array_keys( $new_user_licenses_map ), $this->_module_id, $this->_user->id );

                // Update user licenses.
                $licenses_to_update_count = count( $new_user_licenses_map );
                foreach ( $all_licenses[ $module_id ] as $key => $license ) {
                    if ( 0 === $licenses_to_update_count ) {
                        break;
                    }

                    if ( isset( $new_user_licenses_map[ $license->id ] ) ) {
                        // Update license.
                        $all_licenses[ $module_id ][ $key ] = $new_user_licenses_map[ $license->id ];
                        unset( $new_user_licenses_map[ $license->id ] );

                        $licenses_to_update_count --;
                    }
                }

                if ( ! empty( $new_user_licenses_map ) ) {
                    // Add new licenses.
                    $all_licenses[ $module_id ] = array_merge( array_values( $new_user_licenses_map ), $all_licenses[ $module_id ] );
                }

                $licenses = $all_licenses[ $module_id ];
            }

            if ( ! isset( $all_licenses[ $module_id ] ) ) {
                $all_licenses[ $module_id ] = array();
            }

            $all_licenses[ $module_id ] = $licenses;

            self::$_accounts->set_option( 'all_licenses', $all_licenses, $store );
        }