Some snippets working with panels / panelizer

When we have a panels page in features and we want to move it to database we need to run

  $cache = page_manager_get_page_cache('page-PAGENAME');
  page_manager_save_page_cache($cache);

Build contextual link for the custom pane (after pane's information is in database)

if (user_access('administer site configuration')) {
    ctools_include('modal');
    ctools_modal_add_js();
    // Add ajax-responder so ctools_ajax_command_reload() will work.
    ctools_add_js('ajax-responder');
    // We expect Featured Bird pane to be only on Bird Guide. If this is not
    // the case we will need to change this select query.
    $pid = db_query('SELECT pid FROM {panels_pane} WHERE type = :type', array(':type' => 'PANE_MACHINE_NAME'))->fetchField();
    if (empty($pid)) {
       // Move panels page settings to database.
       $cache = page_manager_get_page_cache('page-PAGENAME');
       page_manager_save_page_cache($cache);
      $pid = db_query('SELECT pid FROM {panels_pane} WHERE type = :type', array(':type' => 'PANE_MACHINE_NAME'))->fetchField();
    }
    $links = array(
      'edit' => array(
        'title' => t('Edit'),
        'href' => 'panels/ajax/editor/edit-pane/panel_context:' . 'page-PAGENAME' . '::page_PAGENAME_panel_context::::/' . $pid,
        'query' => array('reload' => 'true'),
        'attributes' => array('class' => array('ctools-use-modal')),
      ),
    );
    $build = array(
      '#prefix' => '<div class="contextual-links-wrapper">',
      '#suffix' => '</div>',
      '#theme' => 'links__contextual',
      '#links' => $links,
      '#attributes' => array('class' => array('contextual-links')),
      '#attached' => array(
        'library' => array(array('contextual', 'contextual-links')),
      ),
    );
    $contextual_links = drupal_render($build);
  }

Save node, so its panelizer settings got overridden programmatically.

$node = node_load($node->nid, NULL, TRUE);
$node->panelizer['page_manager']->display_is_modified = TRUE;
node_save($node);